Search code examples
phplaravelfile-uploadlaravel-5.3image-uploading

How can I move an image from one folder to another folder?


I use Laravel 5.3

When I upload image, I save the image in :

C:\xampp\htdocs\myshop\storage\temp

My code to save the image like this :

private function addPhoto(UploadedFile $photo, $fileName)
{
    $destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'temp';
    $photo->move($destinationPath, $fileName);
    return $fileName;
}

When I click button submit, I want to move the image from folder storage to folder public

So I want to move the image in :

C:\xampp\htdocs\myshop\public\img

How can I move the image in folder public?


Solution

  • Take a look at the rename function. Try something like that:

    $org_image="C:\xampp\htdocs\myshop\storage\temp\xxxx.jpg";
    $destination="C:\xampp\htdocs\myshop\public\img";
    
    $img_name=basename($org_image);
    
    if( rename( $org_image , $destination.'/'.$img_name )){
     echo 'moved!';
    } else {
     echo 'failed';
    }