为每个图像路径上传多个图像并插入记录

时间:2015-02-27 07:49:23

标签: php mysql file-upload

我正在寻找一种方法一次上传“多个图像”,然后单独为每个图像插入记录。现在我只能上传一张图片,并将其记录顺利保存到mysql中。

我一直在搜索Stackoverflow和谷歌,找到了一些答案,但无法使用我的PHP脚本。

此处使用从用户那里获取必要数据的表单:

<form id="myForm" action="post_product2.php" method="post" enctype="multipart/form-data">
Category:   
    <select name="pcate">
        <option value="category1">category1</option>
        <option value="category2">category2</option>
    </select>
    <br />
Product Name: 
    <input type="text" name="pname" />
    <br />
    <br />
Description: 
    <textarea name="pdesc"></textarea>
    <br />
    <br />
Product Code: 
    <input type="text" name="pcode">
        <br />
        <br />
Price: 
        <input type="text" name="pprice" />
        <br />
        <br />
Offers: 
        <select name="poffers">
            <option value="New Arrival">-- New Arrival --</option>
            <option value="ON SALE">-- ON SALE --</option>
            <option value="Exclusive">-- Exclusive --</option>
            <option value="Featured">-- Featured --</option>
        </select>
        <br />
        <br />
        <input type="file" name="file_img[]" multiple="multiple" />
        <br />
        <br />
        <input type="hidden" name="pdate" value="
            <?php echo $now->format('Y-m-d H:i:s');  ?>" />
            <br />
            <input type="submit" name="btn_upload" value="Submit">
            </form>

以下是处理表单数据的代码:

<?php
if(isset($_POST['btn_upload']))
{
    $prodcat = $_POST['pcate'];
    $prodname = $_POST['pname'];
    $prodec = $_POST['pdesc'];
    $prodcode = $_POST['pcode'];
    $prodprice = $_POST['pprice'];
    $prodffers = $_POST['poffers'];
    $pDate = $_POST['pdate'];

    $filetmp = $_FILES["file_img"]["tmp_name"];
    $filename = $_FILES["file_img"]["name"];
    $filetype = $_FILES["file_img"]["type"];
    $filesize = $_FILES["file_img"]["size"];
    $fileinfo = getimagesize($_FILES["file_img"]["tmp_name"]);
    $filewidth = $fileinfo[0];
    $fileheight = $fileinfo[1];
    // GETS FILE EXTENSION
    $fileextension = pathinfo($filename, PATHINFO_EXTENSION);
    $microtime = preg_replace('/[^A-Za-z0-9]/', "", microtime());
    $filepath = "../static/products/".$microtime.".".$fileextension;
    $filepath_thumb = "../static/products/thumbs/".$microtime.".".$fileextension;
    $filepath2 = "/static/products/".$microtime.".".$fileextension;
    move_uploaded_file($filetmp,$filepath);


    if($filetype == "image/jpeg")
    {
        $imagecreate = "imagecreatefromjpeg";
        $imageformat = "imagejpeg";
    }

    if($filetype == "image/png")
    {                                                 
        $imagecreate = "imagecreatefrompng";
        $imageformat = "imagepng";
    }

    if($filetype == "image/gif")
    {                                                 
     $imagecreate= "imagecreatefromgif";
     $imageformat = "imagegif";
    }

    $new_width = "400";
    $new_height = "400";

    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = $imagecreate($filepath); //photo folder

    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);
    $imageformat($image_p, $filepath_thumb);//thumb folder

$sql = "INSERT INTO `products` (prod_cat,prod_name,prod_desc,prod_code,prod_price,prod_offer,img_path,datetime) VALUES ('$prodcat','$prodname','$prodec','$prodcode','$prodprice','$prodffers','$filepath2','$pDate')";
$result = mysql_query($sql);
}

header("Location: Dashboard.php");
die();
?>

对于多个文件选择,我将输入字段设为multiple="multiple",并将名称设置为数组name="file_img[]",但不知道如何提取要上载的数组数据和单独插入记录。请求帮助

2 个答案:

答案 0 :(得分:1)

你必须遍历$_POST['file_img'],因为它将是一个包含每个文件元素的多维数组:

foreach($_POST['file_img'] as $file){
    $filetmp = $file["tmp_name"];
    $filename = $file["name"];
    $filetype = $file["type"];
    $filesize = $file["size"]; 
    //and so on...   
}

答案 1 :(得分:1)

您的代码可以像这样重构

<?php 
if(isset($_POST['btn_upload'])) 
{ 
    function deal_image($i) { 
        $prodcat = $_POST['pcate']; 
        $prodname = $_POST['pname']; 
        $prodec = $_POST['pdesc']; 
        $prodcode = $_POST['pcode']; 
        $prodprice = $_POST['pprice']; 
        $prodffers = $_POST['poffers']; 
        $pDate = $_POST['pdate']; 

        $filetmp = $_FILES["file_img"]["tmp_name"][$i]; 
        $filename = $_FILES["file_img"]["name"][$i]; 
        $filetype = $_FILES["file_img"]["type"][$i]; 
        $filesize = $_FILES["file_img"]["size"][$i]; 
        $fileinfo = getimagesize(filetmp); 
        $filewidth = $fileinfo[0]; 
        $fileheight = $fileinfo[1]; 
        // GETS FILE EXTENSION 
        $fileextension = pathinfo($filename, PATHINFO_EXTENSION); 
        $microtime = preg_replace('/[^A-Za-z0-9]/', "", microtime()); 
        $filepath = "../static/products/".$microtime.".".$fileextension; 
        $filepath_thumb = "../static/products/thumbs/".$microtime.".".$fileextension; 
        $filepath2 = "/static/products/".$microtime.".".$fileextension; 
        move_uploaded_file($filetmp,$filepath); 


        if($filetype == "image/jpeg") 
        { 
            $imagecreate = "imagecreatefromjpeg"; 
            $imageformat = "imagejpeg"; 
        } 

        if($filetype == "image/png") 
        { 
            $imagecreate = "imagecreatefrompng"; 
            $imageformat = "imagepng"; 
        } 

        if($filetype == "image/gif") 
        { 
            $imagecreate= "imagecreatefromgif"; 
            $imageformat = "imagegif"; 
        } 

        $new_width = "400"; 
        $new_height = "400"; 

        $image_p = imagecreatetruecolor($new_width, $new_height); 
        $image = $imagecreate($filepath); //photo folder 

        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight); 
        $imageformat($image_p, $filepath_thumb);//thumb folder 

        $sql = "INSERT INTO `products` (prod_cat,prod_name,prod_desc,prod_code,prod_price,prod_offer,img_path,datetime) VALUES ('$prodcat','$prodname','$prodec','$prodcode','$prodprice','$prodffers','$filepath2','$pDate')"; 
        $result = mysql_query($sql); 
    }
    foreach($_FILES["file_img"]['name'] as $id => $file) {deal_image($id);} 
} 

header("Location: Dashboard.php"); 
die(); 
?>