douqie1816 2018-08-16 04:24
浏览 69
已采纳

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

$(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;
}
?>

This is my duplicate question. I have create add and remove more file using jQuery. Now, What happen when I click on add button it show me to choose another file similarly again and again. I am able to upload multiple file like this. But the problem is that when I click on submit button to insert into the database and move image to the folder it show me error i.e.

</div>
  • 写回答

1条回答 默认 最新

  • douao7937 2018-08-16 04:53
    关注

    I found the reason in your code:

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

    The $banner_image_tmp will return an array. So, there will be an error

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

    http://php.net/manual/en/features.file-upload.multiple.php. Your code should be:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化