【问题标题】:Why isn't my php move file script actually moving the file?为什么我的 php 移动文件脚本实际上没有移动文件?
【发布时间】:2010-07-30 14:16:27
【问题描述】:

我正在使用 php 将文件上传到服务器,虽然 move_uploaded_file 函数没有返回错误,但文件不在目标文件夹中。如您所见,我使用的是根目录的确切路径,并且上传的文件小于最大大小。

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

感谢任何帮助。

埃里克·R.

【问题讨论】:

  • 脚本产生的内容(我的意思是 $target...等)
  • 你的脚本对文件夹有写权限吗?
  • 有写权限,错误输出0,$target输出路径和正确的文件名

标签: php html file-upload


【解决方案1】:

这很可能是权限问题。我假设您没有任何类型的直接 shell 访问权限来直接检查这些内容,所以下面是如何从脚本中执行此操作:

检查$target目录是否存在:

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

检查它是否可写:

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

检查完整的目标文件名是否存在/是否可写 - 也许它存在但不能被覆盖:

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

除此之外:

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

这里回显error参数是没有用的,它纯粹是指上传过程。如果文件已正确上传,但无法移动,这仍然只会回显0(例如 UPLOAD_ERR_OK 常量)。检查错误的正确方法是这样的:

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']}");
}

【讨论】:

  • 所以这基本上是正确的。问题解释:“上传”中的文件夹“2010”是可写的,因此您的所有检查都顺利通过。但是,Uploads 本身是不可写的。做所有这些检查让我想到了这一点。因此,当我使 Uploads 可写时,它运行良好。谢谢马克。
【解决方案2】:

您需要确保托管您网页的人已配置了允许您上传和移动文件的设置。大多数人会禁用这些功能,因为这会带来安全风险。

只需向他们发送电子邮件并询问他们是否已启用。

希望这会有所帮助。

【讨论】:

  • PS 你在本地机器上测试过脚本吗?
  • 截至去年我能够上传到服务器。我会和他们核实一下。
【解决方案3】:

您对 is_uploaded_file 和 move_uploaded_file 的调用会有所不同。对于 is_uploaded_file,您正在检查“名称”,而对于 move_uploaded_file,您正在传入“tmp_name”。尝试更改对 move_uploaded_file 的调用以使用“名称”

【讨论】:

  • move_uploaded_file() 需要服务器端临时名称作为源,它在tmp_name 中。将其更改为 name 是错误的,因为这是客户端的原始文件名,这将导致移动失败并显示“没有此类文件”。
猜你喜欢
  • 2012-08-13
  • 2014-04-20
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多