我正在尝试创建一个PHP文件来将图像上传到我的网站。我有以下代码,但它无法正常工作。任何人都可以帮我修复它,所以我没有得到任何错误?有没有更好的方法来做这样的事情?我知道这是老式的。
$uploaddir = "uploads/images";
$allowed_ext = "jpg, JPG, png, gif";
$max_size = "500000";
$max_height = "4000";
$max_width = "4000";
$extension = pathinfo ($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for ($i = 0; $i <= count($allowed_paths); $i++){
if ($allowed_paths[$i] == "$extension"){
$ok = "1";
}
}
if ($ok == "1"){
if($_FILES['file']['size'] > $max_size){
print "Your file is too big!";
exit;
}
}
if($max_width && $max_height){
list($width, $height, $type, $w) = getimagesize($_FILES['file']['name']);
if ($width > $max_width || $height > $max_height){
print "Your file width/height are too big!";
exit;
}
}
if (is_uploaded_file($_FILES['file']['tmp_name'])){
move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir.'/'.$_FILES['file']['name']);
print "Your file was uploaded successfully :)";
}
else
print "Wrong extensin";
?>
当我运行脚本时,我收到以下错误:
Warning: getimagesize(1 (8).jpg) [function.getimagesize]: failed to open stream: No such file or directory in D:\Hosting\8923686\html\uploadedimages\upload.php on line 25
Warning: move_uploaded_file(uploads/images/1 (8).jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\phpE5F5.tmp' to 'uploads/images/1 (8).jpg' in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33
或
invalid file
任何人都可以告诉我我的问题在哪里?
答案 0 :(得分:2)
对于第一个错误,请将getimagesize($_FILES['file']['name'])
更改为getimagesize($_FILES['file']['tmp_name'])
。
至于其他两个错误,请确保您的脚本有权将文件写入您尝试将上传文件移动到的文件夹。
修改:还尝试在$uploaddir
中使用完整的绝对路径,而不仅仅是uploads/images
。由于您使用的是不在文档根目录中的脚本的相对路径,因此PHP可能会查找错误的目录。
答案 1 :(得分:1)
getimagesize($_FILES['file']['name']
是第一个罪魁祸首:你的意思是getimagesize($_FILES['file']['tmp_name']
对于其他错误,您需要检查uploads/images
的权限 - 它们显然是对网络服务器用户的限制。