PHP Move File from One Folder to Another Tutorial

PHP 8 , PHP

Nicesnippets

1513

13-04-2021


PHP Move File from One Folder to Another Tutorial

Hi Guys,

Today, I will learn you how to move file from one folder to another using php.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.

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

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:

<?php

/* Store the path of source file */

$filePath = 'images/demo.png';

/* Store the path of destination file */

$destinationFilePath = 'otherImages/demo.png';

/* Move File from images to copyImages folder */

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

echo "File can't be moved!";

}

else {

echo "File has been moved!";

}

?>

it will help you..