Jump to content

php move a file to a folder


Lakisman

Recommended Posts

hello,

 

Im trying to write a script to use in my website cpanel, that moves the backup files created from crons to a spesific folder.

Basically i want to move every file that its name starts with "backup" from the home/ directory to the home/backups directory.

 

I tried to do something but it didnt work

 

<?php

 

    // create a handler for the directory

    $handler = opendir($directory);

 

    // keep going until all files in directory have been read

 

    while ($file = readdir($handler)) {

 

        if (strpos($file,'backup')){

    rename(`/home/$file`, `/home/backups/$file`);

 

        }

        else{   

      //  dont know

        }

    }

 

    closedir($handler);

 

?>

 

Can someone offer a beeter solution or help me fix this.

 

Thank you very much

 

 

Link to comment
https://forums.phpfreaks.com/topic/136200-php-move-a-file-to-a-folder/
Share on other sites

For starters your using `backticks` in this line.....

 

rename(`/home/$file`, `/home/backups/$file`);

 

instead of quotes.

 

Secondly, why isn't this being taken care of by the backup script itself?

 

And thirdly, a better solution using glob.

 

foreach (glob("/home/backup*") as $file) {
  rename("/home/$file", "/home/backups/$file");
}

Hello,

 

First thank you very much for the reply.

 

The backup script below i found it from another blog and its working but it create the backup files in to the home directory, thats why i tried to use the other script, so i can move the files to the spesific directory.

 

<?php


$cpuser = "username"; // Username used to login to CPanel
$cppass = "password"; // Password used to login to CPanel
$domain = "domain"; // Domain name where CPanel is run
$skin = "x3"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for FTP host
$ftpuser = ""; // Username for FTP account
$ftppass = ""; // Password for FTP account
$ftphost = ""; // Full hostname or IP address for FTP host
$ftpmode = ""; // FTP mode ("ftp" for active, "passiveftp" for passive)

// Notification information
$notifyemail = ""; // Email address to send results

// Secure or non-secure mode
$secure = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 1;



//system(`rm -f /home/web_backups/backup-*`);

// *********** Don't Touch!! *********

if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;
}

$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection... Bailing out!\n"; exit; }

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
  $response = fgets($socket,4096);
  if ($debug) echo $response;
}

fclose($socket);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.