How to copy a file from one directory to another using PHP?

后端 未结 8 881
暗喜
暗喜 2020-11-28 06:38

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

相关标签:
8条回答
  • 2020-11-28 07:26
    <?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";  
    }  
      
    ?>  
    
    0 讨论(0)
  • 2020-11-28 07:30

    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....

    0 讨论(0)
提交回复
热议问题