ItSolutionStuff.com

PHP Move File from One Folder to Another Example

By Hardik Savani • May 14, 2024
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:

<?php

/* 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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Remove Directory and Files in Directory Example

Read Now →

PHP Create Directory If It Doesn't Exist Example

Read Now →

How to Delete File from Public Folder / Storage Folder in Laravel?

Read Now →

Laravel Move File from One Folder to Another Example

Read Now →

Laravel Copy File from One Folder to Another Example

Read Now →

Laravel Check If Folder Exists Before Create Directory Example

Read Now →

How to Get Folder Path from File Path in PHP?

Read Now →

How to Get Config Variable Value in Laravel?

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →

How to Count Files in a Directory using Laravel?

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →