Tuesday, January 27, 2009

FTP upload via cURL

FTP hosting is often much cheaper than regular web hosting. The upload with an ftp client is for sure the most common way, but could be a problem for people behind a firewall or without enough rights (capabilities) to install a FTP client. For those a upload via a web form is the best solution.
Using cURL for file transmissions
In this example we want to upload a file to some (password protected) remote FTP server via a web form.
The form is simple and has only one file field and the submit button. Don’t forget to protect this kind of pages. we need some PHP code to handle the upload and opens a stream to send the file via cURL to the remote FTP server.

start form action="" method="post" enctype="multipart/form-data"

input name="upload" type="file"
input type="submit" name="Submit" value="Upload"
end form

After the user has selected a file, the data is uploaded to the web server. We open the temp file with fopen and the cURL session is initialized. Together with the URL for the remote FTP server, we send the FTP login and password to the target location. The other cURL settings are required to send the file via the FTP protocol to the target server. If the returned error code is equal “0″, the upload was successful.
This small PHP snippet is responsible for the upload to some remote FTP server.

Labels:

5 Comments:

At November 11, 2009 at 7:48 PM , Blogger Unknown said...

Hi,

I have read ur curl topics. those r gr8. I m working on curl. the curl_getinfo() function is returning different time values each time i refresh my firefox browser. why this is giving different values each time a refresh?

I have a blog @ newdailyblog.blogspot.com.

thanks.

tahsin.

 
At November 11, 2009 at 8:58 PM , Blogger GlobalWorldTech said...

If we called curl_getinfo() without an optional parameter opt , it will returns an associative array with following element
"url" , "content_type", "http_code" , "header_size", "request_size" , "filetime" , "ssl_verify_result" , "redirect_count" , "total_time" , "namelookup_time", "connect_time" , "pretransfer_time" , "size_upload" , "size_download", "speed_download", "speed_upload" , "download_content_length" , "upload_content_length" , "starttransfer_time" ,"redirect_time".

eg ;-

$ch = curl_init("http://www.octazen.com/demo.php");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

print_r(curl_version());
echo "
";
curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_CONNECT_TIME);
echo '
';
echo curl_getinfo($ch, CURLINFO_SIZE_UPLOAD);
print_r(curl_getinfo($ch));

curl_close($ch);
fclose($fp);

Hope it ans your query.

 
At November 11, 2009 at 9:06 PM , Blogger Unknown said...

Hi,

thanks for ur reply.

*** code starts here


function byte_convert($bytes)
{
$symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

$exp = 0;
$converted_value = 0;
if( $bytes > 0 )
{
$exp = floor( log($bytes)/log(1024) );
$converted_value = ( $bytes/pow(1024,floor($exp)) );
}

return sprintf( '%.2f '.$symbol[$exp], $converted_value );
}

$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
$url = 'http://ksino.com/';

// initialize curl with given url
$ch = curl_init($url);
// make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1);
// add useragent
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
//Tell curl to write the response to a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// The maximum number of seconds to allow cURL functions to execute.
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
// Tell curl to stop when it encounters an error
curl_setopt($ch, CURLOPT_FAILONERROR, 1);

$execute = curl_exec($ch);

// Check if any error occured
if(!curl_errno($ch))
{
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
// Display the File Size
echo byte_convert($bytes);

$total_time = curl_getinfo($ch);

print_r($total_time);
//echo 'Took ' . $total_time . ' seconds to send a request to ' . $url;
clearstatcache();
}
curl_close($ch);

**** code ends here

plz run the code, and try to refresh it several time. and u will see the 'total_time' value will be changed every time. why?

tahsin.

 
At November 11, 2009 at 11:12 PM , Blogger GlobalWorldTech said...

Hi Tahsin

Total time value varies because total transaction time of transfer data changes as it depends on net speed, load on that site, client machine speed. If any of the above mentioned things varies, even in fraction of a second, it will affects on total transaction time.

Hope it solve your query.

Regards

 
At July 14, 2011 at 4:25 AM , Blogger John goh said...

is it possible to bypass web server and use CURL upload directly from client computer to the ftp server?

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home