上传文件不起作用,这段代码有什么问题?

时间:2010-01-08 20:47:05

标签: php upload

这是用于上传的PHP代码:

$upload = "uploads/";
$upload = $upload . basename($_FILES['bgimage']['name']);

if (move_uploaded_file($_FILES['bgimage']['tmp_name'], $upload)) {

    echo "The file has been uploaded successfully.";

} else { echo "Error"; }

当我测试脚本时,它会显示“文件已成功上传”。但是当我检查FTP服务器时,它确实没有......

此外,如果您需要知道,这是HTML代码:

表格标签:

<form name="profilestyle" action="account.php?action=profiletheme" method="post" enctype="multipart/form-data">

输入标记:

<input type="file" name="bgimage" />

额外信息: 是的,我记得CHM上传目录

3 个答案:

答案 0 :(得分:1)

奇怪的是,就我所见,代码看起来很好。

您是否可以使用file_exists()检查文件是否存在,但您的FTP用户可能无法看到?

if (move_uploaded_file($_FILES['bgimage']['tmp_name'], $upload)) {

echo "The file '$upload' has been uploaded successfully.";
if (file_exists($upload)) echo "And it exists! It is ".filesize($upload)." bytes big.";
 else echo "But it doesn't exist.";

} else { echo "Error"; }

答案 1 :(得分:1)

您还需要检查$_FILES['bgimage']['error']以确保它等于UPLOAD_ERR_OK并且不是错误代码。

答案 2 :(得分:0)

请尝试以下测试代码

$upload = "uploads/";
$upload = $upload . basename($_FILES['bgimage']['name']);

sprintf('<pre>Debug: moving file from %s to %s</pre>',
  $_FILES['bgimage']['tmp_name'],
  $upload
);
if (move_uploaded_file($_FILES['bgimage']['tmp_name'], $upload)) {
  echo "The file has been uploaded successfully.";
  sprintf('<pre>Debug: realpath=%s, filesize=%d</pre>',
    realpath($upload),
    filesize($upload)
  );
}
else {
  echo "Error";
}

和esp。密切关注realpath = xyz输出。