0

I create image upload code. This creates a directory first and then uploads images to it.

In the form, I upload 3 images.

My Code is:

$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"];
            $image_name  = $dir_path_up .$filename . $i. ".jpg";
                echo $image_name;

                $i++;
        }
        die();

Result of echo

assets/images/product_images/255_2555/1.jpg
assets/images/product_images/255_2555/2.jpg
assets/images/product_images/255_2555/3.jpg

But that images are not getting uploaded into the directory. I echo the image name which I created. It's already been renamed. Then why are images not getting uploaded into the directory?

What is wrong with this??

5
  • 3
    you forget to write move updoad command in your code? Commented Apr 17, 2015 at 11:37
  • @saty Explain please Commented Apr 17, 2015 at 11:38
  • what you use core php or codignator? Commented Apr 17, 2015 at 11:39
  • formget.com/codeigniter-upload-image Check This Commented Apr 17, 2015 at 11:41
  • ellislab.com/codeigniter/user-guide/libraries/… Commented Apr 17, 2015 at 11:43

3 Answers 3

5

Read manual from here CI image upload.

In codignator we use upload library to upload image .

you pass youe parameter according to your requirment

           $config['upload_path'] = 'assets/images/product_images/'.$model."/";
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('Your View', $error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

                $this->load->view('Your View', $data);
                }
Sign up to request clarification or add additional context in comments.

3 Comments

in this case how to rename image??
after upload or before upload?
want rename before upload
3

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

1 Comment

So there is no index "5.0-Reflective.jpg" in $_FILES, the error states, ,therefore it cannot be uploaded. I do not even know where your $image array comes from, neither where it is filled with what. I am not sure how to help you on this. You need to figure it out by yourself or provide more info on what you are doing.
1

This work fine with multiple image upload

<?php
    $j = 0;     // Variable for indexing uploaded image.
    $target_path = 'assets/images/product_images/' . $last_id . '/';     // Declaring Path for uploaded images.
    for ( $i = 0; $i < count($_FILES['image']['name']); $i++ )
    {
        // Loop to get individual element from the array
        $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
        $ext = explode('.', basename($_FILES['image']['name'][$i]));   // Explode file name from dot(.)
        $file_extension = end($ext); // Store extensions in the variable.
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
        $j = $j + 1;      // Increment the number of uploaded images according to the files in array.
        if ( ($_FILES["image"]["size"][$i] < 100000000)     // Approx. 100kb files can be uploaded.
            && in_array($file_extension, $validextensions)
        )
        {
            move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_path);
        }
        else
        {

        }
    }

Update, you can use

foreach ($image as $new_image)
{
    $dir_path_up = 'assets/images/product_images/'.$model."/";
    $filename = $_FILES["$new_image"]["name"];
    $image_name  = $dir_path_up .$filename . $i. ".jpg";
    if (move_uploaded_file($_FILES["$new_image"]["tmp_name"], $image_name))
    {
        echo "The file has been uploaded.";
    }
    else
    {
        echo "There was an error uploading the file.";
    }
    $i++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.