在此,我非常感谢您的帮助。我整整一整天都在努力地将图像上传到运行Zeus的共享主机上(而不是apache,是的,我知道,它们正在改变!)。主持人指责代码,但不会告诉我为什么“由于他们不是程序员”,我尝试了这么多不同版本的表单脚本,所以我选择不了。我当然已经检查了php ini文件配置的上传限制(主机不允许更改),它们都是128meg。因此,看起来这不太可能是原因。脚本的结果是,我们得到最后的“成功加载文件”消息,但是加载的文件大小为零(因此目标目录中没有新内容)。我对php还是比较陌生,所以请不要轻易讲专业术语。谢谢你。
这是两个文件>>首先是表格...
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
和表单引用的php文件...
<?php
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size >350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to..";
echo $target;
echo "..The uploaded file size is $uploaded_size";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
首先,请确保该uploads
文件夹存在,并且该文件夹的写权限设置为755
或777
另外,您有两个未分配的变量$uploaded_size
和$uploaded_type
,因此将失败。
您将需要使用类似if ($_FILES["file"]["size"] <350000)
etc之类的东西,这可能是它失败的另一个原因。
尝试一下,看看是否适合您,这就是我使用的方法:
修改$allowedExts = array("gif", "jpeg", "jpg", "png");
允许的文件扩展名。
仅当大小小于 350000
注意:更改<input name="uploaded" type="file" />
以<input name="file" type="file" />
查看处理程序代码下的窗体。
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 350000)
&& 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";
}
?>
还要将您的表单更改为此,以反映PHP处理程序:
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" />
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句