0

I'm working on a script which will upload all the zpanel backups to my Amazon S3 account. It works if I don't use the zpanel backups and create and upload backups created by me using CronJob.

The reason why I cannot upload the zpanel backup is I cannot figure out a way to reach the "backups" directory using php opendir function.

I know the absolute path of the backups folder though. And as it seems, this absolute path won't work with opendir()

I've used : opendir('var/zpanel/hostdata/my_username/backups/')

And it won't work. Now if I want to use the relative path, I cannot reach there either.

So, is there a way that I can move the zPanel Backups directory somewhere inside the public_html folder? Or, a web URL that can reach the backups folder? Ajaxplorer can reach there, why I cannot?

If none is possible, I'll greatly appreciate if someone can teach me a way to create backups exactly like zPanel (all DB + everything inside public_html folder).

The code looks like this

require_once('S3.php');

// Enter your amazon s3 creadentials
$s3 = new S3('xxxxx', 'xxxxx');

if ($handle = opendir('var/zpanel/hostdata/my_username/backups/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            //echo "okay";
            echo $file; exit;

            if ($s3->putObjectFile("backups/$file", "my_basket", "$file", S3::ACL_PUBLIC_READ)) {
                    echo "<strong>We successfully uploaded your file.<br /></strong>";
                    //this will delete the file from your server after upload
                    //if (file_exists($baseurl . '/' . $file)) { unlink ($baseurl . '/' . $file); }
            }else{
                echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
            }

        }else{
            echo "No file found here ".$root;
        }
    }
    closedir($handle);
}

Thanks a lot in advance!

Michal
  • 15,429
  • 10
  • 73
  • 104
tintinboss
  • 672
  • 1
  • 7
  • 25

1 Answers1

0

You have a typo in your code. If you want to use an absolute path with opendir, make sure it starts with a slash (/). So the correct version would be:

opendir('/var/zpanel/hostdata/my_username/backups/')

If you still can't get this to work, verify that the path you pass to opendir is indeed a directory and also verify the permissions on that directory to make sure your PHP process can read from it.

From the PHP doc,

If path is not a valid directory or the directory can not be opened due to permission restrictions or filesystem errors, opendir() returns FALSE ...

Also, if you use a any open-basedir restrictions in your php.ini or in your code, make sure the path you pass to opendir is not blocked by those restrictions.

dcro
  • 13,294
  • 4
  • 66
  • 75