Move image from one folder to another folder in php

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?

September 6, 2020 Category : PHP

In this short tutorial we will cover an php move file from one folder to another. i would like to share with you move image from one folder to another in php. you will learn move a file from one folder to another folder in php. if you have question about how to move file from one folder to another in php then i will give simple example with solution.

If you need to move file from one folder to another using php code then we can use "rename()" function of php. php provide rename function to move your file from one place to another.

So, here i will give you very simple example and syntax how it works to move file from one folder to another in php.

Let's see bellow syntax and example that will help you.

Syntax:

bool rename( string $source, string $destination, resource $context )

You can see bellow parameter in details:

$source: you need to give file path that you want to rename.

$destination: you need to give file path for destination source.

$context: this is optional, It specifies the context resource created with stream_context_create() function.

Example:

/* Store the path of source file */

$filePath = 'images/test.jpeg';

/* Store the path of destination file */

$destinationFilePath = 'copyImages/test.jpeg';

/* Move File from images to copyImages folder */

if( !rename($filePath, $destinationFilePath) ) {

echo "File can't be moved!";

}

else {

echo "File has been moved!";

}

?>

I hope it can help you...

Move image from one folder to another folder in php

PHP move and copy all files from one folder or directory to another folder. In this tutorial, you will learn How to move a file into a different folder or directory on the server using PHP and as well as How to copy a files into a different folder on the server using PHP?

  • PHP Move File From One Folder to Another
  • PHP Copy File From One Folder to Another

PHP Move File From One Folder to Another

If you need to move file from one folder/directory to another using php code then you can use “rename()” function of php. php provide rename function to move your file from one place to another.

First of all, you need to see syntax of rename() function.

Syntax:

bool rename( string $source, string $destination, resource $context )

See the explanation of rename() function parameters are followings:

$source: you need to give file path that you want to rename.

$destination: you need to give file path for destination source.

$context: this is optional, It specifies the context resource created with stream_context_create() function.

Example:

PHP Copy File From One Folder to Another

If you need to copy file from one folder/directory to another using php code then you can use “copy()” function of php. php provide copy function to move your file from one place to another.

First of all, you need to see syntax of copy() function.

Syntax:

bool copy( string $source, string $destination, resource $context )

See the explanation of copy() function parameters are followings:

$source: you need to give file path that you want to copy.

$destination: you need to give file path for destination source.

$context: this is optional, It specifies the context resource created with stream_context_create() function.

Example:

Conclusion

In this tutorial, you have learned how to move and copy a file into a different folder or directory on the server using PHP

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

View all posts by Admin

Post navigation

How do I move an image from one directory to another in php?

PHP Copy File From One Folder to Another If you need to copy file from one folder to another using php code then you can use “copy()” function of php. php provide copy function to move your file from one place to another.

How do I move a folder to another directory in php?

Linked.
Move folders and files to a folder..
Using scandir() to find folders in a directory (PHP).
Copy all files and folder from one directory to another directory PHP..

How to move to another file in php?

The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How do I move a file from one directory to another in laravel?

How to Move Files or Folder to One Folder to Another in Laravel ?.
Step 1: Create a fresh laravel project. Open a terminal window and type below command to create a new project. ... .
Step 2 : Create controller. Let's create a controller and add method performMoveFiles..
Step 3: Create two routes in routes/web.php..