My attempt at a recursive move function, after days of research and going through other excellent examples out there.
It provides for an overwriteExisting
flag for choice. Consequently, if the overwriteExisting
flag is false
, the file will not be moved and the folder containing the file would not be removed.
function moveRecursive[$sourcePath, $targetPath, $overwriteExisting] {
clearstatcache[]; // not sure if this helps, or is even required.
$dir = opendir[$sourcePath];
while [[$file = readdir[$dir]] !== false] {
echo nl2br[$file . "\n"];
if [$file != "." && $file != ".."] {
if [is_dir[$sourcePath . "/" . $file] == true] {
if [is_dir[$targetPath. "/" . $file] == false] {
// I believe rename would be faster than copying and unlinking.
rename[$sourcePath . "/" . $file, $targetPath. "/" . $file];
} else {
moveRecursive[$sourcePath . "/" . $file, $targetPath ."/" . $file, $overwriteExisting];
if [$files = glob[$sourcePath . "/*"]] {
// remove the empty directory.
if [@rmdir[$sourcePath . "/" . $file] == false] {
echo nl2br["rmdir has not removed empty directory " . $sourcePath . "/" . $file . "\n"];
}
} else {
// when overwriteExisting flag is false, there would be some files leftover.
echo nl2br["cannot remove. not empty, count = " . count[glob[$sourcePath . "/*"]] . " -> " . $sourcePath . "/" . $file . "\n"];
}
}
} else {
if [file_exists[$targetPath. "/" . $file]] {
if [$overwriteExisting == true] {
// overwrite the file.
rename[$sourcePath . "/" . $file, $targetPath. "/" . $file];
}
} else {
// if the target file does not exist, simply move the file.
rename[$sourcePath . "/" . $file, $targetPath. "/" . $file];
}
}
}
}
closedir[$dir];
}
I have spent about 3 hours testing this in many different scenarios, and it works most of the time. Although, sometimes, it gives me an Access denied code[5]
error on Windows,
which I have been unable to figure out. This is why I have put the clearstatcache[]
function up at the top, after reading up on its documentation. I don't know whether this is its appropriate usage. I can definitely imagine that it would slow down the function.
I can also imagine that this method may be faster than the copy -> unlink
cycle, because if a target sub-folder does not exist, the whole folder tree below would be just moved. However, I am not sure and don't have the experience to conduct
exhaustive tests, yet.
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:
The explanation of the above-mentioned code is as follows.
- The current path of the file is specified first using the variable
$currentLocation
. - Then, the new location along with the filename is specified. In simpler words, it is the new location where we wish to move our file to.
- If you wish to give your file a different name, it can be done by changing
the filename
move-test.txt
in the variable$newLocation
with your new filename. - The rename function takes two parameters:
$currentLocation
for the file path before moving operation and$newLocation
for the new file path after successfully moving it to the desired location.
Important Considerations
The rename
function returns TRUE
, if the move operation is successful. Two important things to note here
are:
- The file to be moved must exist. You should make sure that the file you are trying to move must exist. Otherwise, it will throw a warning stating:
The system cannot find the file specified.
- An existing file can be overwritten. The rename function can result in the overwriting of an existing file, which means if
$newLocation
is the name of some existing file, then after the move operation with rename function, the existing file will be overwritten.
to Check if File Exists
In order to check whether the file exists or not before moving it to another folder, you can use the is_file
function provided by PHP.
The code example to check if the file exists or not using the is_file
function is as follows:
In the above example, is_file[$currentLocation]
is used to check whether the file we wish to move exists or not before calling rename function that performs the move operation.
to Check if File Is Not Overwritten
The method to ensure that an existing file is not overwritten while moving a file from one location to another is explained in the following code example:
In the above example, is_file[$newLocation]
is used to check if another file exists that could be overwritten when the rename
function is called.
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.