A
A
Andrey Terekhov2017-08-31 16:00:35
PHP
Andrey Terekhov, 2017-08-31 16:00:35

What is the problem, the file I download from the ftp server always comes up empty, but when I open it in the browser, I can see the information?

Here are some variations of the code I use to accomplish this task
$fileSrc = $this->getTmpDir(). self::FILE_NAME;
$handle = fopen($fileSrc, 'w');
$matches = parse_url($connection);
$conn_id = ftp_connect($matches['host']);
$login_result = ftp_login($conn_id, $matches['user']?:"anonymous", $matches['pass']?:"");
if (ftp_fget($conn_id, $handle, $matches['path'], FTP_ASCII, 0)) {
ftp_close($conn_id);
fclose($handle);
return $fileSrc;
} else {
throw new \Exception('Failed to write, please try again');
$fileSrc = $this->getTmpDir(). self::FILE_NAME;
$matches = parse_url($connection);
$curl = curl_init();
$file = fopen($fileSrc, 'w');
$url = $matches['scheme']."://".$matches['host'].$matches['path'];
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
curl_setopt($curl, CURLOPT_URL, $url); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($curl, CURLOPT_USERPWD, $matches['user']?:"anonymous".":".$matches['pass']?:"");
curl_exec($curl);
curl_close($curl);
fclose($file);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-08-31
@BorisKorobkov

Probably a problem with FTP or file permissions.
Or FTP and HTTP different files.
Do echo $connection;this, enter this data into any ftp program (for example, FileZilla) and download the file manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question