我在尝试将此图片上传工作时遇到问题。我是否使用enctype ='multipart / form-data'是行不通的,但是当我不使用它时,当我向表单标签添加enctype ='multipart / form-data'时,页面会显示我的错误当我点击提交时,它什么都不做。有没有人看到任何问题?任何帮助表示赞赏!
以下是上传代码:
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
if ($submit == "Add" && $_SERVER['REQUEST_METHOD'] == 'POST' && $product_name != "" && $product_image != "" && $product_description != "" && $product_price != "" && $product_dimensions != "" && $product_category != "" && $product_subcategory != "" && $product_manufacturer != "" && $product_toronto_avail != "" && $product_mississauga_avail != "" && $product_concord_avail != "" && $product_pickering_avail != "" && $product_barrie_avail != "" && $product_kitchener_avail != "") {
// Create Query
$addQuery="INSERT INTO products (name, image, description, price, dimensions, category, subcategory, manufacturer, toronto, mississauga, concord, pickering, barrie, kitchener) VALUES ('$product_name', '$product_image', '$product_description', '$product_price', '$product_dimensions', '$product_category', '$product_subcategory', '$product_manufacturer', '$product_toronto_avail', '$product_mississauga_avail', '$product_concord_avail', '$product_pickering_avail', '$product_barrie_avail', '$product_kitchener_avail')";
// Run Query & Add into products table
mysql_query($addQuery);
//reads the name of the file the user submitted for uploading
$image= $product_image;
//if it is not empty
if ($image)
{
//get the extension of the file in a lower case format
$extension = getExtension($image);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=filesize($image);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="../gallery/".$image_name;
//we verify if the image has been uploaded, and print error instead
$moved = move_uploaded_file($image_name,$newname);
if (!$moved)
{
echo '<h1>Copy unsuccessfull!</h1>';
echo $newname . '<br/>';
echo $image_name;
}}}
//Successful!
$successAdd = "<p style='font-size:12px;font-style:italic;font-family:georgia;'>You have successfully added \"<span style='color:#014380;'>" . $product_name . "</span>\" to your products database.</p>";
} else if ($submit == "Add" && $_SERVER['REQUEST_METHOD'] == 'POST' && $product_name == "" || $product_image == "" || $product_description == "" || $product_price == "" || $product_dimensions == "" || $product_category == "" || $product_subcategory == "" || $product_manufacturer == "" || $product_toronto_avail == "" || $product_mississauga_avail == "" || $product_concord_avail == "" || $product_pickering_avail == "" || $product_barrie_avail == "" || $product_kitchener_avail == "") {
$successAdd = "<p style='font-size:12px;color:red;font-style:italic;font-family:georgia;'>Please fill in all required fields. </p>";
} else {
$successAdd = "";
}
&安培;下面是表格代码:
echo "<form method='post' enctype='multipart/form-data' action='./add_product.php' >";
echo "<table border='0' class='addProductTable' cellpadding='3' cellspacing='0'>";
print ("<tr class='tableHeader large'>
<th style='width:200px;'></th>
<th style='width:175px;'></th>
<th></th>
</tr>
<tr>
<td> Name:* </td>
<td> <input type='text' name='name' /> </td>
<td></td>
</tr>
<tr>
<td> Upload an Image:* </td>
<td> <input type='file' name='image' /> </td>
<td></td>
</tr>
<tr>
<td> Description:* </td>
<td colspan='2'> <textarea name='description' cols='25' rows='5'></textarea> </td>
</tr>
<tr>
<td> Price:* </td>
<td> <input type='text' name='price' /> </td>
<td></td>
</tr>
<tr>
<td> Dimensions:* </td>
<td> <input type='text' name='dimensions' /> </td>
<td></td>
</tr>
<tr>
<td> Category:* </td>
<td> <input type='text' name='category' /> </td>
<td></td>
</tr>
<tr>
<td> Subcategory:* </td>
<td> <input type='text' name='subcategory' /> </td>
<td></td>
</tr>
<tr>
<td> Manufacturer:* </td>
<td> <input type='text' name='manufacturer' /> </td>
<td></td>
</tr>
<tr>
<td> Availability:* </td>
<td colspan='2'>
<div style='float:left; margin-right:10px;'>
<span>Toronto:</span> <input type='text' style='width:30px;' name='toronto_availability' /> <br/>
<span>Mississauga:</span> <input type='text' style='width:30px;' name='mississauga_availability' /> <br/>
<span>Concord:</span> <input type='text' style='width:30px;' name='concord_availability' /> <br/>
</div>
<div style='float:left;'>
<span>Pickering:</span> <input type='text' style='width:30px;' name='pickering_availability' /> <br/>
<span>Barrie:</span> <input type='text' style='width:30px;' name='barrie_availability' /> <br/>
<span>Kitchener:</span> <input type='text' style='width:30px;' name='kitchener_availability' /> <br/>
</div>
<div style='clear:both;'></div>
</td>
</tr>");
echo "</table>";
echo "<input type='submit' name='submit' value='Add' />";
echo "</form>";
答案 0 :(得分:0)
我无法看到问题。你确定只是你改变了吗?我同意Damien Pirsy关于不回应它的评论。可能尝试删除。在动作中的文件名之前,因为这不是&#34;正常&#34;并且可能会混淆浏览器。
echo "<form method='post' enctype='multipart/form-data' action='/add_product.php'>";
答案 1 :(得分:0)
也许是,由于某种原因,您使用的javascript验证脚本实际上剥离了-enctype =“ multipart / form-data”-行,因此即使您已在其中设置了它您的视图只需检查生成的html并确保...