我正在尝试将图像与其他表单数据一起上传到我的数据库。我现在有以下表格:
<form method="post" action="newproduct.php" id="newproduct" enctype="multipart/form-data" >
<table>
<tr>
<td><label for="productName">Product Name:</label></td>
<td>
<input type="text" name='productName' id='productName' />
</td>
</tr>
<tr>
<td><label for="productNumber">Product Number:</label></td>
<td>
<input type="text" name='productNumber' id='productNumber' />
</td>
</tr>
<tr>
<td><label for="sflf"> SF / LF: </label></td>
<td>
<input type="text" name="sflf" id="sflf" />
</td>
</tr>
<tr>
<td><label for="file" >Image:</label></td>
<td>
<input type="file" name="file" id="file" />
</td>
</tr>
</table>
</form>
我正在使用以下 jquery 脚本将数据发送到我的 newproduct.php 文件:
var options = {
target: '#message',
url:'newproduct.php',
beforeSubmit: function() {$('#uploader').html('<img src="loader.gif" border="0" />');},
success: function() {$('#uploader').html('New Product Was Successfully Added');}
};
var newProductDialog = $("#addProductDiv").dialog({
autoOpen: false,
title: "Add New Product",
modal: true,
width: 500,
show: { effect: 'drop',direction:"up",duration:1000 },
hide: { effect: 'drop',direction:"down",duration:1000 },
buttons: {
'Add':function() {
//submitNewProduct($("#newproduct"));
$("#newproduct").ajaxSubmit(options);
return false;
},
'Cancel':function() {
$(this).dialog('close');
$("#container").fadeTo('slow',1);
}
}
});
$("#loadNewProduct").click(function() {
newProductDialog.dialog('open');
$("#container").fadeTo('slow',.4);
return false;
});
这是我的 newproduct.php 的样子:
include("../includes/database.php");
$path = "../images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST))
{
$productNo = isset($_POST['productNumber']) ? $_POST['productNumber'] : "";
$productName = isset($_POST['productName']) ? $_POST['productName'] : "";
$sflf = isset($_POST['sflf']) ? $_POST['sflf'] : "";
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2048*2048))
{
$tmp = $_FILES['file']['tmp_name'];
if(move_uploaded_file($tmp, $path.$name))
{
$sql= "insert into inventory (productName,sfLf,image,time,productNo) values
('{$productName}','{$sflf}','{$name}',now(),'{$productNo}')";
$database->query($sql) or die(mysql_error()."Database Failed")
}
else echo "There Was A Problem Adding Your Product. Please Try Again"
}
else echo "Size Cannot Exceed 2 MB";
}
else echo "Invalid File Format";
}
}
但是,我不断收到 500 内部服务器错误,所以我知道我的 newproduct.php 有一些我无法解决的问题。
编辑:
在调试了我的 newproduct.php 之后,我发现 move_uploaded_file() 函数会导致这个错误,并且由于某种原因它无法上传文件。现在可能出了什么问题?