Use PHP to connect to FTP and backup all folder contents - php

I'm building a custom web app that stores the FTP and MySQL settings for the websites I manage for clients. My goal is not only to store the settings for reference, but to create functionality to assist in doing regular backups.
I've got the MySQL backup functionality working great, as it connects to the remote databases, creates a dump and sends it to my browser to download locally.
BUT... what is the best way to connect to a remote FTP and download all the contents of a specific folder to my local computer?
Any suggestions would be amazing!

This example from the php manual posted by mroerick might help you.
<?php
$ftp_server = "ftp.example.com";
$conn_id = ftp_connect ($ftp_server)
or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, "user", "pass");
if ((!$conn_id) || (!$login_result))
die("FTP Connection Failed");
ftp_sync (".");
ftp_close($conn_id);
function ftp_sync ($dir) {
global $conn_id;
if ($dir != ".") {
if (ftp_chdir($conn_id, $dir) == false) {
echo ("Change Dir Failed: $dir<BR>\r\n");
return;
}
if (!(is_dir($dir)))
mkdir($dir);
chdir ($dir);
}
$contents = ftp_nlist($conn_id, ".");
foreach ($contents as $file) {
if ($file == '.' || $file == '..')
continue;
if (#ftp_chdir($conn_id, $file)) {
ftp_chdir ($conn_id, "..");
ftp_sync ($file);
}
else
ftp_get($conn_id, $file, $file, FTP_BINARY);
}
ftp_chdir ($conn_id, "..");
chdir ("..");
}
?>

Related

How does one move files to a specific folder after an FTP download using PHP?

Is there a way to move files to a specific folder or declare a specific folder to download files by using FTP methods in PHP?
In short, I am downloading 10,000+ files and I want them to go into a certain folder that I already created. I am using an FTP connection to download the files from my script and I am looping through every file in the FTP server. They all download (and it takes a hell of a long time) - I just need to declare a specific path or move the files to the folder.
Here is the code:
function ftp_sync($dir, $conn_id){
if($dir !== '.'){
if(ftp_chdir($conn_id, $dir) === FALSE){
echo 'Change directory failed: ' . $dir . PHP_EOL;
return;
}
chdir($dir);
}
$contents = ftp_nlist($conn_id, '.');
foreach($contents as $file){
if($file == '.' || $file == '..'){
continue;
}
if(#ftp_chdir($conn_id, $file)){
ftp_chdir($conn_id, "..");
ftp_sync($file, $conn_id);
} else {
ftp_get($conn_id, $file, $file, FTP_BINARY);
//TODO: Download the files into a specific directory
}
}
ftp_chdir($conn_id, '..');
chdir('..');
}
$ftp_server = 'server';
$user = 'user';
$password = 'password';
$document_root = '/';
$sync_path = 'Web_Images';
$conn_id = ftp_connect($ftp_server);
if ($conn_id) {
$login_result = ftp_login($conn_id, $user, $password);
ftp_pasv($conn_id, true);
if ($login_result) {
ftp_chdir($conn_id, $document_root);
ftp_sync($sync_path, $conn_id);
ftp_close($conn_id);
} else {
echo 'login to server failed!' . PHP_EOL;
}
} else {
echo 'connection to server failed!';
}
echo 'done.' . PHP_EOL;
The ftp_get($conn_id, $file, $file, FTP_BINARY); should be able to place your remote file anywhere you want as a default, you just have to indicate that spot in the local parameter:
# Where ever you want to download local files to
$dir = __DIR__.'/my/specific/path/';
# See if directory exists, create if not
if(!is_dir($dir))
mkdir($dir,0755,true);
# Saves the file(s) into the $dir folder with the same name as the remote file
ftp_get($conn_id, $dir.$file, $file, FTP_BINARY);

Automatic uploading a local directory all files to ftp server using php

I want to upload the directory content which includes only text files to a ftp server and it should be automated through windows task schedular. Right now I'm using this but its only uploading one file, My requirement is basically a user can copy its files to a specific folder and it should be uploaded on ftp server asap.
this is the code I'm using right now:
$host = 'ftp.xyz.com';
$usr = 'abc123';
$pwd = 'l2345';
date_default_timezone_set('Asia/Kuala_Lumpur');
$date = date('d-m-Y_hi');
$name = $date.".txt";
$local_file = '/Applications/XAMPP/xamppfiles/htdocs/Conversion/Upload/xyz/abc.txt';
$ftp_path = '/public_html/xyz/abc.txt';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_BINARY);
echo (!$upload) ? 'Cannot upload' : 'Upload complete';
echo "\n";
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
echo $ftp_path . " chmoded successfully to 666\n";
} else {
echo "could not chmod $file\n";
}
ftp_close($conn_id);
To upload multiple files, you'll want to either issue a series of FTP (connections and) transfers with a loop in your code...or use a wildcard in FTP's interactive mode.

