php上传文件,我想知道我的代码哪里错了,怎么改?

<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
</head>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="提交">
</form>

</body>
</html>

php
<?php
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
echo $_FILES["file"]["size"];
$extension = end($temp); // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 204800) // 小于 200 kb
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "错误:: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>";

// 判断当期目录下的 upload 目录是否存在该文件
// 如果没有 upload 目录,你需要创建它,upload 目录权限为 777
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " 文件已经存在。 ";
}
else
{
// 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "文件存储在: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "非法的文件格式";
}
?>

点击完“上传文件”后报错
Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.23
代码是我粘的,我只改了路径那里

经代码测试(原文复制、粘贴),测试代码结果正常:


访问页面:



提交结果页面:



从错误提示来看:


Object not found! 通常是出现在有 类 相关代码中才会再现,但在你的代码貌似没有牵涉到。


The requested URL was not found on this server. 

这个错误是 请求的 URL 在服务器上没找到,建议你检查下 upload_file.php 在服务器上的路径以及文件名是否正确。

追问

我找到刚才的错误了,但是现在又出现了warning

截图是运行结果


而且在目录上为什么没有我新加入的文件呢?

追答

从截图信息来看,错误发生在:将临时文件转存为指定目录文件的过程。

从错误提示信息看,建议你检查 upload 目录是否存在,以及目录的权限。

追问

我看我的临时目录路径好像也不对;还有目录的权限我不会看,也不知道怎么改,求助!

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网