【发布时间】:2011-09-27 16:30:27
【问题描述】:
我有这个表格:
<form name="commentform" id="commentform" action="comment.php" method="post"
enctype="multipart/form-data">
Your Name:
<textarea maxlength="60" rows="1" cols="62" class="margin" name="name"
id="name"> </textarea> <br><br>
Submit Picture
<input type="file" name="pic" id="pic" /> <br><br>
<input type="Submit" value="Submit" />
</form>
这是验证图片的 PHP(来自 W3Schools.com):
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
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。提交表单后如何使其加载?此外,此脚本似乎不起作用。
【问题讨论】:
-
不清楚你想做什么!?
-
阻止脚本立即执行
-
oOoK!我建议你 jahufar 回答!在处理您的 php 之前,您需要验证表单是否已提交!使用 if( isset($_POST('submit')) ),把你的整个代码放在这个 if 语句中
标签: php forms file file-upload