0

I am a bit familiar with PHP, although I am trying to work with a PHP script that I haven't really done before:

$src = "/home/user/public_html/test1";
$dest = "/home/user/public_html/test2";
shell_exec("cp -r $src $dest");
echo "Directory successfully created."; //output when done
rename("$dest","uniqid();");

This script is supposed to copy the files of a directory so that the newly created directory will be /home/user/public_html/test2/test1.

However, because this script is supposed to be used dynamically, I would like the rename function to rename the $dest to a uniqid(); function.

Can anyone compile a script for me that can do this or provide instructions on how to??

Thanks.

  • 1
    PHP has built-in functions to do this. No need to access the shell. It's one quick search in the docs ;) –  Dec 31 '13 at 00:32
  • You don't have to copy and then rename, just copy and in the destination use the directory you want it to be in. – Mike Dec 31 '13 at 00:37

2 Answers2

0

Not sure I get the idea here, but you should change this rename("$dest","uniqid();"); to rename($dest,uniqid());. Get rid of the quotes and semicolon.

josephtikva1
  • 789
  • 5
  • 11
0

Here you go :)

$info = array();
$info['src'] = "/home/user/public_html/test1";
$info['dest'] = "/home/user/public_html/".uniqid();

// another thought for dest name if you want to easily sort it and guarantee unique-ness
// $info ['dest'] = "/home/user/public_html/".uniqid(time().'.'.microtime(true).'.', true);

// make sure directory doesn't exist or else overwriting will occur
$info['dir_test'] = is_dir($dest);
if(!$info['dir_test'])
{
    $info['copy_result'] = copy($info['src'], $info['dest']);
    if($info['copy_result'])
    {
        $info['text_result'] = 'copy success :)';
    }
    else
    {
        $info['text_result'] = 'copy failed :(';
    }
}
else
{
    $info['text_result'] = 'destination already exists! - '.$destination;
}

echo '<pre>'.print_r($info, true).'</pre>';
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77