check existance of image on remote server with allow_url_fopen=off - php

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

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.

file_get_contents,CURL settings on a server

I am in process of providing an option similar to facebook share , wherein an external webpage contents ( content from the external page) can be displayed in my site. I am coding this using PHP and Ajax. but when i hosted my page on a free server site like www/0009.ws i get an error as below,
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /www/0009.ws
I would surely be moving this to a paid server later,
Is there a workaround if a paid service provider also does not allow me to use these options?
Do I have to setup my own server?
You can use the curl set of functions:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
assuming support is built in, which is a bit much. Honestly file_get_contents not having URL file access is something you should check before hand when choosing a provider.

FTP download file from server directly into client

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.

Reading HTML file from URL

While most of the time I'd just use file_get_contents and CURL, I can't get it to work with a port in the URL. How can I read this file?
http://174.120.124.178:7800/7.html (It's a shoutcast statistics file)
Ultimately, I just want the text after the last comma.
It has nothing to do with the port. They're blocking you because you're not using a browser user agent. curl does let you fake the user agent, but that may be a violation of the site's terms of service.
According to this post it's not about blocking scripts, but just distinguishing between Shoutcast clients and everything else. So the code is:
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla");
I tried to download your file with Curl on the command line and got a 404 error; it does load with Firefox and Lynx. This page says that you need to change the User-Agent string for it to download.
CURLOPT_PORT Needs to be set to the appropriate port perhaps~

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);

Categories