Download a File from Ftp Using Curl and PHP

Download file from FTP using curl in php

Ok guys i get it :)

I used the ftp_* functions for ftp, in place of curl, because curl tried to change directories, but its not allowed on this ftp server, so the code i used is;

$local_file = 'output.rar';
$server_file = '/somedir/1/bar/foo/somearchive.rar';
$ftp_user_name='anonymous';
$ftp_user_pass='mozilla@example.com';
$ftp_server='server.host';
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

/* uncomment if you need to change directories
if (ftp_chdir($conn_id, "<directory>")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
*/

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}

// close the connection
ftp_close($conn_id);

It works !!

FTP upload file to distant server with CURL and PHP uploads a blank file

After 2 days of banging my head against the keyboard.. finally i did it..
Here is how:

<?php

if (isset($_POST['Submit'])) {
if (!empty($_FILES['upload']['name'])) {
$ch = curl_init();
$localfile = $_FILES['upload']['tmp_name'];
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://domain.com/'.$_FILES['upload']['name']);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
} else {
$error = 'Please select a file.';
}
}
echo $error;
?>

Here is the source, from where i found the solution

FTPS with PHP Curl getting partial download

curl probably notices, and curl_error() probably reports it (as a CURLE_PARTIAL_FILE error), but your code is completely ignoring that error. instead of

        if (curl_error($this->curlhandle)) {
return false;
} else {

try

        if (curl_errno($this->curlhandle)) {
throw new \RuntimeException('curl error: '.curl_errno($this->curlhandle).': '.curl_error($this->curlhandle));
} else {

now you should get a proper error if the curl download failed. however, to give you something to debug, i also suggest adding a protected $curldebugfileh;
to class ftps and in __construct do: curl_setopt_array($this->curlhandle,array(CURLOPT_VERBOSE=>true,CURLOPT_STDERR=>($this->curldebugfileh=tmpfile())));

and then change the exception to:

            throw new \RuntimeException('curl error: '.curl_errno($this->curlhandle).': '.curl_error($this->curlhandle).' curl verbose log: '.file_get_contents(stream_get_meta_data($this->curldebugfileh)['uri']));

and add to __destruct: fclose($this->curldebugfileh);

now you should get a verbose log in the exception about what happened up to the corrupted download, which would probably reveal why the download was corrupted.

edit: after reading closer, i see that you have no __destruct, and are never closing the curl handle, and is thus leaking memory. you should probably fix that too. adding function __destruct(){curl_close($this->curlhandle);fclose($this->curldebugfileh);} would prevent that memory/resource leak.

edit 2: i see you're doing $fp = fopen($local, 'w') - don't use w, this will corrupt just about everything you download, on certain OSs, like microsoft windows (it wont do any harm on linux, though..), use wb, and your code is safe to run on windows (and other OSs, including pre-OSX Mac, DOS, CP/M, OS/2, Symbian, and probably others)

edit 3: you're also leaking resources because you never fclose($fp); , you should probably fix that too.

How to get file(s) from FTP and copy it to another via PHP/CURL

$files = ["yourfile1.txt","yourfile2"];
foreach($files as $file){
$curl = curl_init();
//The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl, CURLOPT_URL, "http://example.com/putscript");

// A username and password formatted as "[username]:[password]" to use for the connection.
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file));
$fp = fopen($file, "r");
curl_setopt($curl, CURLOPT_INFILE, $
curl_exec($curl);
curl_close($curl);
fclose($fp);
}

Using CURL/PHP to download from FTP in chunks to save memory

This is pretty easy. All you need to do is provide cURL with a callback to handle data as it comes in.

function onResponseBodyData($ch, $data)
{
echo $data;
return strlen($data);
}

curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'onResponseBodyData');

Returning the length of data from your callback is important. It signifies how much data you processed. If you return something other than the length of data passed in (such as 0), then the request is aborted.

Now, make sure you don't have output buffering turned on, and configure your server to not buffer the entire response before sending. It will work out of the box on most configurations.

You can find more examples here: http://curl.haxx.se/libcurl/php/examples/callbacks.html



Related Topics



Leave a reply



Submit