通过PHP上传文件不起作用 [英] Uploading file via PHP, doesn't work
问题描述
我从此页面复制了代码: http://www.w3schools.com/php/php_file_upload.asp
I copied the code from this page: http://www.w3schools.com/php/php_file_upload.asp
问题是上传文件时没有出现错误,而是 显示哪个应该正确:
Problem is that i don't get an error when uploading a file, instead this shows which should be correct:
上传:images.jpeg 类型:图片/jpeg 大小:5.8603515625 kB 临时文件:/tmp/phpZ67YXk 存储在:upload/images.jpeg
Upload: images.jpeg Type: image/jpeg Size: 5.8603515625 kB Temp file: /tmp/phpZ67YXk Stored in: upload/images.jpeg
但是没有文件保存在服务器上.
But no file is saved on the server.
我不知道出什么问题了,但是我在考虑许可的问题,仍然有一个文件夹 具有777权限的命名上传.
I don't know what is wrong but I'm thinking in terms of permission, still there is a folder named upload with 777 permissions.
这些php文件托管在在线虚拟主机上,因此我不在本地运行.
These php-files are hosted on a online web host so I don't run this locally.
HTML表单
HTML-form
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
upload_file.php
upload_file.php
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
推荐答案
我认为问题出在您的目标相对路径(相对于您当前的工作目录= .php文件的路径),请尝试使其为绝对值像这样:
I suppose the problem is in your relative path of destination (relative according to your current working directory = path of your .php file), try to make it absolutem like this:
move_uploaded_file(
$_FILES["file"]["tmp_name"],
$_SERVER['DOCUMENT_ROOT'] . 'upload/' . $_FILES["file"]["name"]
);
这篇关于通过PHP上传文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!