Upload Files With PHP Powered Server To Server File Copy Script

By | April 22, 2013
  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

One of the things that’s frustrating for webmasters who have slow upload speeds is definitely uploading files for new websites that they’re setting up. It’s one thing when the upload is small, but for large files, it can take some time. If the data that you’re uploading is already hosted somewhere online, on a web sever, you might not have go through the hassle of downloading and re-uploading the files again to your server.

<!--?php 
$source = 'http://www.example.com/file.zip';
$target = 'file.zip';
 
if (@copy($source, $target))
{
echo 'File copied';
}
else
{
echo 'Could not copy file';
}
?-->

What’s we’re aiming at is a PHP powered server to server file copy script. It’s very short and not very particularly difficult to figure out, this is it what you can see in the code snippet above. Hosting that you’re using has to support PHP in order to use this script. Transfer of files from standard hosting account, meaning from website hosts, should work just fine. It might even be possible to transfer files from file hosts, but don’t hold your hopes too high.



Best way to show how something works is with a video. What you see here is this handy little sever to server file copy script in action. We only changed two variables. First one, $source, tells the script where the file that we want copied and moved is located. For the second one, $target, we wrote a local path where the file’s gonna be saved, it can also be renamed if you’re interested in doing so. Here’s how the completed code looks like direct server to server file copy from WordPress website.

<!--?php 
$source = 'http://wordpress.org/latest.zip';
$target = 'Wordpress.zip';
 
if (@copy($source, $target))
{
echo 'File copied';
}
else
{
echo 'Could not copy file';
}
?-->

Now you just need to save this as a PHP file, call it upload.php for example. Save it somewhere in a folder on the server where you website is hosted and where you want to transfer the files.. You can use cPanel to create and edit the document or edit the on your computer and upload it via FTP, like we did in the video above. Open up the website and execute the server to server file copy script by opening it up in your web browser.

http://example.com/upload.php

This is how the address should look like, provided of course that you’ve named the PHP file upload.php. Uploading files this way is very fast, you should be able to transfer large amounts of data at once. Try it and see for yourself,

Leave a Reply