PHP图像上传奇怪的问题
Posted
技术标签:
【中文标题】PHP图像上传奇怪的问题【英文标题】:PHP Image Upload Strange Issue 【发布时间】:2012-04-15 07:27:06 【问题描述】:<?
if(isset($_POST['submit']))
$url = 'http://www.connectedcampus.org/ckeditor/images/uploads/';
if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) )
$message = "No file uploaded.";
else if ($_FILES['upload']["size"] == 0)
$message = "The file is of zero length.";
else if (($_FILES['upload']["type"] != "image/pjpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png"))
$message = "The image must be in either JPG or PNG format. Please upload a JPG or PNG instead.";
else if($_FILES["file"]["size"] > 1000000)
$message = "File size too big!";
else if(file_exists($url.$_FILES["file"]["name"]))
$message=$_FILES["file"]["name"] . " already exists. ";
else if (!is_uploaded_file($_FILES['upload']["tmp_name"]))
$message = "Invalid File!";
else
$message = "success!";
$move = move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']);
echo $_FILES['upload']['tmp_name']."<br>";
echo $url.$_FILES['upload']['name'];
if(!$move)
$message = "Error moving uploaded file.";
$url = $url.$_FILES['upload']['name'];
echo $message;
</script>";
?>
这会打印“移动上传的文件时出错” 这意味着 move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']) 返回假。但是我 500% 确定我已经正确创建了 http://www.connectedcampus.org/ckeditor/images/uploads/ 文件夹......那为什么会出现错误?
好的,现在改为:
$url = 'bloguploads/';
if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) )
$message = "No file uploaded.";
else if ($_FILES['upload']["size"] == 0)
$message = "The file is of zero length.";
else if (($_FILES['upload']["type"] != "image/pjpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png"))
$message = "The image must be in either JPG or PNG format. Please upload a JPG or PNG instead.";
else if($_FILES["file"]["size"] > 1000000)
$message = "File size too big!";
else if(file_exists($url.$_FILES["file"]["name"]))
$message=$_FILES["file"]["name"] . " already exists. ";
else if (!is_uploaded_file($_FILES['upload']["tmp_name"]))
$message = "Invalid File!";
else
$message = "";
$move = move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']);
if(!$move)
$message = "Error moving uploaded file.";
$url = $url.$_FILES['upload']['name'];
echo $message;
</script>";
?>
现在问题是,它说“已经存在”。为什么?通过ftp客户端可以看到文件没有上传
【问题讨论】:
读/写权限可能吗? @hjpotter92 chmod($_FILES['upload']['tmp_name'], 777); $message = "成功!"; $move = move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']);并更改了上传文件夹的权限......没有任何作用 它说“已经存在”而不是“Filename.jpg 已经存在”的事实应该是一个提示...... 【参考方案1】:move_uploaded_file
适用于文件系统,而不适用于 url。
而不是$url
move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']);
使用文件系统路径,即:
move_uploaded_file($_FILES['upload']['tmp_name'], "/home/www/uploads/".$_FILES['upload']['name']);
【讨论】:
$url = '/ckeditor/images/uploads/';仍然显示上传文件错误! :-/ 如果您尝试使用相对路径,请从开头删除/
。但更好的办法是花一些时间了解绝对/相对路径、当前工作目录以及如何在 php 中处理它们。
查看上述问题中的编辑。我更正了路径。现在非常奇怪的是它说“已经存在”
尝试在你的 ftp 客户端刷新文件列表,也许它已经存在了。此外,您应该使用自己的文件名(将空格更改为下划线,添加上传时间戳等)并在尝试将文件移动到上传文件夹之前检查文件是否存在。以上是关于PHP图像上传奇怪的问题的主要内容,如果未能解决你的问题,请参考以下文章
PHP curl文件上传::当表单名称为整数时出现奇怪的curl行为
使用 PHP GD lib 压缩和调整图像大小不适用于 png 和 jpg 的奇怪结果