move_uploaded_file():无法在PHP中移动文件

时间:2014-06-09 19:35:42

标签: php

我有一个PHP脚本,在最近的PHP更新(Arch Linux)之前一直运行良好。我似乎无法弄清楚如何解决这个问题。这是脚本的相关部分。

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
}

尝试执行move_uploaded_file时似乎很困难,并且会引发以下错误。

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

知道可能出现的问题以及解决方法吗?

1 个答案:

答案 0 :(得分:1)

以下是我对此脚本的基本修复。他们专注于使用file_exists&在is_dir进程之类的任何其他操作之前move_uploaded_file

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
  }
}

也就是说,这可能会阻止PHP通知&警告,但潜在的问题仍然存在。意思是,没有更多错误消息!但是文件不会让我采取行动。

对我来说最重要的是什么&目录./在哪里?这是否存在于PHP脚本的上下文中? Apache(运行PHP)可以访问./并在那里写文件吗?意思是Apache可以使用./的目录权限吗?

一般来说,像这样的相对路径是痛苦的。引起许多可避免的头痛。所以我建议您只需将其设置为完整路径,如下所示:

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

然后看看它是否有效。

但这确实是任何人都可以使用您提供的一小段代码。对于所有人都知道,表单/上传过程的其余部分也可能会搞砸。当它达到这一点时,它只是一个破碎过程中失败的最后阶段。

但我相信,为$target_path设置完整路径并检查文件/目录权限的建议将清除此问题。

相关问题