FTP download file from server directly into client - php

I try to download a file from FTP server into client. If I use ftp_get, the file is downloaded into PHP server, which can write the output into browser. So the download process is
FTP server -> PHP server -> client
This doubles traffic - this is bad in downloading big files. There is a way how to write the file directly into the browser described here: Stream FTP download to output - but the data flows through PHP server anyway, am I right?
Is there any way how to establish this download (if yes, how?), or is it principially impossible?
FTP server -> client
Edit: it should work also with non-anonymous FTP servers in secure way.

Download the file ;-)

If the client can directly access the file in question (i.e. no secret usernames or passwords necessary), just redirect him to it:
header('Location: ftp://example.com/foobar');
This will cause the client to access the URL directly. You can't control what the client will do though. The browser may simply start to download the file, but it may also launch an FTP client or do other things which you may or may not care about.

try below code for that.
$curl = curl_init();
$file = fopen("ls-lR.gz", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
Thanks.

Related

Laravel storage disk put gives internal server error 500 on live server | no logs are created

I am saving image files to disk. After that I create a ZIP and download it. Everything works fine on localhost.
On the live server I get:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
The error occurs if I want to save an image to the storage:
$user = DB::connection('mysql_live')->table('users')->where('id', $userid)->first();
Storage::disk('gdpr')->put($user->picture, file_get_contents(env('AVATAR_PHOTO_SRC') . $user->picture));
env('AVATAR_PHOTO_SRC') is the absoulte path to the image, example: https://example.com/uploads/thumb/
There is no log file created. And the size of the zip is less then 1MB, images are even smaller.
What is happening here?
EDIT
$user->picture is the name of the file. example: example.jpg.
The file does exist and can be opened for example in the browser. Remember everything works fine on localhost.
EDIT
I found a log in try, catch:
file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0
EDIT
PHP ini cant be modified on a shared server. I will need an alternative way to store the image.
From PHP : www.php.net/manual/en/filesystem.configuration.php
allow_url_fopen boolean
This option enables the URL-aware fopen wrappers that enable accessing
URL object like files. Default wrappers are provided for the access of
remote files using the ftp or http protocol, some extensions like zlib
may register additional wrappers.
So you may need to enable allow_url_fopen=1 in your php.ini to enable ftp or http protocol.
Here is the curl solution, which worked for me:
$c = curl_init();
curl_setopt($c, CURLOPT_URL, env('AVATAR_PHOTO_SRC') . $user->picture);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($c, CURLOPT_CAINFO, Storage::disk('other')->path("cacert-2020-01-01.pem"));
$img = curl_exec($c);
$httpCode_post = curl_getinfo($c, CURLINFO_HTTP_CODE);
$cerror_post = curl_error($c);
curl_close($c);
Storage::disk('gdpr')->put($user->picture, $img);
You can set CURLOPT_SSL_VERIFYPEER to false then you dont need the step with the certificate.
But I advise you to add the certificate. Its super easy. Just download it here and save it somewhere in your storage.

Including a PHP File: cURL, Pathing, and Security Risks

So I have heard a lot about cURL having security risks. I need to know if what I am doing can have security risks, and if so, how can I prevent it.
I have my own VPS (Virtual Private Server) through Host Gator.
Basically I am trying to include a file from a different domain. Both domains are in the same server. I tried to use the absolute path to include the file, but I keep getting a permission denied error.
This is my code to include the file using cURL:
$url = "http://mydomain.com/include.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
Is there a security risk doing this? If so, how can I prevent this security risk?
In addition, how can I set permissions on the domain so I can include a PHP file from another domain on the same server?
If you're loading data from different domain but same server you can simply use include and path but you can neither grab php file with url nor its recommended.

check existance of image on remote server with allow_url_fopen=off

I am working on a site which is fetching images from a cdn server that has allow_url_fopen in turned off means allow_url_fopen=off.
The problem is that before showing the image in main site we have to check the existence of image in cdn server. If image exists then we will show it otherwise not.
file_exists and file_get_contents will not work as they require allow_url_fopen=on.
Is there any other way to do it???
Any help will be appreciated.
Thanks
You can use cUrl:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if(empty($result)){die('No image');}
You can use the CURL library to get the file via HTTP method. If that is used , if the file exists, the status code will be success otherwise not found status code will be returned. Another options is to use the socket library function like fsockopen to connect to that server and access the files via FTP if that is enabled.
Go through the examples given
http://php.net/manual/en/function.fsockopen.php
http://php.net/manual/en/function.fsockopen.php

Send files from LAMP stack to Windows SFTP server via PHP

I'm having a good deal of trouble sending a file from a linux server to a windows server over SFTP via PHP.
I seem to be connecting just fine, but it always throws an error that I can't create the file on the remote server's end. It's possible that I am messing up at the syntax for the file location.
I have tried two ways now, one using ssh2_scp_send(), and another trying
fopen(ssh2.sftp://D:/path/file.csv)
Also, logging into the sftp server via a client puts me at my home folder (ie D:\path\to\home) but if I do a
ssh2_exec($connection, 'cd');
and print the stream to the screen, it shows me that my ssh session is currently in the windows filesystem on the C drive.
I was hoping someone would have some advice on this. And I'm not married to this method. I'm using php on my end because it's all coming from a drupal module, but I could always try and incorporate another method.
If all you want to do is send one file, curl works, I suppose, but if you want to do anything more - like maybe verifying that the file has been uploaded, upload multiple files, use publickey authentication, or whatever, curl just isn't versatile enough for that.
My recommendation would be to use phpseclib, a pure PHP SFTP implementation.
CURL can use th sftp library.
$localfile = 'test.txt';
$ch = curl_init();
$fp = fopen ($localfile, "r") or die('Cannot open textfile');
curl_setopt($ch, CURLOPT_URL,"sftp://123.123.123.123:10022/username/test.txt");
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
if(curl_exec($ch)){
echo 'File was successfully transferred using SFTP';
}else{
echo 'File was unable to be transferred using SFTP';
}
curl_close ($ch);

How to copy a remote image to my website directory?

I post pictures from other websites and I would rather have those on my servers, in case their server dies all of a sudden. Say the file is located at "www.www.www/image.gif", how would I copy it to my directory "images" safely?
I write in PHP.
Thanks!
The following should work:
// requires allow_url_fopen
$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image);
or the cURL route:
$ch = curl_init('http://www.url.com/image.jpg');
$fp = fopen('/images/image.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
If your server is configured to support http:// file paths, you could use file_get_contents.
If that doesn't work, the second simplest way is by using curl for which you will certainly find full-fledged download scripts.
Some servers you pull images from may require a User-agent that shows that you are a regular browser. There is a ready-made class in the User Contributed Notes to curl that handles that, and provides a simple DownloadFile() function.

Categories