0
我想用我的表單數據上傳圖片,但是出現錯誤。用php上傳圖片
在我的形式,該文件的線路有:
<li id="li_6" >
<label class="description" for="file">Filename:</label>
<div>
<input type="file" name="file" id="file"><br>
</div><p class="guidelines" id="guide_5"><small>Select the image to upload.</small></p>
</li>
這裏是上傳腳本:
if(isset($_POST['title'])) {
$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"] < 60000)
&& 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"],
"media/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
$img_url = '/media/uploads/'.$_FILES["file"]["name"];
$title = $_POST['title'];
$description = $_POST['description'];
$type = $_POST['description'];
$startPrice = $_POST['startPrice'];
$reservePrice = $_POST['reservePrice'];
$buyPrice = $_POST['buyPrice'];
也不太清楚怎麼回事錯了。我已經看過了。我的意思是代碼直接來自手冊..它應該工作!
不過,我得到這個錯誤:
> A PHP Error was encountered Severity: Notice Message: Undefined index: file Filename: controllers/user.php Line Number: 76
哪條線是#76? – 2014-09-13 19:35:22
$ temp = explode(「。」,$ _FILES [「file」] [「name」]); – user3175451 2014-09-13 19:36:50
您是否已將'enctype =「multipart/form-data」'添加到您的表單中? – 2014-09-13 19:37:37