0

我正在尝试使用 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");
   }
4

1 回答 1

4

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

exec('cp -r sourcedir destdir');
于 2012-08-03T05:41:38.567 回答