PHP pull specific sets of images from a directory - php

I'm pulling an image from a directory (images) and placing it in an html table row along with my mysql query. PHP finds the image as the mysql DB id matches the image name. i.e. if id = 12 then 12.jpg is pulled from directory using $imgnum in the path.
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$imgnum = $row["id"];
?>
<tr>
<td>
<img src="images/<?php echo $imgnum ?>.jpg"/>
The issue is there may be a series of images all associated with that row, i.e. 1-1.jpg 1-2.jpg and so on, sometimes there may be only one and the code above works fine.
Is there a way to output all images with the name equalling the id but with a dash afterwards so it picks up other associated images? Even if someone can tell me what to learn to achieve this?
cheers

You can use php's glob() to find similar files. Something along the lines of this:
foreach (glob("/path/to/images/folder/" . $imgnum . '-*') as $filename) {
// do something with $filename
}

Thought I'd post what I used from the help above. It now outputs a list of all of the images I needed.
<?php
$imgnum = $id;
foreach (glob("images/" . $imgnum . '-*') as $filename) {
echo "<a href=''><img src='$filename' class='imgsize'></a> <br>";
}
?>

Related

Images are not being displayed on php

I have a txt file that has in it a painting id. This painting id is the same as the name of the image. For example in the txt file the painting id is 00000 and the name of the images in the path I wrote in my echo statement is 00000.jpg.
What I'm trying to do is to display all the images using this for loop and echo statement. But for some reason the images are not displaying. Whats displaying instead is thats what is being displayed instead of the images. Can anyone help me solve this problem please?
<?php
$paintings = file('paintings.txt') or die('ERROR: Cannot find file');
$delimiter = '~';
foreach ($paintings as $painting){
$paintingFields = explode($delimiter, $painting);
$painting_id = $paintingFields[0];
}
for($i=0;$i<count($paintings); $i++){
echo "<img src='resources/paintings/square-medium/%s.jpg' . $painting_id></br>";
}
?>
You're using echo wrong. If you view the source of your generated page you'll see the literal '%s' in the code.
To use '%s' as a format string, use sprintf():
echo sprintf("<img src='resources/paintings/square-medium/$s.jpg'></br>", $painting_id);
Or:
echo "<img src='resources/paintings/square-medium/" . $painting_id . ".jpg'></br>";
Even better, just do the ouputting in PHP mode:
for($i=0;$i<count($paintings); $i++){
?>
<img src="resources/paintings/square-medium/<?= $painting_id ?>.jpg"><br>
<?php
}

Im new to php, how can I use links to change what loads on a page?

What I was looking for before was a way to dynamically create links for the same directory but have different URL variables, that way the same page works from the variables provided to load images from a folder. Below was my example code for loading all of the images inside the directory.
$dir = "./Ch.001/";
$a = glob($dir."*png");
foreach($a as $image){
echo "<img src='". $image ."'>";
}
?>
At the time I wasn't sure what to research to achieve what I wanted but thanks to an answer and some more experience in PHP, my answer to what I was looking for above would have been $_GET variables and urlencode()/htmlspecialchars().
Now I would use a database to store all of the information I wanted to display, so when the series is created from a form I would store the title, description, etc, and then calculate how many rows are in the database, and then make that number + 1 the file name, and maybe add a prefix so it isn't just numbers.
Presuming 001 is 001 to n etc.
Use a simple $_GET parameter, check or cast to int, 0 pad it and then show your links with +1 for next.
<?php
$chapter = str_pad((int) ($_GET['chapter'] ?? '001'), 3, "0", STR_PAD_LEFT);
//
$dir = "./Ch.$chapter/";
//
$images = glob($dir."*png");
if (empty($images)) {
echo 'Go back';
} else {
foreach($images as $image){
echo "<img src='". $image ."'>";
}
echo 'Chapter: '.$chapter.PHP_EOL;
echo 'Goto Chapter';
}

Pulling an image from directory based on file name?

