PHP, uploading image with file_put_contents make 0 byte file - php

uploading image with file_put_contents make 0 byte file.
here is the code that I use. I extract facebook image url and put it into web server.
$fb_image_url = 'https://example.com/229282.jpg'
$filename = substr($fb_image_url, strrpos($fb_image_url, '/') + 1);
file_put_contents('upload/user_pic/original/'.$filename, file_get_contents($fb_image_url));
after I do this, the server receive file name successfully, but it is 0 bytes.
I checked php.ini, and allow_url_fopen is ON.
uploading folder permission is also fine.

To copy images from facebook, script/php program require permission for same. if program/ FB API dont passes validation/permission check, FB dont allows to download any image.
Its looks like your Application don't have permission to download/copy this image from Facebook that's why your getting 0 bytes.
Try giving PUBLIC access to image and keep FB account logged in while coping image

I just put that URL into the browser:
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/274661_1171545457_6475606_n.jpg
And received 404 Not Found in response. This would explain why you get empty file locally. I strongly suggest that you load the data first, verify what you received and then, if validation passes, save it locally.

here is the solution.
I had a problem with facebook profile address.
$fb_image_url = 'https://graph.facebook.com/'.$facebook_id.'/picture?type=large';
$filename = $fb_id.'_'.time().'.jpg';
$result = file_get_contents($fb_image_url);
file_put_contents('upload/user_pic/original/'.$filename, $result);
with this_ it worked fine. Yeah! Thank you everyone!

Related

How to get a file from remote server and dispose to user as a download?

Sorry if I did not get this title right.
I have in my server the redirect.php script that receives a URL passed by the client user, fetches the content using file_get_contents() function and them shows it to the user with echo() function.
The problem is when the user points directly to a PDF or JPG file in that URL and them the script shows the file contents as binary code.
When I set the code to recognize when the requested URL points directly to a downloadable file,
What should be the function or header to echo to the user so that his browser ask him to download the file insted of showing it?
Do I have to first put it into a file inside my server or can I do it directly from a command like file_get_contents()? If I can do that without writing it to my server, it would be a much better approuch.
I can't point directly to the server because some sites are blocked by my employee and the third party company that does this service thinks that StakExchenge sites are malicious and not constructive and were tegged as online communities like Facebook.
Try this:
$sourcefile = "http://www.myremotewebsite.com/myfile.jpg";
$destfile = "myfile.jpg";
copy($sourcefile, $destfile);

PHP Amazon S3 access private files through URL

I'm using AWS PHP sdk to save images on S3. Files are saved privately. Then, I'm showing the image thumbnails using the S3 file url in my web application but since the files are private so the images are displayed as corrupt.
When the user clicks on the name of file, a modal is opened to show the file in larger size but file is displayed as corrupt there as well due to the same issue.
Now, I know that there are two ways to make this working. 1. Make the files public. 2. Generate pre-signed urls for files. But I cannot go with any of these two options due to the requirements of my project.
My question is that is there any third way to resolve this issue?
I'd highly advise against this, but you could create a script on your own server that pulls the image via the API, caches it and serves. You can then restrict access however you like without making the images public.
Example pass through script:
$headers = get_headers($realpath); // Real path being where ever the file really is
foreach($headers as $header) {
header($header);
}
$filename = $version->getFilename();
// These lines if it's a download you want to do
// header('Content-Description: File Transfer');
// header("Content-Disposition: attachment; filename={$filename}");
$file = fopen($realpath, 'r');
fpassthru($file);
fclose($file);
exit;
This will barely "touch the sides" and shouldn't delay the appearance of your files too much, but t's still going to take some resources and bandwidth.
You will need to access the files through a script on your server. That script will do some kind of authentication to make sure the request is valid and you want them to see the file. Then fetch the file from S3 using a valid IAM profile that can access the private files. Output the file
Instead of requesting the file from S3 request it from
http://www.yourdomain.com/fetchimages.php?key=8498439834
Then here is some pseudocode in fetchimages.php
<?php
//if authorized to get this image
$key=$_GET['key'];
//validate key is the proper format
//get s3 url from a database based on the $key
//connect to s3 securely and read the file from s3
//output the file
?>
as far as i know you could try to make your S3 bucket a "web server" like this but then you would probably "Make the files public".Then if you have some kind of logic to restrict the access you could create a bucket policy

