Richard Downes
Richard Downes

Reputation: 87

Upload a folder via FTP using PHP

Our website currently backs up every night to a seperate server that we have which is fine, but when we go to dowload the files the next day it take's a long time to download the files (usually around 36,000+ images). Downloading this the following day takes quite some time and affects the speeds of everyone else using our network so ideally we would try and do this in the middle of the night - except there's no-one here to do it.

The server that the backup is on is running Cpanel which appears to make it fairly simple to run a PHP file as a Cron job.

I'm assuming the following, feel free to tell me I'm wrong.

1) The server the backup is on runs Cpanel. It appears that it shouldn't be too difficult to set up a PHP script to run as a Cron job in the middle of the night.

2) We could deploy a PHP script utilizing the FTP functions to connect to another server and start the backup of these files using this cron job.

3) We are running Xampp on a windows platform. It has Filezilla as part of it so I'm assuming it should be able to accept incoming FTP connections.

4) Overall - we could deploy a script on the backup server that would run every night and send the files back to my local computer running Xampp.

So that's what I'm guessing. I'm getting stuck at the first hurdle though. I've tried to create a script that runs on our local computer and sends a specified folder to the backup server when it executes, but all I seem to be able to find is scripts relating to single files. Although I've some experience of PHP, I haven't touched upon the FTP functions before which are giving me some problems. I've tried the other examples here on stack overflow with no success :(

I'm just looking for the most simplistic form of a script that can transfer upload a folder to a remote IP. Any help would be appreciated.

Upvotes: 1

Views: 862

Answers (1)

MrGlass
MrGlass

Reputation: 9252

There is a fair amount of overhead involved in transferring a bunch of small files over FTP. Ive seen jobs take 5x as long, over a local network. It is by far easier to pack the files in something like a zip and send them in one large file.

you can use exec() to run zip from the command line (or whatever compression tool you prefer). After that, you can send it over ftp pretty quickly (you said you found methods for transferring 1 file). For backup purposes, having the files zipped would probably makes things easier to handle, but if you need them unzipped you can setup a job on the other machine to unpack the file.

Upvotes: 1

Related Questions