I have study case want to show image from database from folder but the name in the database is not the same as the image file name, so it's like the %like% function (like in a query).
For example the file name: 1234_abcd_0.jpg
The data in the database is only: abcd.jpg
Can php glob work if i use like this?
$partnameoffile="abcd";
$dirname = "media/images/";
$images = glob($dirname."*".$partnameoffile."*.png");
foreach($images as $image) { echo '<img src="'.$image.'" /><br />'; }
Related
On my website's dashboard, I use a while loop to retrieve data from a DB and echo each record. Inside this while loop, I use glob() to show all the images inside the specific folder, which is a column in my DB table, $record["folder"].
This is the code I use:
while($record = mysqli_fetch_array($result))
{
$directory = '../send-us-your-photos/media/'.$record['folder'];
chdir($directory);
$images = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image) {
echo '<div class="col-sm-4">
<img src="'.$directory.'/'.$image.'" />
<div>'.$image.'</div>
</div>';
}
}
Now, the first record works fine and is showing the images in the specific folder. But the next records all show the same $image data but for a different folder.
So for example:
Record 1: folder 1 - shows image1.jpg
Record 2: folder 2 - tries to find same image1.jpg in folder 2
Record 3: folder 3 - tries to find same image1.jpg in folder 3
What I need:
Record 1: folder 1 - shows images in folder 1
Record 2: folder 2 - shows images in folder 2
Record 3: folder 3 - shows images in folder 3
How should I change my code so it shows the correct images in each record?
simply , don't change your directory , and use your directory path directly in glob function
while($record = mysqli_fetch_array($result))
{
$directory = '../send-us-your-photos/media/'.$record['folder'];
// chdir($directory);
$images = glob($directory . "/*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image) {
echo '<div class="col-sm-4">
<img src="'.$directory.'/'.$image.'" />
<div>'.$image.'</div>
</div>';
}
}
I think it's down to your chdir statement. You are going down one folder .. then up to send-us-your-photos/media/_FOLDER_.
So the next time you start off in send-us-your-photos/media/_FOLDER_ and go back one folder .. to send-us-your-photos/media/ and then it tries to go to send-us-your-photos/media/send-us-your-photos/media/_FOLDER_ and obviously fails.
It doesn't look like it's failing because you're outputting the value of $directory which you set yourself. Try outputting getcwd() instead.
Im trying to display alla images in a <img> from the db but i am only getting a broken image!
I am storing the ['tmp_name'] as LONGBLOB and the ['name'] as type in my tabel.
$query = $this->connection->prepare("SELECT * FROM images");
$query->execute();
$row = $query->fetchAll();
foreach($row as $img){
echo "<pre>";var_dump($img['name']);"</pre>";
echo "<img src=".$img["name"].">";
}
//header("Content-type: ".$row['type']);
print $row['name'];
die();
Should i use ['tmp_name'] or ['name'] in the <img>?
<img src=""> should include the full server path where your image is successfully uploaded. Also should include the image extension as well (.jpg, .png, .gif). should look like similar to following.
<img src="http://ichef.bbci.co.uk/images/ic/160xn/p0306tt8.png">
Easiest way is to check the source code of page and see if you get complete / correct path to the image. If not, check/change your code for missing part.
So i have a set of 20 images in a set, all labeled as dog. So dog01, dog02, dog03, etc. I'm using this code to pull those out of a directory and display them 5 to a row, in 4 rows like so.
dog01 dog02 dog03 dog04 dog05
dog06 dog07 dog08 dog09 dog10 (etc.)
I"m using this code to load the images from a directory, and it is loading them in order.
<?php
$dirname = "images/";
$images = glob($dirname."dog*.png");
foreach ($images as $i=>$image) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
if(($i+1)%5 == 0) echo '<br />';
}
?>
However, I want to see if there is a way to get it so that if one of the dogs is missing from the directory, it instead loads a filler in its place, and continues to load the others in sequence. So if dog03 is missing, it would look like this:
dog01 dog02 filler dog04 dog05
It would show filler, and continue on the sequence. I'm genuinely not sure if I can achieve this in php. If someone knows what approach I need to take here, that would be appreciated. I should note that the filler image is in another directory called "fillers/".
Thanks in advance.
You have two ways of approaching this I think. The first is by not worrying about it in php, and loading the filler image as background image. Normally the image will be loaded over the filler image, but if the image does not load, the filler image stays visible.
The other way is by testing if the file exists.
As background image:
.deck {
//whatever you had here
background-image: url( "/filler/filler.png" );
}
You might need to make container divs around your images, and put the css on that instead. Please note that if no explicit width or height is set, this will not work, as the background-image does not assign any width or height to the element it is attached to.
By testing beforehand:
<?php
$dirname = "images/";
$images = glob($dirname."dog*.png");
foreach ($images as $i=>$image) {
if( file_exists( $image ) ) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
} else {
echo '<img class="deck filler" src="/filler/filler.png" alt="This image does not exist." title="This image does not exist.">';
}
if(($i+1)%5 == 0) echo '<br />';
}
?>
This method might not work if you are running php in safe mode.
Hi I am using this code to output image from a file called images
<?php
//Path to folder which contains images
$dirname = $_POST['dir'];
//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."*");
//Display image using foreach loop
foreach($images as $image) {
//print the image to browser with anchor tag (Use if you want really :) )
echo '<p><img style="height:176px; width:221px; float:right;" src="'.$image.'" /></p>';
}
?>
I called it image.php and I used this form to make the user choose the directory
<form method="POST" action="image.php">
<input type="text" name="dir" placeholder="choose directory" />
<input type="submit" value="choose" />
</form>
when I run the code it output all files in the folder I have files like test.php and files as js and css and a file called images what happening is when I run it output the js and css and all .php file put it's not output the images what the error
That is extremely dangerous. Anyone can type whatever folder they want and immediately see (and in the case of PHP files, execute) all files in that directory.
Note the comment in your code about "only want JPEG or PNG", well you can do this:
$images = glob($dirname."*.{png,jpg,jpeg,gif}",GLOB_BRACE);
This will only allow image files of those types.
You should also be aware that glob is NOT recursive. You have to manually recurse through directories for that.
Or this, if you don't want to filter the extensions:
//Path to folder which contains images
$dirname = $_POST['dir'];
//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."/"."*");
//Display image using foreach loop
foreach($images as $image) {
//print the image to browser with anchor tag (Use if you want really :) )
echo '<p><img style="height:176px; width:221px; float:right;" src="'.$image.'" /></p>';
}
based on the original code in this question and Tims correction over there (answer 1) I'm trying to achieve the following:
I have a folder "img" containing images as follows:
image_123.jpg
image_123_alternate.jpg
image_456.jpg
image_456_alternate.jpg
image_789.jpg
image_789_alternate.jpg
and so on...
(Note: all images have the same size of 200px/200px)
When pulling from the "img" folder the simple HTML page should display images and filenames in the following fashion:
The actual images that contain the "_alternate" part in their filename (and only these) + the image filename not containing the "_alternate" part (without the file-extension) underneath the image in a textbox. All pairs should be pulled in an alphabetical order. In the example below bold capital letters indicate the actual image to be displayed:
IMAGE_123_ALTERNATE
textbox: "image_123"
IMAGE_456_ALTERNATE
textbox: "image_456"
IMAGE_789_ALTERNATE
textbox: "image_789"
This is what I have so far but this displays all images and filenames:
<?php
$dir = opendir("img");
while (($file = readdir($dir)) !== false)
{
echo "<a href='img/".$file."' target='_blank'> <img src='img/".$file."'width='200' height='200' /></a><br />";
echo "<textarea>$file</textarea><br /><br />";
}
closedir($dir);
?>
Any help would be much appreciated!
Thanks a lot!
This code should display a list of all images in alphabetical order (using PHP's GLOB function).
It then outputs a HTML image (leading to the "_alternate" image) and the image's filename (without the path, "_alternate" or "extension").
<?php
foreach (glob("img/*_alternate.jpg") as $file) {
$filename_parts = pathinfo($file);
$filename = $filename_parts['filename'];
$filename = str_replace("_alternate", "", $filename);
echo "<div>";
echo "<img src=\"/$file\" width=\"200\" height=\"200\" />";
echo "<p>$filename</p>";
}
?>
I have broken apart the functions that find the new filename and the parts that output the HTML code to make it easier to read and understand, you could merge these into one function if you wanted to.
Just through an if statement in your while loop. Check if the file name contains alternate or or not. If it doesn't contain alternate run your same code replacing the file extension with "_alternate.ext" while keeping the text area the same. If it does contain alternalte, do nothing. That way you are only processing half the files.
$files = new \DirectoryIterator('img'); // <- this should be the full absolute path!
$images = array();
// pick up jpg images that end in 'alternate'
foreach($files as $file){
$name = $file->getBasename('.jpg');
if(strripos($name, 'alternate') === (strlen($name) - 9))
$images[] = $name;
}
// sort them
sort($images, SORT_STRING);
// and do your thing here with $images...
var_dump($images);