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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?