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 ...
Related
I have got a directory which contains several folders with images. What I want to do is to read all the images using php script and showing them with div in a browser. The code I have tried to implement is the following:
<?php
function listFolderFiles($dir)
{
echo '<ol>';
foreach (new DirectoryIterator($dir) as $folder) {
if (!$folder->isDot()) {
// echo '<li>' . $folder->getFilename() . '<br>';
if ($folder->isDir()) {
$user = $dir . '/' . $folder;
foreach (new DirectoryIterator($user) as $file) {
if ($file != '.' && $file != '..') {
// echo '<li>' . $file->getFilename();
$image = $user . '/' . $file;
echo $image . '<br>';
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
}
}
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('images');
The command echo $image.'<br>'; prints the names of all images. With the following command I manage to create div, however without the images been displayed inside them:
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
Am I doing something wrong? The $image paths are correct.
EDIT: I move the folder of images in the same folder with the php file in the wamp folder. Now for example the image file is the following images/01virarias/1_.jpg. I change the call of my function as listFolderFiles('images');. However I am getting the same empty divs.
You need to use an URL. It seems you are using local files. Your browser is the 'problem'. Although it is a matter of security.
The images should be in your server environment.
And it seems you forgot a <li> start tag.
if your path is correct do a inspect element on your browser i think it's quoting issue try
echo '<img src="'. $image .'" width="500" height="500" alt=""/>';
You can use images path relative/absolute like
relative :- ../images/your_image
absolute :- http://localhost/your_image_path
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.
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/>";
}
I have the code below
<?php
$directory = 'images/slideshow';
try {
// Styling for images
echo '<div id="myslides">';
foreach ( new DirectoryIterator($directory) as $item ) {
if ($item->isFile()) {
$path = $directory . '/' . $item;
echo '<img src="' . $path . '"/>';
}
}
echo '</div>';
}
catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}
?>
copied from this topic:
Display Images From A Folder (slideshow)
I would like to know if its possible to load the images externally instead, or from a specific directory of a URL? Thanks in advance.
It is possible, but you would need to know the urls of each photo. Though that would be easy if you can make them follow a certain pattern I.e. www.example.com/dir/photo1, www.example.com/dir/photo2.
The external server you point must allow remote linking as well.
Then it's just a matter of making the links:
for ($i = 1; $i < NUMBER_OF_PHOTOS; ++$i) {
echo '<img src="http://www.example.com/dir/photo'.$i.'"/>';
}