Image upload not working in codeignitor

后端 未结 3 1279
萌比男神i
萌比男神i 2021-01-27 07:41

I create image upload code. this create directory first and then upload images to it.

In form I upload 3 images.

My Code is:

$dir_path = \'asset         


        
3条回答
  •  悲&欢浪女
    2021-01-27 08:29

    When you upload a file with PHP it gets stored into a temporary folder. You can access this file in your script with $_FILES, but it is still in your temporary folder it will be deleted upon next request.

    To keep your uploaded File, you need to move it to the desired location.

    The function for this is called move_uploaded_file (API: http://php.net/manual/en/function.move-uploaded-file.php)

    bool move_uploaded_file ( string $filename , string $destination )
    

    In your case it would result in something like:

    $dir_path = 'assets/images/product_images/'.$model;
    mkdir($dir_path, 0777);
    
    $i = 1 ;
    
        foreach ($image as $new_image)
            {
                $dir_path_up = 'assets/images/product_images/'.$model."/";
                $filename = $_FILES["$new_image"]["name"];
                $tmp_name = $_FILES["$new_image"]["tmp_name"]
                $image_name  = $dir_path_up .$filename . $i. ".jpg";
                move_uploaded_file($tmp_name, $image_name);
                    echo $image_name;
    
                    $i++;
            }
            die();
    

提交回复
热议问题