I'm trying to get a "reset" script working in PHP to essentially just copy all files/folders from one specific directory to another.
I've found plenty of snippets of code on Stackoverflow for recursively copying files/folders but I cannot get any of them to work.
My directory essentially looks like:
/public_html/folder1/reset.php
/public_html/folder1/files/
/public_html/docs/reset/files/
The reset.php file is the file I am trying to get working and I want to copy all of the files from /docs/reset/files/ over to /folder1/files/ overwriting everything...
I've tried the following code but it generates a 500mb~ file full of errors
<?php
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
recurse_copy("../docs/reset/", "/")
?>
The code does appear to be cycling through correctly but the paths are all wrong.
[18-Jun-2020 07:54:33 UTC] PHP Warning: copy(/files//files/copies/index.php): failed to open stream: No such file or directory in /home/USERNAME/public_html/folder1/reset.php on line 11
If I could get the above working I was going to try the following:
$src = '/full/path/to/src'; // or relative path if so desired
$dst = '/full/path/to/dst'; // or relative path if so desired
$command = 'cp -a ' . $src . ' ' .$dst;
$shell_result_output = shell_exec(escapeshellcmd($command));
As I assume that would be faster?
I must be doing something wrong and I'm sure it's going to be something really minor but I cannot figure out what I am doing wrong.
Related
I have a some template files organised like this in a directory:
index.php
preferences.xml
user.xml
I have also have subdirectory of this directory that contains a large number of folders (the exact number of folders can change a lot) which have outdated versions of the above template files:
randomfolder1
randomfolder20
randomfolder29
...
I would like to be able to copy the template files into all of these folders in the subdirectory, regardless of how many directories or template files there are. This is an example of what I would like in every folder:
randomfolder
- index.php
- preferences.xml
- user.xml
I have used this to copy the template files into ONE directory, but not all of them:
$cdire = 'GMXD/Users/firstdirectory';
mkdir($cdire);
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
recurse_copy('template',$cdire);
So how can you make this script copy all files in the template directory to all files in the subdirectory ?
The purpose of this question can be served by writing independent function for each source & destination directory in an include file but I'm looking for a better approach.
The following function copy files from one source directory to one destination directory.
How can I use this function to copy file from another source directory to destination directory?
Is array(); applicable here or explode(); shall be the right choice or none of these is applicable in this case?
if (isset($_POST['submit'])) {
$old_umask = umask(0);
if (!is_dir($dst)) mkdir($dst, 0777);
umask($old_umask);
function recurse_copy($src,$dst) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
//echo "$src";
}
$dir = $_POST['name'];
$src = "/home/user/public_html/directory/subdirectory/source/";
$dst = "/home/user/public_html/directory/subdirectory/destination/$dir/";
recurse_copy($src,$dst);
}
I have a bunch of files in a directory that I would like to rename. I have a complete list of existing file names and in the column next to the old name, I have a new name (desired) filename, like below: (the list is in excel so I can apply some syntax to all the rows very easily)
OLD NAME NEW NAME
-------- --------
aslkdjal.pdf asdlkjkl.pdf
adkjlkjk.pdf asdlkjdj.pdf
I would like to keep the old name and old files in their current directory and not disturb them, but just create a copy of the file, with the new filename instead.
Not sure what language to use and how to go about doing this.
http://php.net/manual/en/function.rename.php
<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>
EDIT: in case of copy -
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
Something like this should work:
$source = '/files/folder';
$target = '/files/newFolder';
$newnames= array(
"oldfilename" => "newfilename",
"oldfilename1" => "newfilename1",
);
// Copy all files to a new dir
if (!copy($source, $target)) {
echo "failed to copy $source...\n";
}
// Iterate through this dir, rename all files.
$i = new RecursiveDirectoryIterator($target);
foreach (new RecursiveIteratorIterator($i) as $filename => $file) {
rename($filename, $newnames[$filename]);
// You might need to use $file as first parameter, here. Haven't tested the code.
}
RecursiveDirectoryIterator documentation.
Just try with the following example :
<?php
$source = '../_documents/fees';
$target = '../_documents/aifs';
$newnames= array(
"1276.aif.pdf" => "aif.10001.pdf",
"64.aif.20091127.pdf" => "aif.10002.pdf",
);
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
// Copy all files to a new dir
recurse_copy($source, $target);
// Iterate through this dir, rename all files.
$i = new RecursiveDirectoryIterator($target);
foreach (new RecursiveIteratorIterator($i) as $filename => $file) {
#rename($filename, $target.'/'.$newnames[''.$i.'']);
}
?>
This is pretty easy to do with a shell script. Start with the file list as you presented in files.txt.
#!/bin/sh
# Set the 'line' delimiter to a newline
IFS="
"
# Go through each line of files.txt and use it to call the copy command
for line in `cat files.txt`; do
cp `echo $line | awk '{print $1;}'` `echo $line | awk '{print $2};'`;
done
I have a question regarding file handle, i have:
Files:
"Mark, 123456, HTCOM.pdf"
"John, 409721, JESOA.pdf
Folders:
"Mark, 123456"
"Mark, 345212"
"Mark, 645352"
"John, 409721"
"John, 235212"
"John, 124554"
I need a routine to move files to correct folders.
In case above, i need to compare 1st and second value from file and folder. If are the same i move the file.
Complement to post:
I have this code, work right but i need to modify to check name and code to move files...
I'm confused to implement function...
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
foreach (glob($pathToFiles . DIRECTORY_SEPARATOR . '*.pdf') as $oldname)
{
if (is_dir($dir = $pathToDirs . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_FILENAME)))
{
$newname = $dir . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_BASENAME);
rename($oldname, $newname);
}
}
As a rough-draft and something that will only work with your specific case (or any other case following the same naming pattern), this should work:
<?php
// define a more convenient variable for the separator
define('DS', DIRECTORY_SEPARATOR);
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
// get a list of all .pdf files we're looking for
$files = glob($pathToFiles . DS . '*.pdf');
foreach ($files as $origPath) {
// get the name of the file from the current path and remove any trailing slashes
$file = trim(substr($origPath, strrpos($origPath, DS)), DS);
// get the folder-name from the filename, following the pattern "(Name, Number), word.pdf"
$folder = substr($file, 0, strrpos($file, ','));
// if a folder exists matching this file, move this file to that folder!
if (is_dir($pathToDirs . DS . $folder)) {
$newPath = $pathToDirs . DS . $folder . DS . $file;
rename($origPath, $newPath);
}
}
I found this question (here) about copying a file without overwriting
How do you copy a file in PHP without overwriting an existing file?
What I need is a php script to copy all files in a folder, there are also some subfolders; so it should be recursive.
I need to transfer it via FTP so I don't know this makes a big difference to the approach.
Thanks a lot in advance!
Try this, from a comment on the manual page for copy:
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
Note that this solution will happily overwrite any files that exist in the $dst directory. If you want to avoid that, you could wrap the code in this question into a function, and call that function instead of copy.
I'm not sure what you want to transfer via FTP, if you clarify that I'll be happy to edit my answer.
There is a big difference between filesystem copy and FTP copy you could take a look at the PEAR FTP Class