I have a webpage that logs on to facebook and displays a list of all friends with their picture. I want to download all those pictures in order to a zip folder on my server for download. Each picture has a unique id, but needs to be downloaded in order and renamed with the naming scheme 1.jpg, 2.jpg, etc.
I have tried a few things but nothing has worked. First I tried:
$i = 1;
foreach ($friends["data"] as $value) {
$url = 'https://graph.facebook.com/' . $value["id"] . '/picture?type=large';
copy($url,"$i.jpg");
$i++;
}
I thought this worked, but when I tried to open the images, they were all 0kb-also this does not save all the contents as a zip for easy download.
I then tried this:
$i=1;
foreach ($friends["data"] as $value) {
$fileName = '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/> <br />';
echo $fileName;
header("Content-Disposition: attachment; filename='".$fileName."'");
$target=$fileName;
$newName=$i.'jpg';
rename($target, $newName);
$i++;
}
But this gave an error:
Warning: Cannot modify header information - headers already sent by (output started at /home/xxx/public_html/xxx.com/facebooksdk/facebook-php-sdk/examples/test.php:70) in /home/xxx0/public_html/xxx.com/facebooksdk/facebook-php-sdk/examples/test.php on line 138
Warning: rename(graph.facebook.com/xx/picture?type=large"/> <br />,1jpg) [function.rename]: No such file or directory in /home/xxx/public_html/xxx.com/facebooksdk/facebook-php-sdk/examples/test.php on line 141
This second code I may not have even written correctly though...any help would be greatly appreciated!
This seems to be working:
$i = 1;
foreach ($friends["data"] as $value) {
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/> <br />';
$url = 'https://graph.facebook.com/' . $value["id"] . '/picture?type=large';
$img = 'photos/'.$i.'.jpg';
file_put_contents($img, file_get_contents($url));
$i++;
}
Though I haven't yet tackled how to zip the folder.
Related
I have this code on my computer and it runs perfectly fine but when someone else tries to run it in a different environment, getimagesize() is returning false every time for some reason (should be returning true a lot). Any ideas why this snippet of code would run completely different in different environments?
$i = 2;
while ($i != 0){
$theFile = "url/to/images/" . $image . $i . ".GIF";
//echo $theFile . "<br />";
if ($imageSize = #getimagesize($theFile)){
//echo "added...<br />";
$theRow .= "<a href='" . $theFile . "' rel='lightbox[" . $image . "]'></a>";
$i++;
}else{
$i = 0;
}
}
If I uncomment out the two lines there all $theFile's print to the screen fine and they are all valid URLs but it's just a bunch of
thisimage2.GIF
thatimage2.GIF
anotherimage2.GIF
...
They all end with 2.GIF but there are many that should have 3, 4, 5, 6 all the way up to 12.GIF but it's never increasing $i because it never returns true with getimagesize(). Again, when I uncomment echo $theFile . "<br />"; it prints valid URLs to images that the other person can paste into a browser address bar and see the image just fine.
I'm running php 5.4.17 and the exact same code works fine for me. The other machine is running php 5.4.7 and it's not working correctly. I tried to look up any differences between the two versions for getimagesize() but couldn't find anything.
Edit: When run without the "#" on getimagesize() on the machine where it's not working it gives the following warning: Warning: getimagesize(): Unable to find the wrapper “https” - did you forget to enable it when you configured PHP?
There are a couple things wrong here. Fortunately, most of them are easy fixes.
$image doesn't appear to be defined anywhere, but maybe it has and you just didn't include it.
There's no text in between each opening and closing <a> tag, so the only thing you'd see would be links like this: <a href='url/to/images/imagename1.GIF' rel='lightbox[]'></a> (but again, maybe that's intentional).
$theRow doesn't appear to echoed anywhere, but maybe it is and you just didn't include that part. Further, it doesn't look like $theRow was initially defined anywhere either.
Your while() loop will only display the last image processed. In this case, I'd use a for() loop instead.
If your goal is to build up $theRow and then display it all at the end, I'd go with something like this instead:
<?php
// EDIT: check to see if openssl exists first, due to the https error you're receiving.
$extensions = get_loaded_extensions();
if (!in_array('openssl', $extensions)) {
echo 'OpenSSL extension not loaded in this environment';
}
// Define $theRow first
$theRow = '';
// "Given that $i = 0, while $i is less than 3, auto-increment $i"
for ($i = 0; $i < 3; $i++){
$theFile = "url/to/images/" . $image . $i . ".GIF";
//echo $theFile . "<br />";
// Remove the # sign to display errors if you want
if ($imageSize = #getimagesize($theFile)){
//echo "added...<br />";
// Add to $theRow, using $theFile as the text displayed in between each <a> tag
$theRow .= "<a href='" . $theFile . "' rel='lightbox[" . $image . "]'>" . $theFile . "</a>";
}
else {
//This should only appear if $imageSize didn't work.
echo '$imageSize has not been set for ' . $theFile . '<br />';
}
}
// Now that $theRow has been built, echo the whole thing
echo $theRow;
I want to show my visitors the images in a folder and after then have seen it, I want all those files deleted!
This is what I tried, but It won't work. I think it's because PHP is generating a html file which tells the browser it must first get an image from a different place but the html file was already removed.
<?php
foreach (glob("files/*.*") as $prevpic) {
echo '<img src="' . $prevpic . '" />';
}
foreach (glob("files/*.*") as $file) {
unlink($file);
}
move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"]);
?>
You can do something like so ...
<?php
foreach (glob("files/*.*") as $file) {
echo '<img src="data:image/' . pathinfo($file, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($file)) . '" />';
unlink($file);
}
?>
... which is basically writing the image data into the html, and then discarding the image.
I would handle this by simply managing your images through a download script (php).
You track sessions and simply don't display the images requested, but fail gracefully with a response, or let your app handle it via session based tracking.
That way no images are deleted 'onview'.
This may be a strange question, but I have this loop here:
foreach ($friends["data"] as $value) {
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/> <br />
}
Running this opens over 2000 images.
I need to download each of these images to a folder on my desktop with the name 1.jpg, 2.jpg, 3.jpg, etc.
Frankly this is weird to me, and I don't even know how to try and research how to do this. This images aren't huge, but the do need to stay in order as they will be lined up later with captions that are numbered.
Any help greatly appreciated!
I tried this:
foreach ($friends["data"] as $value) {
$i = 1;
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/> <br />';
$url = 'https://graph.facebook.com/' . $value["id"] . '/picture?type=large';
$img = '/me/Desktop/Facebook/'.$i.'.jpg';
file_put_contents($img, file_get_contents($url));
$i++;
}
but I got the error:
Warning: file_put_contents(/me/Desktop/Facebook/1.jpg) [function.file-put-contents]: failed to open stream: No such file or directory in /home/blahblah blah on line 125
looks easy.
$i = 1;
foreach ($friends["data"] as $value) {
$url = 'https://graph.facebook.com/' . $value["id"] . '/picture?type=large';
copy($url,"$i.jpg");
$i++;
}
check your directory placed 1.jpg, 2.jpg, 3.jpg ...
i have this code to display images, where each user has his own, i'll comment it to save your time
<?php
session_start();
$name=$_SESSION['valid_user']; //saved current username in variable
$loc="./uploads/"; //location of image directory
$path=$loc.$name; //current user's folder to save his images
echo $path."<br>"; //i used this to make sure the path is ok, only for testing
if(is_dir($path)) //if directory exists, show it exists,otherwise show it
{ //doesnt exists
echo "<br>exists";
}
else
{
echo "<br>not exists";
}
$files = glob($path."/");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i]; //picture number
print $num."<br />";
echo '<img src="'.$path.'" alt="random image" height="100" width="100"/>'."<br /><br />";
} //shows the picture till the last one
?>
the output that i get is this this
./uploads/user_name
exists
but it does not show the images, even though the folder is not empty (upload script works fine).
EDIT; solved it (low rep, cant answer my own question).
got it. For anyone who cares, this line here
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
wasn't working because i added $files, which already contained the path, and it was giving input to img src as
/uploads/username/uploads/username
so that was two times the same path.Upon removing $path, and using just
<img src="' . $files[$i] . '"
did the trick. Thank you all for your help.
I think you need to pass a wildcard path to glob: glob($path . '/*'). You are also not printing the filename in the image source attribute:
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
Also, your $num is actually the filename, not the picture number - that is $i. You could really simplify that loop using the foreach construct:
foreach($files as $filename) {
// etc
}
you need to add a pattern for using glob afaik
$files = glob($path."/*.*"); // all files
$files = glob($path."/*.jpg"); // all jpgs etc.pp
foreach($files as $idx => $file)
{
$num = $idx+1; //idx starts with 0 so we add one here
print $num."<br />";
echo '<img src="'.$path.'/'.$file'" alt="random image" height="100" width="100"/>'."<br /><br />";
}
Hi I am trying to get images to load into a page using the file names from an array,
This is what I have so far
<?php
$i=0;
$img=array("1.png","2.png","3.png","4.png");
while ($i<count($img))
{
echo "<img class='loadin' alt='imgg' src=" . "'http://www/images/" . $img[i] . "'" . "/" . ">" . "<br/>";
$i++;
}
?>
It seems to ignore the file name and just enters:
http://www/images/
as the source and ignores the file name from the array
Any Help would be great Thanks
Mikey
You forgot the dollar sign with your $i variable: $img[$i]
EDIT:
(btw. using a foreach-loop would be easier...)
foreach($img AS $filename) {
echo "<img class='loadin' alt='imgg' src='http://www/images/" . $filename . "'/><br/>";
}