虽然我知道这是一个无处不在的问题,但我找不到它不起作用的原因:
我收集了以下代码,以便将图片上传到我的网站:
<html>
<body>
<form action="insert.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>
并且insert.php是:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$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"] < 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("/httpdocs/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/httpdocs/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/httpdocs/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
嗯,成功消息正确显示。但是,没有文件上载到指定的文件夹。
有什么想法吗?
答案 0 :(得分:1)
从根目录运行脚本,创建一个名为uploads
的文件夹,然后将/httpdocs/
的所有实例更改为uploads/
(如下所示),而不会打开/
和它应该(理论上)起作用,就像它对我一样。
要注意的一点是,如果文件小于20,000字节,请确保增加< 20000
。如果您尝试用于上传的文件高于此数字,则脚本将抛出错误Invalid file
确保该文件夹是可写的,例如:0755
或0777
(在我的情况下是0755)
我已在自己的服务器上使用uploads
作为我的上传文件夹进行了测试。
加上Nikolay states in his answer,你可能也想检查一下。
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$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"] < 20000) // <= you may have to increase this value
&& 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("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
答案 1 :(得分:0)
如果文件夹路径的所有内容都正确,您应该检查文件上传的限制,例如.htaccess:
php_value upload_max_filesize 10M
php_value post_max_size 10M
或在pnp.ini中:
upload_max_filesize = 10M
post_max_size = 10M
默认限制为2048KB。
答案 2 :(得分:0)
确定你的根文件夹$_SERVER["DOCUMENT_ROOT"]
以查看php是否可以访问该文件夹或将文件添加到右侧文件夹,否则一切看起来都很不错。
答案 3 :(得分:0)
您指定的地址不正确!
使用c:/wamp/www/site/httpdocs/
而非 http://site/httpdocs/
如果在Linux上检查文件夹的写入权限。
答案 4 :(得分:0)
这是100%和测试代码 addimage1是文件输入类型的名称 postimages是一个文件夹 并成功移动到postimages文件夹
move_uploaded_file($ _ FILES [&#34; addimage1&#34;] [&#34; tmp_name的值&#34], &#34; postimages /&#34; 。 $ _FILES [&#34; addimage1&#34;] [&#34;名称&#34;]);
答案 5 :(得分:-1)
if you have try image upload in php but not uplaod
if you are working on cpanel then
your image folder must be change permission from 755 to 777
its work for me
<?php
$target_path = "upload/";
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
echo "File uploaded successfully!";
} else{
echo "Sorry, file not uploaded, please try again!";
}
?>
if this helpful for you give a thumbs up