Codeigniter 4: Uploading files with move_uploaded_file

12,260

Solution 1

You are not using CodeIgniter's built in functions. Everything shown in your code are PHP functions. If you want to leverage built in CI functions, then look through the documentation as linked by @Boominathan Elango.

To get the file from the request:

$file = $this->request->getFile('here_goes_input_name');

As specified here

To move the file using CI function:

$file->move(WRITEPATH.'uploads', $newName);

As specified here

Solution 2

This worked for me and I hope it will also work for you. In codeigniter 4 please use this to upload your files and move it in your controller.


        if($imagefile = $this->request->getFiles())
{
    if($img = $imagefile['gfile'])
    {
        if ($img->isValid() && ! $img->hasMoved())
        {
            $newName = $img->getRandomName(); //This is if you want to change the file name to encrypted name
            $img->move(WRITEPATH.'uploads', $newName);
            
            // You can continue here to write a code to save the name to database
            // db_connect() or model format
                            
        }
    }
}

OR

if($img = $this->request->getFile('gfile'))
        {
            if ($img->isValid() && ! $img->hasMoved())
            {
                $newName = $img->getRandomName();
                $img->move(ROOTPATH . 'public/uploads/images/users', $newName);
 
                // You can continue here to write a code to save the name to database
                // db_connect() or model format
                            
            }
        }

Then in your html input field

<input type="file" name="gfile">

I hope this works else call my attention

Share:
12,260
arun kumar
Author by

arun kumar

Updated on June 04, 2022

Comments

  • arun kumar
    arun kumar almost 2 years

    I just started moving CodeIgniter 3 project to CodeIgniter 4.

    Everything works fine except file upload.

    I would like to keep the user uploaded files in /writable/uploads. Below is the code I use to move the uploaded file to desired location.

                $target_dir = '/writable/uploads/recordings/';
                $target_file = $target_dir . basename($_FILES["gfile"]["name"]);
                $FileType = pathinfo($target_file,PATHINFO_EXTENSION);
    
                if($FileType != "mp3") {            
                 $vmuploadOk = 1;
                }               
                else
                 $vmuploadOk = 1;   
    
    
                if ($vmuploadOk == 1) {
                    $greetfile = $id . "g" . basename($_FILES["gfile"]["name"]);
    
                    $target_filenew = $target_dir . $greetfile;     
    
                    move_uploaded_file($_FILES["gfile"]["tmp_name"], $target_filenew);                 
                }
    

    I assume that it is because CI4 keeps writable folder outside public folder.

    • Boominathan Elango
      Boominathan Elango about 4 years
    • Boominathan Elango
      Boominathan Elango about 4 years
      you can upload file outside public folder too thats why i show you the reference link above
    • arun kumar
      arun kumar about 4 years
      I used $file = $this->request->getFile('gfile'); $file->move(WRITEPATH.'uploads/recordings', $greetfile); It works but it does not overwrite the file if exists. It creates a copy of the file.