Answer the question
In order to leave comments, you need to log in
Copying a file from one directory on the server to another?
Hello.
Tell me a ready-made script for copying a file on the server from directory No. 1 to directory No. 2 with a check for the existence of such a file in directory No. 2, if such a file already exists, then you need to rename the copy, for example, add some prefix to the name and copy. The condition must be met until there is no match for the file name in directory #2. That is, the original file can be file.xml, and in folder No. 2 it can already be like this: file.xml, file_0.xml, file_1.xml, file_2.xml, and so on.
Thanks in advance.
Answer the question
In order to leave comments, you need to log in
Simple, not the most beautiful option
$file2 = $file = "1.xml";
$copy_to = "/var/www/";
$copy_from = "/var/xxx/";
$i=0;
while(file_exist($copy_to.$file2)){
$file2 = basename($path, ".xml") . "_" . ($i++) . ".xml";
}
copy($copy_from.$file1, $copy_to.$file2);
Ready script with three lines?
file_exists() for checking and copy()
If you are already looking for ready-made, then look at programs for creating backups or archives. With twists.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question