duanou3868 2010-07-30 14:16
浏览 37
已采纳

为什么我的php移动文件脚本实际上没有移动文件?

I am uploading files to a server using php and while the move_uploaded_file function returns no errors, the file is not in the destination folder. As you can see I am using the exact path from root, and the files being uploaded are lower than the max size.

$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";  
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
    echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}
else {
    echo $_FILES['image']['error']."<br />";
    echo $target;   
}

Any help is appreciated.

Eric R.

  • 写回答

3条回答 默认 最新

  • douxi3977 2010-07-30 15:22
    关注

    Most like this is a permissions issue. I'm going to assume you don't have any kind of direct shell access to check this stuff directly, so here's how to do it from within the script:

    Check if the $target directory exists:

    $target = '/data/etc....';
    if (!is_dir($target)) {
        die("Directory $target is not a directory");
    }
    

    Check if it's writeable:

    if (!is_writable($target)) {
        die("Directory $target is not writeable");
    }
    

    Check if the full target filename exists/is writable - maybe it exists but can't be overwritten:

     $target = $target . basename($_FILES['image']['name']);
     if (!is_writeable($target)) {
         die("File $target isn't writeable");
     }
    

    Beyond that:

    if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
        echo $_FILES['image']['error']."<br />";
    }
    

    Echoing out the error parameter here is of no use, it refers purely to the upload process. If the file was uploaded correctly, but could not be moved, this will still only echo out a 0 (e.g. the UPLOAD_ERR_OK constant). The proper way of checking for errors goes something like this:

    if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
        // file was properly uploaded
        if (!is_uploaded_File(...)) {
            die("Something done goofed - not uploaded file");
        }
        if (!move_uploaded_file(...)) {
            echo "Couldn't move file, possible diagnostic information:"
            print_r(error_get_last());
            die();
    
        }
    } else {
        die("Upload failed with error {$_FILES['images']['error']}");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)