I'm trying to pull an image from a directory based on the file name matching the id number given in the URL. (ie: php?id=1 being the same as 1.png)
I've tried several different methods, and while I'm not getting any errors, I'm still getting no image that shows up when I type in the id tag.
Here's the most recent version of the code I've been working with:
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$img = $id;
foreach(glob("gallery3/var/albums/" . $img) as $filename)
?>
<img src = " <?php echo $filename; ?> " />
I'm at a loss. I've tried everything I can find and nothing seems to be working. I really don't want to pull the images from the database, and would prefer to pull them straight from the directory so that future uploaded images will be accessible directly from their id tag.
I think you're over-complicating it. All you need is this
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$img = $id;
?>
<img src="gallery3/var/albums/<?php echo $img; ?>.png" />
You could also go for:
<?php
if(isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {
echo "<img src='gallery3/var/albums/".$_GET['id'].".png'/>";
}
else {
echo "Not Valid!";
}
?>
In the else statement at the bottom you could implement some form of error checking to present to the user in the event that a non valid input is received.

PHP foreach loop for all images in a HTML 3 table column

Hi I'm a newbie with PHP and this site, so please be nice :)
I'm currently having trouble working the below PHP foreach code out as I'm trying to echo all images in a HTML table 3 column but it echo's with 2 only.
UPDATE: I've managed to fix some issues thanks to the comments guy's, thank you. However, I', now experiencing another issue which is confusing.
Basically, If I have one picture in a folder, it will echo that one picture, but If I put two pictures there, it echo's out with 4, 1 first picture echo's with 2 and the second is with 2 as well. Basically showing 4 images even though I have 2 images in that folder. I can't seem to fix this..
Here's the code:
<?php
// get images
$images = glob($imagedir.'/' . "*.png");
$i = 0;
echo'<table><tr>';
foreach($images as $image)
{
$i++;
echo '<td><img src="'.$image.'" height="200"></td>';
if($i == 3)
{
echo '</tr><tr>';
$i = 0;
}
}
echo '</tr></table>';
?>
Thanks in Advance
You're code for rendering the HTML is just fine.
If you have duplicates, the content of your imagedir must be wrong.
A few remarks:
glob($imagedir.'/' . "*.png"); also include directories which names end as .png.
Depending on the amount of images, the last table row will not be completely filled with table cells.
It's good practice not using the php closing tag ?> at the end a the php file.
I've altered you code to avoid above to problems.
I'm sure there more/other ways to do this, but this came in mind first.
<?php
$imagedir = 'images';
//Get *.png files only
$images = array_filter(glob("$imagedir/*.png"), 'is_file');
//Make image array divisble by 3 (columns)
while (count($images) % 3 != 0) {
$images[] = '';
}
echo'<table><tr>';
for ($i = 1; $i <= count($images); $i++) {
//Render TD if $image is not empty
if ($images[$i-1] != '' != '') {
echo '<td><img src="' . $images[$i-1] . '">', "<br>Image $i</td>";
}
//Close table row after 3 TD's
if($i % 3 == 0)
{
echo '</tr><tr>';
}
}
echo '</tr></table>';

php to display file information and download link if files are present - How to adjust for multiple files

I have a piece of php that if a specified folder has a file in, it will display the filename, date created and present a download button, if the folder is empty it will show nothing. This works very well but if I have more than one file in the folder it bunches all the filenames together - what I want is the separate information displayed for every file.
To help you understand the problem here is an image showing the problem and the code. I got very far on my own but its way above my head, I just cant see a simple way to correct the problem. The code may look very awkward and odd as I'm totally new at this but it looks visually right on the browser. I would really appreciate any help thank you.
Here is an image of the problem: http://i46.tinypic.com/m79cvs.png
<?php if (!empty($thelist)) { ?>
<p class="style12"><u>Fix</u></p>
<p class="style12"><?=$thelist?><?php echo " - " ?> <?php $filename = '../../customers/client1/client1.fix.exe';
if (file_exists($filename)) {
echo "" . date ("m/d/Y", filemtime($filename));
}
?> <?php echo " - <a href='download.php?f=client1/client1.fix.exe'><b>Download</b></a> <a href='download.php?f=client1/client1.fix.exe'>
<img src='../css/images/dlico.png' alt='download' width='35' height='32' align='absmiddle' /></a>" ?>
</p>
<?php } ?>
The list ($thelist) contains your files, yes?
You are not working on the $thelist, but on the $filename which is a hardcoded string.
Why? Currently you are outputting <?=$thelist?> and it looks like concatenated string from filenames. I would suggest that $thelist should be something like an array of your files. Then you could iterate over the files and output html dynamically for each entry.
<?php
// define your directory here
$directory = xy;
// fetches all executable files in that directory and loop over each
foreach(glob($directory.'/*.exe') as $file) {
// output each name and mtime
echo $file . '-' . date ("m/d/Y", filemtime($file));
// or you might also build links dynamically
// $directory needs to be added here
echo ''.$file.' - Size: '.filesize($file).'';
}
?>

Categories