doudouji2016 2014-06-09 19:35
浏览 89

move_uploaded_file():无法在PHP中移动文件[关闭]

I have a PHP script that has worked just fine until recent PHP updates (Arch Linux). And I can’t seem to figure out how to resolve the issue. Here is the relevant portion of the script.

ini_set("log_errors", 1);
 ini_set("error_log", "php-error.log");
 require_once "Mail.php";
 require_once "crypto_new.php";
 $target_path  = "./";
 $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
//stuff
}

It seems to bork up when trying to execute move_uploaded_file, and it throws the following error.

PHP Notice:  Undefined index: uploadedfile in /srv/http/test/receiver.php on line 14
PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phppRkyQy' to './0a68457237fcc579c2ec03d69519f021' in /srv/http/test/receiver.php on line 14

Any idea what might be wrong and how to resolve it?

  • 写回答

1条回答 默认 最新

  • doumen1883 2014-06-09 19:43
    关注

    Here are my basic fixes to this script. They focus on using file_exists & is_dir before any other actions—like the move_uploaded_file process—are taken:

    ini_set("log_errors", 1);
    ini_set("error_log", "php-error.log");
    require_once "Mail.php";
    require_once "crypto_new.php";
    $target_path  = "./";
    if (file_exists($_FILES['uploadedfile']['tmp_name']) && is_dir($target_path)) {
      $target_path = $target_path . basename($_FILES['uploadedfile']['name']);
      if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
        //stuff
      }
    }
    

    That said, this might stop the PHP notices & warnings, but the underlying issue will still exist. Meaning, no more error messages! But the file won’t me acted on.

    The big thing for me here is what & where is the directory ./? Does that exist in the context of the PHP script? Can Apache (which runs PHP) access ./ and write files there? Meaning, are the directory permissions for ./ usable by Apache?

    In general, relative paths like this are a pain & cause of many avoidable headaches. So I would recommend you simply set that to a full path like this:

    $target_path  = "/full/path/to/my/files/";
    

    And then see if it works.

    But this is really the best anyone can do with the small snippet of code you are providing. For all anyone knows, the rest of the form/upload process could be screwing up as well. And when it gets to this point, it’s just the last stage of failure in a broken process.

    But I am fairly confident that the recommendation to set a full path for $target_path and checking file/directory permissions will clear this issue up.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算