简体   繁体   中英

copy one directory to another in php

       <?php
         extract($_REQUEST);
        if(isset($_POST['submit']))
         {
          $get_folder = $_POST['url'];
       $q = mysql_query("insert into test (url) values ('$url')");
        if($q)
        {
        copydir("test",$get_folder);
      function copydir($source,$destination)
       { 
       if(!is_dir($destination))
       {
       $oldumask = umask(0); 
      mkdir($destination, 01777); 
      umask($oldumask);
       }
      $dir_handle = @opendir($source) or die("Unable to open");
     while ($file = readdir($dir_handle)) 
      {
        if($file!="." && $file!=".." && !is_dir("$source/$file")) //if it is file
      copy("$source/$file","$destination/$file");
      if($file!="." && $file!=".." && is_dir("$source/$file")) //if it is folder
      copydir("$source/$file","$destination/$file");
      }
      closedir($dir_handle);
       }
        }
        }
       ?>

this is my code ...it shows Fatal error: Call to undefined function copydir() in C:\\xampp\\htdocs\\mywork\\creating-folder\\1.php on line 14. But when i copy from copydir("test",$get_folder); to closedir($dir_handle); in separate file it works perfectly but instead of $get_folder need to give some static name

Use copy() .

Note that this function does support directories out of the box. A function from one of the comments on the linked documentation page might help:

<?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); 
} 
?>
  // Will copy foo/test.php to bar/test.php
  // overwritting it if necessary
  copy('foo/test.php', 'bar/test.php');

This works:

foo();

function foo() { ... }

This won't:

if (...) {

    foo();

    function foo() { ... }

}

This will:

if (...) {

    function foo() { ... }


    foo();

}

In general, you're required to declare the function before you call it. The exception is with plain, globally defined functions as in the first example; those are being handled right in the parsing step before execution. Since your function declaration is inside an if statement and thereby conditional, the if condition and thereby the whole code need to be evaluated first. And while the code is being evaluated, you're trying to call a function which hasn't been declared yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM