move file from folder in php

File management in PHP is often tricky when you start out. In this case I am talking about moving files between folders or directories on your server.

To do this we will only need to use the PHP rename function.

Content

  • rename snippet between folders
  • Examples of moving files between folders in PHP
  • Examples of copying files between folders

rename snippet between folders

I’ll start straight to the point: an example of move file into a different folder in php.

In the following example I move a file called “file.pdf” from a folder “folder-imgs” to another “folder-imgs-delete”.

<?php
rename( 'var/www/ejemplo-dominio/folder-imgs/fichero.pdf', 'var/www/ejemplo-dominio/folder-imgs-delete/fichero.pdf' );
?>

Are you looking for more complete information? So, keep reading…

Examples of moving files between folders in PHP

Let’s start moving files from one folder to another, yes, MOVE, since the file will disappear from the source path. As if we dragged a file with the mouse in Windows.

For this task I will use the php function rename me a function that does two things:

  • rename a file indicating the full path of this plus its name and extension.
  • move a file from a folder to another using full paths (once again).
<?php

$sep = '/';
$filename = 'srcodigofuente-test-ficheros.jpg';
$sourceFolder = 'carpeta-origen';
$destFolder = 'carpeta-destino';

rename( $sourceFolder . $sep, $destFolder . $sep . $filename);

?>

In this example, although I have assembled everything parameterized and through php concatenations, what I have done is show you how through a source route and a destination routewe managed to move the file from one point to another.

Very important to tell you that the name of the file It must always appear, or what is the same, that we must indicate the complete path.

Examples of copying files between folders

I have already taught you how to move files between folders, now it’s time to perform a copy or what is the same: copy and paste the file in another directory.

I am going to solve this with the PHP copy function.

<?php

$sep = '/';
$filename = 'srcodigofuente-test-ficheros.jpg';
$sourceFolder = 'carpeta-origen';
$destFolder = 'carpeta-destino';

copy( __DIR__ . $sourceFolder . $sep, __DIR__ . $destFolder . $sep . $filename);

?>

You’ve understood? No? I’ll explain:

  1. The first is easy, I define the separator symbol for more comfort when writing.
  2. I parameterize the name of the file.
  3. I define and save in variables the folders or folders (sourceFolder and destFolder) to minimize typing errors or pass this snippets to function.
  4. I run copy with the absolute paths.

Wondering what __DIR__ is? Is a global variable which returns the full path to the current directory. For example, if this PHP script is located on my server at /var/www/public_html/domain/script.phpthis variable will return the entire path to my “script.php”, ie: /var/www/public_html/domain/.

Leave a Reply