$_FILES to a resource - Laravel 5 and Dropbox

Well, I've uploaded an app to Heroku, and I've discovered that I can't upload files to it. Then I started to use Dropbox as storage option, and I've done a few tests, of send and retrieve link, and all worked fine.
Now, the problem is to use the uploadFile() method on DropboxAdapter. He accepts an resource as the file, and I did'nt work well. I've done a few tests, and still no way. Here is what I am doing, if anyone could me point a solution, or a direction to this problem, please. :)
Here is my actual code for the update user (Update the user image, and get the link to the file).
$input = $_FILES['picture'];
$inputName = $input['name'];
$image = imagecreatefromstring(file_get_contents($_FILES['picture']['tmp_name']));
Storage::disk('dropbox')->putStream('/avatars/' . $inputName, $image);
// $data = Storage::disk('dropbox')->getLink('/avatars/' . $inputName);
return dd($image);
In some tests, using fopen() into a file on the disk, and doing the same process, I've noticed this:
This is when I've used fopen() on a file stored on the public folder
http://i.imgur.com/07ZiZD5.png
And this, when i've die(var_dump()) the $image that I've tried to create. (Which is a suggestion from this two links: PHP temporary file upload not valid Image resource, Dropbox uploading within script.
http://i.imgur.com/pSv6l1k.png
Any Idea?
Try a simple fopen on the uploaded file:
$image = fopen($_FILES['picture']['tmp_name'], 'r');
https://www.php.net/manual/en/function.fopen.php
You don't need an image stream but just a filestream, which fopen provides.

How do I directly upload a file stream to Flickr using a CURL POST?

I'm writing a web app that at one point allows a user to upload a photo to a flickr account (mine). I want to do this without saving the intermediate image on the server my web app is on.
What I've got so far is a page which implements phpFlickr and accepts a POST from a simple html form. I use $_FILES['file']['tmp_name'] as the path for phpFlickr to use. Here's the code:
<?php
require_once("phpFlickr.php");
$f = new phpFlickr("apikey", "secret", true);
$_SESSION['phpFlickr_auth_redirect'] = "post_upload.php";
$myPerms = $f->auth("write");
$token = $f->auth_checkToken();
$phid = $f->sync_upload($_FILES['file']['tmp_name']);
echo "Uploading Photo..." . $phid;
?>
I'm guessing that the tmp file is being lost because of the redirect that happens when $f->auth("write") is called, but I don't know. Is there a way to preserve it? Is there any way to do this without saving the file to the server?
Answer: There is No way to directly upload a file to Flickr without saving it as an intermediate file.
I've moved on to using move_uploaded_file() followed by a flickr API call, and its working perfectly.
I've also managed to get it to play nice with the excellent Jquery Uploadify, which lets me send multiple files to it in one go.

Upload file from remote server... (PHP)

I am working with the FaceBook API to upload photos, and the API requires a local file path. As far as I can tell they are handing the request off to CURL like such: upload=#localfilename
But my files don't reside locally, so I am trying to figure out if there is a way to make it work with a remote file....
I tried pointing it to a local file which just did 'echo file_get_contents('some_remote_image.jpg');'
but that didn't work.
Does anyone have any ideas?
Thanks!
You can however link to them eg
<img src="www.mydomain.com"/>
Is it not possible to cache the file on your server and then delete it after upload? I wrote a Flickr -> Facebook Photo Album app that does this very thing.
All right, well if you don't want to do that, you can do this:
Edit the file facebookapi_php5_restlib.php, tracing the photo upload calls down to the actual curl call, which is in:
private function post_upload_request($method, $params, $file, $server_addr = null)
Now the hacky part, instead of passing the filename for $file, pass in an array and then check if its an array in this function. If it is, extract the binary data from it and set it as one of the post parameters. Some curl parameters that you may need can be found here:
http://www.digimantra.com/technology/php/post-pictures-on-twitpic-api-using-php/

Categories