如何插入多个图像并将其移入php中的文件夹?

时间:2018-08-16 04:24:48

标签: php jquery

$(document).ready(function(){
    $("#add_small").click(function(event){
        event.preventDefault();
        $(".add_small").append('<div class="form-group">\
            <label for="product_small_image">Product Image:</label>\
            <input type="file" name="product_image[]" class="product_image" value=""/>\
            <a href="javascript:void(0)" class="remove_small">Remove</a>\
        <div>');
    });

    jQuery(document).on('click', '.remove_small', function() {
        jQuery(this).parent().remove();
        return false;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form method="post" name="user_registration" class="register" enctype="multipart/form-data">
		<div class="form-group">
			<label for="product_small_image">Product Image:</label>
			<input type="file" name="product_image[]" class="product_image" value=""/>
			<a href="javascript:void(0)" id="add_small">Add</a>
		</div>
		<div class="add_small"></div>
		<br/>

		<input name="submit" type="submit" class="submit" value="Submit" />
    </form>

<?php
    if(isset($_POST['submit']))
{   
    $file_ary = reArrayFiles($_FILES['product_images']);
    foreach ($file_ary as $file) 
    {
        //print 'File Name: ' . $file['name'];
        //print 'File Type: ' . $file['type'];
        //print 'File Size: ' . $file['size'];

        $folder_Path = "../images/product_image/";
        $banner_image_name = str_replace(" ", "", strtolower(basename($file['name'])));
        $banner_image_name_upload = $folder_Path.$banner_image_name;
        //$banner_image_tmp = $_FILES['product_image']['tmp_name'];
        $imageFileType = strtolower(pathinfo($banner_image_name,PATHINFO_EXTENSION));
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) 
        {
            $msg = "<div class='alert alert-success'>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</div>";
        }
        else
        {
            if (move_uploaded_file($banner_image_name_upload)) 
            {
                $set_width = 600;
                $set_height = 600;
                $banner_image_source_file = $banner_image_name_upload;
                $banner_image_save_file = $banner_image_name_upload;
                list($width_orig, $height_orig) = getimagesize($banner_image_source_file);
                $image_p = imagecreatetruecolor($set_width, $set_height);
                $image = imagecreatefromjpeg($banner_image_source_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $set_width, $set_height, $width_orig, $height_orig);
                imagejpeg($image_p, $banner_image_save_file, 75);

                $query = "insert into inventory_add_in_stock(`product_image`)values('".$file['name']."')";
                echo $query;

                $result = mysqli_query($con,$query);
                if($result==true)
                {
                    $msg = "<div class='alert alert-success'>Record Save Successfully</div>";
                }
                else
                {
                    $msg = "<div class='alert alert-danger'>Unable to Save Please Try Again !!!</div>";
                }
            }
            else
            {
                $msg = "<div class='alert alert-danger'>Unable to Proceeed Please Try Again !!!</div>";
            }
        }
    }
}

function reArrayFiles(&$file_post) {
    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);
    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }
    return $file_ary;
}
?>

这是我重复的问题。我已经创建添加和使用jQuery删除更多文件。现在,当我单击添加按钮时会发生什么,它表明我一次又一次地选择另一个文件。我可以像这样上传多个文件。但是问题是,当我单击“提交”按钮插入数据库并将图像移动到文件夹时,它向我显示错误,即

1 个答案:

答案 0 :(得分:1)

我在您的代码中找到了原因:

$banner_image_tmp = $_FILES['product_image']['tmp_name'];

$banner_image_tmp将返回一个数组。所以会有一个错误

move_uploaded_file() expects parameter 1 to be string, array given

http://php.net/manual/en/features.file-upload.multiple.php。您的代码应为:

if(isset($_POST['submit']))
{
    $file_ary = reArrayFiles($_FILES['product_image']);

    foreach ($file_ary as $file) {
        print 'File Name: ' . $file['name'];
        print 'File Type: ' . $file['type'];
        print 'File Size: ' . $file['size'];
        //Your custom code here

    }
}

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}