使用PHP将整个目录和内容从一个位置复制到另一个位置

时间:2012-08-03 05:39:30

标签: php directory copy

我正在尝试使用PHP将整个文件夹从一个位置复制到另一个位置,但它似乎不起作用:

$username = "peter" //this is just an example.
$userdir = "../Users/".$username."/";
mkdir($userdir);// create folder 

// copy image folder
$source = "templates/template1/images/";//copy image folder -source
$dest = $userdir;

function copyr($source, $dest){
// Simple copy for a file
if (is_file($source)) {
$c = copy($source, $dest);
chmod($dest, 0777);
return $c;
}

// Make destination directory
if (!is_dir($dest)) {
$oldumask = umask(0);
mkdir($dest, 0777);
umask($oldumask);
}

// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == "." || $entry == "..") {
continue;
}

}

// Clean up
$dir->close();
return true;
}

我也尝试过在线看到的其他解决方案但没有成功。希望得到任何可能的帮助


我也没试过这个剧本。

我刚尝试了另一个剧本,但仍然没有运气:(。

$template_homepage = "templates/template1/index.php";//path to default template homepage
$homepage = file_get_contents($template_homepage);//get default homepage structure


$username = testuser;// folder name for store


if (trim($username) == '') {
die("An error occured.");
} else {
$userdir = "../Users/".$username."/";
mkdir($userdir);// create folder for new website

// copy image folder
$src = 'templates/template1/images';//copy image folder -source
$dst = $userdir;

   function rcopy($src, $dst) {
  if (file_exists($dst)) rrmdir($dst);
  if (is_dir($src)) {
mkdir($dst);
$files = scandir($src);
foreach ($files as $file)
if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file"); 
}
  else if (file_exists($src)) copy($src, $dst);

}




$fh = fopen($userdir."index.php", 'w') or die("An error occured. ");// create home page in users folder

   // $stringData = $title; //."\n";//
   fwrite($fh, $homepage);// write homepage structure into new homepage file.
    fclose($fh);// close new homepage file.
 $launchpage = "../Users/".$username."/"; // launch new homepage file.
header("Location: $launchpage");
   }

1 个答案:

答案 0 :(得分:4)

为什么不使用exec并使用OS命令复制文件夹?

exec('cp -r sourcedir destdir');