(Used to work!) Get Facebook Picture and Copy To Server - php

I have this code in PHP that used to work but now it isn't working.
I'm trying to grab the user's profile picture from Facebook, then copy and paste it into a directory on my server.
$url = 'http://graph.facebook.com/'.$fb_userID.'/picture?type=large';
$data = file_get_contents($url);
$fileName = 'users/avatars/'.$fb_username.$fb_userID.'.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);
Update: This code will put a jpg image into my directory with a size of 0 Kb so I'm pretty sure the content is never being grabbed correctly. Please help!

The solution was to adjust something in my php.ini file (or php5.ini) on my server.
I had to change this:
allow_url_fopen = Off
To on:
allow_url_fopen = on
Done.

Related

php how download file from url and save it on server

my link (URL) is different!!! and does not work with usual method
I think because site load with js or aspx
you can test my link (URL) in your browser and see download starting
but cant work in php
I have tested all methods (fetch, curl, file, get, put), but it does not work.
I have a similar URL here: 'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'
I can open it in the browser and download a csv file I need to do this in php and save the csv file on server
Here is what I have tried so far:
<?php
$file = fopen('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
$file = file_get_contents('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
file_put_contents('ClientTypeAll.csv', $file);
?>
I do not want Contents !!! I want a csv file form my link
if you test my link in your browser download start in your pc
I run this code with a remote PDF file.
<?php
$url = 'https://example.com/file.pdf';
$dir_name = 'storage-x'; // saVe the directory name
if (!file_exists($dir_name)) {
mkdir($dir_name, 0777, true);
}
$path = $dir_name.'/'.rand().'.pdf';
$file = file_get_contents($url);
file_put_contents($path, $file);
?>
Please follow the below step.
Get file form URL.
Set directory and check file exit condition and update directory access permission.
Set new file path, name, and directory.
Save the file.
Please check the below example which works for me.
<?php
$file = file_get_contents('https://example.com/file.pdf');
$dirName = 'storage-pdf';
if (!file_exists($dirName)) {
mkdir($dirName, 0777, true);
}
$newFilePath = $dirName.'/'.rand().'.pdf';
file_put_contents($newFilePath, $file);
?>

Issues with using file_get_contents and file_put_contents in PHP

I have to download an image in a weblink to my local folder using php. Below is the program.
<?php
if (isset ($_POST['submit'])){
$URL = 'http://10.251.13.7/gtz/temp3.php/download.png'; // Like "http:// ...."
$FileToSave = 'uploads/download.png'; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content); //file_put_contents('uploads/image.jpg',file_get_contents('10.251.13.7/gtz/temp3.php/mtps_files.png'));
} ?>
I could successfully download the image. But when I try to open it's showing the file has been damaged. In one way I am able to right click the image in the weblink and save the file to my local directory. It works.
On the other hand, in php programming I was able to download the file and when I access the file, it is showing as file got damaged.
Please review below code. Please assign the home folder path in file & save file.
$URL = 'C:/home/demo/Downloads.png'; // Like "http:// ...."
$FileToSave = 'D:/demo/download.png'; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content);

PHP fwrite() not working

I'm writing a function in php, client side I have a canvas image which I use toDataUrl() along with a file name to save the image on the server. The here's the code:
<?php
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
$data = json_decode($imageData, true);
$file = $data["file"];
$image = $data["data"];
$filteredData=substr($image, strpos($image, ",")+1);
$unencodedData=base64_decode($filteredData);
$fp = fopen( 'image/' . $file , 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
?>
The thing is that this code works. And for two out of three of the pages I used it on it works fine. The problem is when I copy and pasted it a third time to implement it again, for some reason the file is made on the server except that no data get's written into the file. I don't think it's a problem client side because I write in a debug alert message in the javascript and a debug echo into the PHP and both are able to print out the data fine. I made this short debug file:
<?php
$fp = fopen('data.txt', 'wb');
if(is_writable('data.txt')){
echo "file is writable<br>";
}
if(fwrite($fp, 'test') == FALSE){
echo "failed to write data<br>";
}
fclose($fp);
?>
And the output is
file is writable
failed to write data
I've tried using chmod and setting everything, the folder, the text file before I write to it to 0777 and I still get the same result; the file is made but no data is written into it. Is there anything I'm missing or any other approaches that might help. I haven't found anything on google and am still baffled as to why the same code worked exactly as expected twice before suddenly stopping for no apparent reason.
Thanks in advance.
I know this is an old post, but I had a very similar problem and found a solution (for me at least)! I ran out of disk space on my server, so it could create a 0 byte file, but wouldn't write to it. After I cleared out some space (deleted a 13gb error.log file) everything started working again as expected.
If fopen works but fwrite mysteriously doesn't, check your disk space. 'df -h' is the command to check disk space on a linux server.
instead of $fp = fopen('data.txt', 'wb'); give $fp = fopen('data.txt', 'w'); and try
Changed "wb" to "w"
When you write $fp = fopen('data.txt', 'w'); for your domain website.com having root at /var/www/website/ and if the php file is located at /var/www/website/php/server/file/admin.php or something similar, it will actually create a file at /var/www/website/data.txt
Try giving absolute path or path relative to your domain root to create files like,
$fp = fopen('php/server/file/data.txt', 'w');
Try the find command to see if the file is created anywhere else in the folder directory by using the following in Ubuntu,
find /var/www/website/ -name 'data.txt'
I had this issue, probably can help you solve if you have similar issue.

PHP copy() creates an empty image file

I have one image in a directory on my server, and want to copy it to another directory.
So I'm using
$post_picture = 'http://mysite.com/image.jpg';
copy($post_picture,
'images/pictures/post/thumb/' .
$info['filename'] .
'_thumb.' .
$info['extension']);
The issue is that in fact a file is created in my thumb directory, but that image is empty (0 x 0 pixels). I get no errors.
Any idea what is happening?
Permissions on all dirs are 755, both original and copy image have 644. The original show normally on a browser.
Thanks.
Do you have any form of hot-link protection that could alter what php receives?
Is allow_url_fopen allowed?
The $post_picture variable should probably have the file system path to the file, rather than the URL to the file.
Is allow_url_fopen set to true in your php.ini?
Sometimes that can produce this result if remote connections are being blocked.
$post_picture should be a local path i believe
copy( '/path/image.jpg', ... );
Please use this one...
<?php
$source = 'f-1/2.jpg';
$destination = 'f-2/2.jpg';
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
?>

Wamp php calling to same site to download a file

I'm writing tests for my CodeIgniter site using simpletester.
One of the things that it does is offer autogenerated files.
I want to test if the output of a file is correct.
However, if I do this:
function testFunction1(){
$url = site_url('downloader/function1');
$handle = fopen($url,'r');
$contents = stream_get_contents($handle);
echo $contents;
fclose($handle);
}
It outputs a 404.
It works perfectly when I put "www.google.com" as the url.
I can also download the file if I copy paste the URL in the browser.
Thanks!
edit:
Even if I try to download a regular file (so not a php function), with the full url, it gives the 404.
when your using relative paths, simply remove site_url() for relative path instances.
fopen accepts the following:
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password#example.com/somefile.txt", "w");
But site_url seems to be adding something.
also be sure to check the relative path directory is corrent.
If you're accessing locally then just use
$url = "main/function":
$file_content = file_get_contents("http://localhost/codeignitor/" . $url);

Categories