duandong1869 2018-06-14 16:01
浏览 46
已采纳

尽管路径和现有文件正确,move_uploaded_file仍无法正常工作

I'm really struggling with an issue relating the move_uploaded_file function of php.

The path should be correct, but see yourself:

$imgDir     = "../img/".$CATEGORY;
$fileName   = $_FILES['image']['name'];
$maxsize    = 800;
$compQuality= 75;
if(!is_dir($imgDir)) {
    mkdir($imgDir.'/tn', 0777, true);
    chmod($imgDir, 0777);       
    chmod($imgDir, 0777);
}

$imgDir     = $imgDir."/";
$imgTnDir   = $imgDir."tn/";

I have also tested it by echo-ing everything and it seems to work, but when it comes to the move_uploaded_file it still does not work, even though I test if the file really exists:

if(file_exists($imgDir.$fileName)) {
    $status = "The file ".$fileName." already exists, please choose a different title.";
} 

if(!move_uploaded_file($_FILES['image']['tmp_name'], $imgDir.$fileName)) {
    $status = "File upload failed, sorry.";
}   

if(!empty($status)) { 
    echo $status;
    exit();
}   

I hope that someone can help me. If you want I can print anything out you need or give you more snippets of the code. The var $_FILES['image']['tmp_name'] does not only exist, but has a proper name by the way.

Also I have checked the php.ini and both uploading is allowed and the size is surely set high enough.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongmingxiang0312 2018-06-14 16:13
    关注

    make sure that you have the right path in $imgDir. You can try:

    if(!is_writable($imgDir)) { exit('CANNOT_WRITE_IN_DIR'); }
    

    and also you can use an absolute path, like this:

    $imgDir = __DIR__ . "/../img/".$CATEGORY;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向