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 />";
}
Related
I have a bunch of .png's in a directory that are named the same as the values in the database. I would like to do a check if the filename (without '.png') exists in the database or not.
At the moment I am using this code to display the flags those are in the database
<?php
$result = mysql_query("SELECT `country` FROM `countries`");
while($row = mysql_fetch_array($result)) {;
?>
<img src="images/countries/<?php echo $row['country']?>.png" />
<?php }; ?>
Since there are only 3 countries in the column it's displayed as:
I would like to display the flags that are not in the database as well, but then in greyscale.
I came up wit this code to get all the flags from the directory
<?php
$directory = "images/countries/";
$images = glob($directory . "*.png");
foreach($images as $image)
{
echo '<image src="'.$image.'" class="flaginactive"/>'; //show all flags as inactive
echo basename($image, '.png'); //get file name with .png
}
?>
But somehow I am stuck and clueless how I could get them both in an if statement.
Can someone advise me how I can solve this the best way. I am aware I am using the old mySQL functions.
There are many ways to achieve this. I will relate one.
First load the names in the database in to an array. Then check the existence of the enumerated file names of the directory in the array to decide the class of the element.
Elements are shown inactive if a file in the directory is not found in the database.
<?php
$directory = "images/countries/";
//Lets save all the file names in the database to an array
$dbImages = array();
$result = mysql_query("SELECT `country` FROM `countries`");
while($row = mysql_fetch_array($result)) {
array_push($dbImages, $directory . $row['country'] . '.png');
}
//Lets go through all the files in the directory and see if they are in the $dbImages array
$images = glob($directory . "*.png");
foreach($images as $image)
{
//Decide the class attribute based on the existence of the file name in $dbImages array
if (in_array($image, $dbImages))
$classAttribute = '';
else
$classAttribute = 'class="flaginactive"'
echo '<image src="'.$image.'" ' . $classAttribute . ' />';
}
?>
You can file_exists() function like this
<?php
$directory = "images/countries/";
$images = glob($directory . "*.png");
foreach($images as $image)
{
if(file_exists($_SERVER['DOCUMENT_ROOT'] . $directory . $image . '.png')) //Files exists goes here
{
echo '<image src="'.$image.'" class="flaginactive"/>'; //show all flags as inactive
echo basename($image, '.png'); //get file name with .png
}
}
?>
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
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 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 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.'"/>';
}