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


Upload Multiple Images and Insert Record for Each image path

我正在寻找一种方法,一次上传"多个图像",然后为每个图像单独插入记录。现在我只能上传一张图片,它的记录被顺利保存到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[]",但不知道如何提取数组数据以分别上载和插入记录。请帮助

您必须循环通过$_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...   
}

可以像这样重构代码

<?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(); 
?>