skip to Main Content

We recently upgraded our server to cPanel 78 and migrated from EA3 to EA4. Our server only has two sites on it, and prior to the upgrade we could use PHP scripts to copy files between the two sites using PHP’s file_exists() and copy() functions.

We could use the file_exists() function to grab files from site1 and migrate them to site2 using code similar to this:

$current_path = '/home/site1/public_html/uploads';
$new_path = '/home/site2/public_html/uploads';

if(file_exists($current_path.'/v2n62l6v.jpg')) {
    echo 'File exists: true' . "nn";
    copy($current_path.'/v2n62l6v.jpg', $new_path.'/2020/03/30/v2n62l6v.jpg');
} else {
    echo 'File exists: false' . "nn";
}

This code also creates new directories and sets the permissions to 0755.

After the upgrade, when we attempt to execute the script, we are greeted with this error:

File exists: true
Warning: copy(/home/site2/public_html/uploads/2020/03/30/v2n62l6v.jpg): failed to open stream: Permission denied in /home/site2/public_html/move.php on line 15

We are able to move the files if we set the permissions to the folders to 0777, but I would prefer to not have to change all of the folder permissions (there are 10s of thousands).

Any ideas on where to start or what settings may have changed during the upgrade to either EA or cPanel/WHM?

Site is using:

PHP 5.5 (ea-php55)
DSO PHP Handler
CENTOS 6.10
cPanel v78.0.47

I am happy to provide any other information to help trouble shoot this issue.

Thanks so much for any/all help.

2

Answers


  1. As you said that it is CentOS so the following should work

    <?php
    $current_path = '/home/site1/public_html/uploads';
    $new_path = '/home/site2/public_html/uploads';
    //Below command will copy paste all folders recursively
    shell_exec("cp -r $current_path $new_path");
    echo "All Done";
    ?>
    

    If the above one doesn’t works for some reasons then try going to the SSH terminal directly and run that shell command with your paths.

    As you mentioned in comments that you want to further process the files that could be done on site2 itself once all files are there you could scandir/is_dir to scan and process files. But that is different topic.

    Login or Signup to reply.
  2. If you really need to do this you have to make sure php user owns both sites files – apparently this is not your case.

    Alternatively you can use root user to copy files preserving ownership and permission or changing their owner and permission at the destination accordingly.

    One other option is to umask user privileges before your operation; Is strongly recommended to revert umask value after operation is completed:

    https://www.php.net/manual/en/function.umask.php

    Ex:

    $old = umask(0);
    //your operation here
    umask($old);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search