使用jquery表单插件失败的文件上传

时间:2013-02-26 09:38:54

标签: php jquery ajax forms jquery-plugins

我正在尝试将图像与其他表单数据一起上传到我的数据库。我现在有以下表格:

<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()函数会导致此错误,并且由于某种原因无法上传该文件。现在可能出现什么问题?

0 个答案:

没有答案