我写了一个 php 脚本来上传图片,脚本和上传文件夹都在我的 htdocs 文件夹中。这是我的脚本:
<head>
<title>Upload an Image</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 0.21" />
<style type="text/css" title="text/css" media="all">
.error {
font-weight: bold;
color:#Coo;
}
</style>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD']=='POST') {
if(isset($_FILES['upload'])) {
$allowed = array('image/pjpeg','image/JPG','image/X-PNG','image/png','image/x-png');
if(in_array($_FILES['upload']['type'],$allowed)) {
if(move_uploaded_file($_FILES['upload'] ['tmp_name'],"../uploads/{_FILES['upload']['name']}")) {
echo '<p><em>The file has been uploaded!</em> </p>';
}
}
else {
echo '<p class="error">Please uploaded a JPEG or PNG image.</p>';
}
}
if($_FILES['upload']['error'] > 0) {
echo '<p class="error">The file could not be uploaded because: <strong></p>';
switch ($_FILES['upload']['error']) {
case 1:
print 'The file exeeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exeeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded';
break;
case 6:
print 'No tmp folder was available';
break;
case 7:
print 'Unable to write on the disk';
break;
case 8:
print 'File upload stopped';
break;
default:
print 'A system error occured';
break;
}
print '</strong>';
}
if(file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
unlink ($_FILES['upload']['tmp_name']);
}
}
?>
<form enctype="multipart/form-data" action="upload_image.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset><legend>Select a JPEG/PNG image of 512KB or smaller to be uploaded:</legend>
<p><b>File:</b><input type="file" name="upload" /></p>
</fieldset>
<div align="center">
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
我收到如下错误:警告:move_uploaded_file(../uploads/110221110100_96.png):无法打开流:第 27 行的 /opt/lampp/htdocs/upload_image.php 中没有此类文件或目录
警告:move_uploaded_file(): Unable to move '/tmp/phph4iy1j' to '../uploads/110221110100_96.png' in /opt/lampp/htdocs/upload_image.php on line 27.
但我已经设置了所有必需的权限.....期待帮助,谢谢