Download a file that is force bound to PHP

I am trying to download a file from PHP from an FTP server, which I can also access from http. For example: FTP://username:password@someserver.com/file_name_here.gz. The file is forcibly used as an attachment from the server (and not in PDF or TXT format, for example: output). I tried this with three different ways:

  • file_get_contents -> Return file of zero size
  • Curl β†’ Timeout
  • FTP function -> Returns a file of zero size

I think CURL is a way to do this, but I cannot upload the file ... Here is my code:

$curl = curl_init();
$url ='ftp://'.$network['username'].':'.$network['password'].'@'.$network['metadata_url'].'/'.$path.'/'.$data['title'];
curl_setopt($curl, CURLOPT_URL,$url);
//curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$file = fopen("./feeds/".$data['title'], "w");
curl_setopt($curl, CURLOPT_FILE, $file);
$result = curl_exec ($curl);

I tried to set both the username and password in CURLOPT_URL and set them using CURL. The same thing happens in both ways ...

You can help?

Edit: , . , , : filename.gz.check, , .check, . Dunno, , , .

+3
2

( : " " FTP-. FTP- , FTP-, , . HTTP-, , (, HTTP), , , ( / ). FTP-, , FTP GET, , , HTTP GET.)

, , , $network $_FTP? , , , , , :

$curl = curl_init();
$url = "ftp://hostname/filename.gz";
$file = fopen("filename.gz", "w");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_exec($curl);
curl_close($curl);
fclose($file);

, , :

curl_setopt($curl, CURLOPT_VERBOSE, 1);

, ?

+1

cURL - :

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,0);

, :

<?php

$conn = ftp_connect("ftp.someserver.com") or die("Could not connect");

ftp_login($conn,"username","password");

echo ftp_size($conn,"source.tgz");

echo ftp_get($conn,"target.tgz","source.tgz",FTP_BINARY);

ftp_close($conn);

?> 
0

Source: https://habr.com/ru/post/1726919/


All Articles