FTP folder to local using PHP

I want to make a copy of ftp in a folder of my project(localhost) so I need to specify source_folder and dest_folder. My problem is that my code just save all files in my project's root and I would like to copy my ftp folder "/" into my "./copy_data" folder in my local project.
Here is my code:
function ftp_sync ($dir) {
if ($dir != ".") {
if (ftp_chdir($this->conn, $dir) == false) {
echo ("Change Dir Failed: $dir<BR>\r\n");
return;
}
if (!(is_dir($dir)))
mkdir($dir);
chdir ($dir);
}
$contents = ftp_nlist($this->conn, ".");
foreach ($contents as $file) {
if ($file == '.' || $file == '..')
continue;
if (#ftp_chdir($this->conn, $file)) {
ftp_chdir ($this->conn, "..");
$this->ftp_sync ($file);
}
else{
//ftp_get($this->conn, $file, $file, FTP_BINARY);
ftp_get($this->conn, $file, $file, FTP_BINARY);
}
}
ftp_chdir ($this->conn, "..");
chdir ("..");
}
ftp_sync('.');
Could anyone help me?
You can execute bash command from php to zip, copy and move the target folder is my recommendation
$message=shell_exec("/var/www/scripts/testscript 2>&1");
print_r($message);

Save FTP files in JSON with PHP

I'm trying to make an array of all my ftp files. I thought that would be easy. However, I want to have an array like this:
files = [
"index.html":"_content from index.html_"
"somedir/things/server.txt":"_content from server.txt_"
"process/server.php":"_content from process.php_"
]
Is there any way to do this?
After I have this, I would like to put this array in a JSON file, that doesn't seem so hard. However, then I have things like images, which can't be saved in a JSON file right? So, I want them to download to my hard drive. How do I do that?
Thanks in advance...
UPDATE 1
This is what I just found:
<?php
$ftp_server = "ftp.example.com";
$conn_id = ftp_connect ($ftp_server)
or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, "user", "pass");
if ((!$conn_id) || (!$login_result))
die("FTP Connection Failed");
ftp_sync ("."); // Use "." if you are in the current directory
ftp_close($conn_id);
// ftp_sync - Copy directory and file structure
function ftp_sync ($dir) {
global $conn_id;
if ($dir != ".") {
if (ftp_chdir($conn_id, $dir) == false) {
echo ("Change Dir Failed: $dir<BR>\r\n");
return;
}
if (!(is_dir($dir)))
mkdir($dir);
chdir ($dir);
}
$contents = ftp_nlist($conn_id, ".");
foreach ($contents as $file) {
if ($file == '.' || $file == '..')
continue;
if (#ftp_chdir($conn_id, $file)) {
ftp_chdir ($conn_id, "..");
ftp_sync ($file);
}
else
ftp_get($conn_id, $file, $file, FTP_BINARY);
}
ftp_chdir ($conn_id, "..");
chdir ("..");
}
?>
I want to do something like this, but instead of getting the whole file, I want to get the path to the file and the content and save them in an array.

PHP Sync over ftp giving errors

So I got this code that's suppose to grab all the contents from /upload/ folder on remote host and copy it into the location where the file is at. Code looks like this:
<?php
$ftp_server = "server";
$conn_id = ftp_connect ($ftp_server)
or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, "user", "password");
if ((!$conn_id) || (!$login_result))
die("FTP Connection Failed");
ftp_sync ("/update/");
ftp_close($conn_id);
function ftp_sync ($dir) {
global $conn_id;
if ($dir != ".") {
if (ftp_chdir($conn_id, $dir) == false) {
echo ("Change Dir Failed: $dir<BR>\r\n");
return;
}
if (!(is_dir($dir)))
mkdir($dir);
chdir ($dir);
}
$contents = ftp_nlist($conn_id, ".");
foreach ($contents as $file) {
if ($file == '.' || $file == '..')
continue;
if (#ftp_chdir($conn_id, $file)) {
ftp_chdir ($conn_id, "..");
ftp_sync ($file);
}
else
ftp_get($conn_id, $file, $file, FTP_BINARY);
}
ftp_chdir ($conn_id, "..");
chdir ("..");
}
?>
When I run it in an empty folder it does what it's suppose to. However, if I run it in a folder that contains the files from /update/ it will give me two errors. The errors are listed below
Warning: mkdir() [function.mkdir]: Permission denied in /home/limited/public_html/download.php on line 25
Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /home/limited/public_html/download.php on line 26
It's suppose to just overwrite the files with new ones, not throw errors.
Thank you
Either the script (actually the user running the script) has no permissions to create new folders on the server OR the folder where you are trying to copy the files and create new folders is restricted from your user.
Try with a user that has higher permissions or add write permissions to the folder where you want to sync the files into

Categories