181

Say I've got a file test.php in foo directory as well as bar. How can I replace bar/test.php with foo/test.php using PHP? I'm on Windows XP, a cross platform solution would be great but windows preferred.

10 Answers 10

336

You could use the copy() function :

// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');

Quoting a couple of relevant sentences from its manual page :

Makes a copy of the file source to dest.

If the destination file already exists, it will be overwritten.

Sign up to request clarification or add additional context in comments.

3 Comments

Does copy( 'foo/test.php', 'bar/test.php' ) create the bar directory if it doesn't already exist?
No @henrywright, it do not create directories itself. You have to do it manually. check it on php manual
I didn't realize this was a thing... thanks for sharing.
24

You could use the rename() function :

rename('foo/test.php', 'bar/test.php');

This however will move the file not copy

3 Comments

I wonder why the named this function rename and note move or something
@themis I too wish they had named the function move. It would be intuitive if one has a little linux exposure.
@themis because rename('foo/test1.php', 'foo/test2.php'); ;)
14

copy will do this. Please check the php-manual. Simple Google search should answer your last two questions ;)

Comments

11

You can copy and paste this:

<?php
$file = '/test1/example.txt';
$newfile = '/test2/example.txt';
if(!copy($file,$newfile)){
    echo "failed to copy $file";
}
else{
    echo "copied $file into $newfile\n";
}
?>

Comments

7

Best way to copy all files from one folder to another using PHP

<?php
$src = "/home/www/example.com/source/folders/123456";  // source folder or file
$dest = "/home/www/example.com/test/123456";   // destination folder or file        

shell_exec("cp -r $src $dest");

echo "<H2>Copy files completed!</H2>"; //output when done
?>

1 Comment

Not a good example since most web servers have shell commands disabled for security reasons.
2

PHP's copy() function actually works only if the destination has some file to override. For example, you are copying a file A to file B, the copy() function will work something like following;

$fileA = "foo/fileA.txt";
$fileB = "bar/fileA.txt";
copy($fileA, $fileB);

So the copy() requires to have a file at the destination path and in fact destination path should include that file name too otherwise it will through error and will not work.

But if you do not have any file at the destination to override well than simply make a file with that name first and you will write something like this;

$source = "foo/fileA.txt";
$destination = "bar/"; // note how the destination has no file.
$newFile = "somefile.txt";
touch($destination . $newFile);
// then do the copy part
copy($source, $destination.$newFile);

I had the same problem and came to this solution of making a file at the destination for the copy function to override.

Comments

1

Hi guys wanted to also add on how to copy using a dynamic copying and pasting.

let say we don't know the actual folder the user will create but we know in that folder we need files to be copied to, to activate some function like delete, update, views etc.

you can use something like this... I used this code in one of the complex project which I am currently busy on. i just build it myself because all answers i got on the internet was giving me an error.

    $dirPath1 = "users/$uniqueID"; #creating main folder and where $uniqueID will be called by a database when a user login.
    $result = mkdir($dirPath1, 0755);
            $dirPath2 = "users/$uniqueID/profile"; #sub folder
            $result = mkdir($dirPath2, 0755);
                $dirPath3 = "users/$uniqueID/images"; #sub folder 
                $result = mkdir($dirPath3, 0755);
                    $dirPath4 = "users/$uniqueID/uploads";#sub folder
                    $result = mkdir($dirPath4, 0755);
                    @copy('blank/dashboard.php', 'users/'.$uniqueID.'/dashboard.php');#from blank folder to dynamic user created folder
                    @copy('blank/views.php', 'users/'.$uniqueID.'/views.php'); #from blank folder to dynamic user created folder
                    @copy('blank/upload.php', 'users/'.$uniqueID.'/upload.php'); #from blank folder to dynamic user created folder
                    @copy('blank/delete.php', 'users/'.$uniqueID.'/delete.php'); #from blank folder to dynamic user created folder

I think facebook or twitter uses something like this to build every new user dashboard dynamic....

Comments

1
<?php

$file = 'example.txt';
$newfile = 'example2.txt';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

You can use both rename() and copy().

I tend to prefer to use rename if I no longer require the source file to stay in its location.

Comments

0
<?php  
  
// Copy the file from /user/desktop/geek.txt  
// to user/Downloads/geeksforgeeks.txt' 
// directory 
  
// Store the path of source file 
$source = '/user/Desktop/geek.txt';  
  
// Store the path of destination file 
$destination = 'user/Downloads/geeksforgeeks.txt';  
  
// Copy the file from /user/desktop/geek.txt  
// to user/Downloads/geeksforgeeks.txt' 
// directory 
if( !copy($source, $destination) ) {  
    echo "File can't be copied! \n";  
}  
else {  
    echo "File has been copied! \n";  
}  
  
?>  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.