Keep your options open with FTP file uploads - Web Development - Techguide

Keep your options open with FTP file uploads

 

Summary

A look at FTP-based file uploads using the PHP-FTP protocol to upload in a two-step process.

Events

Echelon 2012
June 11 and 12, 2012

University Cultural Centre, National University of Singapore

Startup Asia Jakarta 2012
June 7 and 8, 2012

12th Floor, Annex Building, Wisma Nusantara Complex, Jl. M.H. Thamrin No. 59 Jakarta 10350, Indonesia

MMA Forum Singapore
April 23-25, 2012

Grand Hyatt Singapore

With PHP, there's always more than one way to accomplish a particular task.

Take file upload, for example. Sure, you can do it the traditional way, using HTTP file upload and transferring the file directly to a location on your Web server's disk. Or you can do it the more exotic way, and use the FTP protocol to upload in a two-step process: from your local disk to a Web server, and then to an FTP server.

PHP supports both FTP and HTTP upload methods natively, leaving it to you to make the best decision based on the design requirements of your application. Transferring a file using PHP's FTP functions is almost the same as doing it using a traditional FTP client--as you'll see even the function names are similar to standard FTP commands.

Reams have already been written about HTTP file upload, which is why this brief tutorial focuses on FTP-based file uploads instead (in the example that follows, though, you'll see both in action). Note that this tutorial assumes that you have a working PHP/Apache installation, with both HTTP file upload and FTP functions active.

Step 1: Make sure you have permission to connect to/upload to the FTP server
PHP's FTP functions works over a client-server connection, and so you will need to log in to the target FTP server before you can perform a file upload. Your first task is to make sure that you have the credentials necessary for this action. This step might seem somewhat obvious, but you'd be surprised how many developers forget it and waste hours of debugging time later on.

You can check this by using a command-line FTP client to log in to the target FTP server and attempt to upload a dummy file (Listing A):

Listing A

$ ftp
ftp> open some.host.com
Connected to some.host.com.
220 Welcome to leon FTP Server!
User: upload
331 User upload okay, need password.
Password: ***
230 Restricted user logged in.
ftp> bin
200 Type okay.
ftp> hash
Hash mark printing On ftp: (2048 bytes/hash mark) .
ftp> put file.bin
200 PORT command successful.
150 Opening BINARY mode data connection.
##
226 Transfer completed.
ftp: 4289 bytes sent in 0.00Seconds 4289000.00Kbytes/sec.
ftp> bye
221 Goodbye.
Once you've confirmed that you have the relevant access privileges, log out.

Step 2: Construct an upload form
Next, create a simple HTML form that asks the user for critical parameters--FTP server access information, the server directory to upload to, and the full path and name of the file to be uploaded. Here's a sample (Listing B) of what this form might look like:

Listing B

<html>
<head></head>
<body>
<h2>Please provide the following information:</h2>
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
Host <br/>
<input type="text" name="host" /><p />
Username <br />
<input type="text" name="user" /><p />
Password <br />
<input type="password" name="pass" /><p />
Destination directory <br />
<input type="text" name="dir" /><p />
File <br />
<input type="file" name="file" /><p />
<input type="submit" name="submit" value="Upload File" />
</form>
</body>
</html>
Here, the <input type=file...> element renders as a file selection dialog box, from which the user can select the file to be uploaded. The <form enctype=...> element then forces the form data to be encoded as a multi-part submission, which makes it easier for PHP to identify the file component of the submission.

Step 3: Create the PHP upload processor
Once the form has been submitted to the Web server, the next (and final) step is to use PHP's FTP functions to transfer it to the target FTP server, using the access credentials supplied by the user. Here's the script (upload.php) that does all the work (Listing C):

Listing C

<?php
// get FTP access parameters
$host = $_POST['host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$destDir = $_POST['dir'];
$workDir = "/usr/local/temp"; // define this as per local system

// get temporary file name for the uploaded file
$tmpName = basename($_FILES['file']['tmp_name']);

// copy uploaded file into current directory
move_uploaded_file($_FILES['file']['tmp_name'], $workDir."/".$tmpName)
 or die("Cannot move uploaded file to working directory");

// open connection
$conn = ftp_connect($host) or die ("Cannot initiate connection to
 host");

// send access parameters
ftp_login($conn, $user, $pass) or die("Cannot login");

// perform file upload
$upload = ftp_put($conn, $destDir."/".$_FILES['file']['name'], 
 $workDir."/".$tmpName, FTP_BINARY);

// check upload status
// display message
if (!$upload) {
  echo "Cannot upload";
} else {
  echo "Upload complete";
}

// close the FTP stream
ftp_close($conn);

// delete local copy of uploaded file
unlink($workDir."/".$tmpName) or die("Cannot delete uploaded
 file from working directory -- manual deletion recommended");
?>
This might appear complicated, but it's actually fairly straightforward. Here's what's happening:

1- Once the form has been submitted, the credentials supplied by the user in the various form input fields are extracted into regular PHP variables. Information about the uploaded file also now becomes available through PHP's special $_FILES array.

2- The $_FILES array is made up of multiple sub-arrays, one for each uploaded file. The keys of each sub-array hold information about the size, MIME type, original name and temporary name of the corresponding uploaded file. This information is used by the move_uploaded_file() function to transfer the file from the system's temporary directory to the working directory. Remember to alter the value of $workDir to reflect a valid file path on your system.

3- The ftp_connect() and ftp_login() functions are used to initiate a connection to the named FTP host, and log in to it using the supplied credentials.

4- Assuming a successful login, the ftp_put() function is used to upload the file from the working directory to the remote directory specified by the user and rename it to its original form. Note the addition of the special FTP_BINARY argument to ftp_put(), to specify that the file be transferred in binary (not ASCII) mode. Depending on the result code returned by the ftp_put() function, an error or success message is displayed to the user.

5- The ftp_close() function is used to end the FTP session, and the unlink() function is used to clean up by deleting the local copy of the file that was created in step (2).

Simple, isn't it? Go on, try it out for yourself and see!

Talkback

Excellent article.

Not to put too much demand on free advice, but what would be *really* swell is a method to use the HTTP file upload facility to initial transfer of the file *directly* to another server than the local one providing the input form.

That would avoid double-transfer of the file and thus avoid huge delays when the local server has limited network bandwidth and the ultimate target server has lots. (I am facing this problem now with huge MPEG files that must be transferred.)

Is there some way accomplish this kind of voodoo?

Bud Hovell June 25, 2006

Keep your options open with FTP file uploads

I have a customer that needs to have a file submission/upload form on their website. The thing is that they have 2 servers - One is the hosting ftp and the other in a in-house network ftp. They want the form on the website server to upload the files to the in-house ftp. This is out of my expertise and I need help. I have been looking for a third party script/application that would allow me to do all of this. Can you point me in the right direction? Any help would be greatly appreciated! - Thanks

Baltadesign April 19, 2009
Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

ZDNet Asia Live

The key for mobile operators is identifying the applications that are popular with subscribers on their network. They can then work partn...

1 hour ago by camcullen on Experience trumps content in apps monetization

Experience trumps content in apps monetization | ZDNet http://t.co/gBXcjbGd

Experience trumps content in apps monetization - ZDNet Asia News: "What we are doing currently is not to monetiz... http://t.co/S2EZtd8m

Malaysia organizations don't realize severity of cyberattacks: "Minister Maximus Johnity Ongkili said at the Sec... http://t.co/bgVlOBvx

#security Malaysia organizations don't realize severity of cyberattacks: "Minister Maximus Johnity Ongkili said ... http://t.co/hkFb4zrI

Malaysia organizations don't realize severity of cyberattacks http://t.co/EEEmRM3j via @zdnetasia

Malaysia organizations don't realize severity of cyberattacks - ZDNet Asia News http://t.co/YpNMYgb5

Malaysia organizations don't realize severity of cyberattacks http://t.co/FFems54Q

China solar cell makers seek Taiwan partnerships http://t.co/p5Hh7kJD

Big data acquisitions pave way to fast, effective innovation http://t.co/hdiEfBsz via @zdnetasia

Integration, focused investments to propel Windows Phone: By Kevin Kwang , ZDNet Asia on May 23, 2012 (2 hours a... http://t.co/E7tsZbHJ

Integration, focused investments to propel Windows Phone http://t.co/u9TqjQ8C

ZDNet Asia IT Salary Benchmark 2012 http://t.co/rVwYlV7H

AsiaClassifiedToday. Integration, focused investments to propel Windows Phone - ZDNet Asia: S... http://t.co/47tdjZyG #asia #google #biz

Malaysian organizations are apathetic about information security and fail to realize they are potentially under... http://t.co/XeuvbXrs

Big data acquisitions pave way to fast, effective innovation - ZDNet Asia News http://t.co/vDZpl0lu

So much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...

1 day ago by y15822137359 on 5 SaaS adoption speed bumps to avoid

I reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...

2 days ago by wykoong on Gartner: Mobile CRM gives better ROI than social

This video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...

3 days ago by TradeBrother on A quick fill handle trick for Microsoft Excel

waiting...

5 days ago by eapete on What should count in a company's market value?

Boy, you've opened a can of worms now.

Wait for the rants & raves.

5 days ago by eapete on What should count in a company's market value?

I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...

5 days ago by wykoong on Drop the egos, copy ideas, then innovate