【问题标题】:Uploading image error上传图片错误
【发布时间】:2011-06-01 08:56:47
【问题描述】:

大家好,我正在尝试将图像上传到指定目录,我得到的所有输出都是“上传文件时出错,请重试!”

$upload_dir = "blog/wp-content/uploads/products";    
if (file_exists($upload_dir) && is_writable($upload_dir)) {
    echo "Directory exists and is fine....";
}
else {
    echo "Upload directory is not writable, or does not exist.";
}

$target_path = "blog/wp-content/uploads/products/";

$target_path = $target_path . basename( $_FILES['image_file']['name']); 

if(move_uploaded_file($_FILES['image_file']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['image_file']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";

}

有什么想法吗?

【问题讨论】:

  • 你也应该检查$_FILES['image_file']['error']中的上传状态(php.net docs)这个错误代码可能表明失败的原因是什么
  • 您真的不应该保留原始文件名。想象一下有人上传了一个 php 文件,你的网络服务器会很乐意运行它。

标签: php debugging image-processing error-handling


【解决方案1】:

如果您没有收到警告,这是唯一的原因:If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE. 来自文档的引用。

使用.is_uploaded_file检查上传。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2016-01-02
    • 2018-06-22
    • 2013-07-07
    • 2015-11-02
    相关资源
    最近更新 更多