用于多文件上传的PHP脚本

时间:2013-06-13 05:23:01

标签: php file-upload

我正在为开放式购物车制作Cpanel,我必须上传3张这样的产品图片

<label for="password" class="control-label"><b>Product image:</b></label>
   <div class="controls">
<input type="file" name="Upload" id="password" class="span9 required" required />
</div>
<label for="password" class="control-label"><b>Product image:</b></label>
   <div class="controls">
    <input type="file" name="Upload" id="password" class="span9 required" required />

</div>

<label for="password" class="control-label"><b>Product image:</b></label>
   <div class="controls">
    <input type="file" name="Upload" id="password" class="span9 required" required />
</div>

我搜索了许多文件上传代码,但没有一个可以让我的工作轻松。 我正在使用Framework,因此我将此数据发送到数据库类:$this->mlogin->dbfunction($POST,$image1,$image2,$image3);

我的插入查询将执行

这是数据库代码

public function product_upload($_POST,$path) {

    $this->insert_query="INSERT INTO `_product`(
                                             `_p_name`,
                                             `_p_cat`,
                                             `_p_brand`,
                                             `_p_price`,
                                             `_p_amount`,
                                             `_p_image`,
                                             `_p_dcrp`

                                             )
                                             VALUES(
                                             '" .$_POST['name'] . "',
                                                 '" .$_POST['cat'] . "',
                                                     '" .$_POST['brand'] . "',
                                                         '" .$_POST['price'] . "',
                                                             '" .$_POST['avail'] . "',
                                                             '" .$path . "',
                                                                 '" .$_POST['detail'] . "'
                                                                 )";

    return $this->select();
}

1 个答案:

答案 0 :(得分:0)

除了有关未经过滤的输入的安全问题外,在$ _POST中找不到上传的文件,但在$ _FILES中找到了更复杂的布局。

只需执行var_output($ _ FILES)检查其内容并参考此处的文档: http://php.net/manual/en/features.file-upload.php

要牢记的步骤:

  • 设置表单enc类型:enctype =“multipart / form-data”
  • 检查$ _FILES,而不是$ _POST

$ _FILES的布局:

$_FILES[$fieldname]['name']  // The original name of the file on the client machine.
$_FILES[$fieldname]['type']  // The mime type of the file. Unsafe! Dont rely on it!
$_FILES[$fieldname]['size'] // The size, in bytes, of the uploaded file.
$_FILES[$fieldname]['tmp_name'] // The temporary filename of the file on the server.
$_FILES[$fieldname]['error'] // The error code associated with this file upload.