• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

Create Directory and copy files php code needed

Hi all,

I need so php code that will check if a folder exists on a specific path and if not then create the folder and copy some specified files / folders into it.

Can anyone help please?

Thanks
0
error77
Asked:
error77
  • 5
  • 2
  • 2
  • +1
2 Solutions
 
DerokorianCommented:
Relevant manual pages:
http://us.php.net/copy
http://us.php.net/is_dir
http://us.php.net/mk_dir
http://us.php.net/basename
http://us.php.net/glob

example:
<?php

$source = 'c:\test';
$dest = 'c:\newtest';

copyFiles($source,$dest);

function copyFiles($source,$dest) {
   if( !is_dir($dest) ) {
      mkdir($dest,'0777');
      echo 'creating dir '. $dest .'<br>';
   }

   foreach( glob($source . DIRECTORY_SEPARATOR . '*') as $file ) {
      $path = $dest . DIRECTORY_SEPARATOR . basename($file);
      if( is_file($file) ) {
         copy($file,$path);
         echo $file .' -> '. $path . '<br>';
      } else {
         copyFiles($file,$path);
      }
   }
}

Open in new window


HTH
0
 
Marco GasiFreelancerCommented:
Try this:

<?php
function copyRecursive($source, $destination) {
    if (!file_exists($source)) { die("'$source' is not valid"); }
    if (!is_dir($destination)) {
        mkdir ($destination);
    }
    $dh = opendir($source) or die ("Cannot open directory '$source'");

    while (($file = readdir($dh)) !== false) {
        if ($file != "." && $file != "..") {
            if (is_dir("$source/$file")) {
                copyRecursive("$source/$file", "$destination/$file");
            } else {
                copy ("$source/$file", "$destination/$file") or die ("Cannot copy file '$file'");
            }
        }
    }
    closedir($dh);
}

copyRecursive("www/template", "www/site12");
echo "Directories successfully copied.";
?>

Open in new window


Cheers
0
 
error77Author Commented:
OK, trying the code but nothing happening...

Could be my code though...

Trying:

copyRecursive("/_includes/folderContentsToCopy", "/myDest/".$user_id.'/');

Note: The destination folder does not exist so it should be creating it.

Any ideas why it might not be doing anything please?

THanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
Maybe a file or directory permissions issue?  Most of the PHP functions return a value like TRUE or FALSE (see http://php.net/mk_dir and look on that page for Return Values) and you might want to test the return values with an if statement.  Example:

if (!is_dir($destination))
{
    if (!mkdir ($destination)
    {
        die("DISASTER: mkdir($destination) FAILED");
    }
}
0
 
error77Author Commented:
The directory has cmod 777 so it is writable. I thought it was that too.
0
 
error77Author Commented:
OK, I'm getting an error now:

From here:

copyRecursive("/_includes/folderContentsToCopy", "/myDest/".$user_id.'/');

It's telling me:

/_includes/folderContentsToCopy is not valid. :o/ The Folder DOES exist though.

0
 
error77Author Commented:
Note: folderContentsToCopy - is the folder and inside is the stuff I want to copy (not the folder but the inside of the folder)

0
 
DerokorianCommented:
leading slash does not mean relative to the current file. Maybe you mean to use ./ (dot slash)
0
 
error77Author Commented:
I've added a backslash to get to the root directory
0
 
Ray PaseurCommented:
leading slash ... backslash

PHP has a predefined constant that is OS-context aware.  DIRECTORY_SEPARATOR should be used instead of any kind of slash character.  It will always be right.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now