Executing curl image download from browser load - php

I have a php curl script which downloads an image from a specified url.
Save image from url with curl PHP
function grabImage($url, $saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
grabImage("www.example.com/image/2.png", "/var/www/site/images/image_2.png")
When a person visits my site an image from my server is downloaded however if the image is unavailable it will use curl to download it from my suppliers server, I would rather all images are downloaded from my server directly as I would not want to put any strain on my suppliers server.
The script works fine when running it in ssh e.g. /usr/bin/php /var/www/site/image_s.php but when loading it in a browser it does not download the image.
e.g. www.example.com/image_s.php
What do I need to do to make sure that a browser load is enough to download the image?

looks like a web server permissions error, when you run it on the console, you are running under the user you logged in as. this is not the same as the user that serves the website content (it could be 'nobody')

I did some more searching on the web and it turns out that you need to change folder permissions, by default the public can not write into a folder.
in linux you can change permission by doing the following
chmod 666 /var/www/site/folder_name

Related

Unable to save picture from url to my server

I'm try to save some pictures from url to my server, but i'm not able to do it.
this is my code (it's standard, i've found on internet):
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
but for each link i put inside $url variable, on my server is always saved a 26 byte image (that's not the original image).
What's wrong?
I can successfully download image using your curl code. It can happen that your server is not allowed to connect the outside web links.
This is a curl equivalent code that download images as well. I believe from your server, you can not download image using this code.
file_put_contents("img.jpg", file_get_contents("http://www.letteratu.it/wp-content/uploads/2014/03/cielo.jpg"));
Run your curl with verbose mode to see the curl's debug messages, and show that us.
curl_setopt($ch, CURLOPT_VERBOSE, 1);
I'm pretty sure you need to include the http:// in the URL. I'm fairly certain it thinks that it is a local file without it (i.e., an implicit file://).

on deploying php project on another system shows an error as permission denied

when i have tried to deploy a php project in another system i got a error like this
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'image.jpg' for writing: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/WISMAN/pmhome.php on line 61
here i am getting a json after calling a web service to get a json as a response which contains a image in the string form,which i am encoding it into an image and saving it as image.jpeg but shows this one
<?php
$userid=$_SESSION[user_id];
//echo $userid;
$useridofuser=array(
'user_id'=> "$userid");
//echo json_encode($useridofuser);//coverting the vlaues collected from form into json
//calling the web service
$url='webservice url';
$data=$useridofuser;
//echo("Input to Server : ".$data."\n");
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($useridofuser));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response= curl_exec($ch);
echo('\n'."Server response : \n \n".$response);
curl_close($ch);
//parsing the json response from server
$jsonde="$response";
$json_o=json_decode($jsonde);
$json_a=json_decode($jsonde,true);
//echo("$json_a");
$company_name=$json_a[user_data][company_name];
$name=$json_a[user_data][name];
$registration_date=$json_a[user_data][registration_date];
$user_id=$json_a[user_data][user_id];
$product_id=$json_a[product_data][0][product_id];
$product_registration_id=$json_a[product_data][0][product_registration_id];
$product_name=$json_a[product_data][0][product_name];
$registration_date=$json_a[product_data][0][registration_date];
$image=$json_a[product_data][0][image];
$expiry_date=$json_a[product_data][0][expiry_date];
$product_evaluation_level=$json_a[product_data][0][product_evaluation_level];
$_SESSION[regid]=$product_registration_id;
if($product_evaluation_level=="1")
{
$level=trial;
}
$image_base64="$image";
$img = imagecreatefromstring(base64_decode($image_base64));
if($img != false)
{
imagejpeg($img, 'image.jpg');
} ?>
You just don't have enough privileges for writing to destination folder. Your server is probably running under user apache, httpd or something like that and you have your user like Sree.
Therefore you have to be either in one group and have chmod set to x7x - writeable for group; or (not in the same group) xx7 - writeable for all.
Not mentioning possible issues caused by seLinux (which your system may be running).
You don't have a write permission to your directory. Give it permission then it should work.
Your application does not have write permissions in the target directory that you are trying to save the file into. you will need to log in using a secure shell and update the permissions using chmod

How to Copy twitter account image to server?

I am using the twitter api to access some account details. It's been made clear that you can easily get the twitter account image:
http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg
However, when i try to copy() this image in PHP to my local server it doesn't work. It seems you can't even ping the address as 'could not find host'.
How can I copy the twitter account image to my local server?
$twitsImg ='http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg';
$twit=file_get_contents($twitsImg);
$new_img = basename($twitsImg);
file_put_contents($new_img,$twit);
The problem you are having is that the php ini-file variable allow_url_fopen is turned off, as it should be. You need to be using cURL to get the file. cURL is usually installed on web servers by default. This example works on my site:
<?php
function save_image($img,$path){
$ch = curl_init($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$data=curl_exec($ch);
curl_close ($ch);
if(file_exists($path)){
unlink($path);
}
$fp = fopen($path,'x');
fwrite($fp, $data);
fclose($fp);
}
save_image("http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg","up/file2.jpg");
?>
Also make sure you have the right CHMOD (775) set on your destination folder.

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