I am trying to echo out all of the images in a folder directory with a couple of exceptions/ignores.
This is working ok apart from it also echoes out a blank photo for every photo it echoes out?
why is this happening can someone please show me where I'm going wrong thanks.
<?php
$dirname = "./data/photos/".$profile_id."/";
$images = scandir($dirname);
$ignore = Array("_cover.jpg", "_default.jpg");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
};
}
?>
You appear to have an extra slash after your image source:
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
//--------------------------------------------------------^ here
This may be interefering with how the browser parses the DOM and causing an extra image to appear.
Also, small suggestion, try using this line instead:
echo "<img src='".$dirname.$curimg."' class=\"profile_photos\"><br>\n";
You should ensure that $curimg is actually a jpg file:
if(!in_array($curimg, $ignore) && preg_match("/\.jpg$/i", $curimg)) {
scandir returns not just files but subdirectories, including . and ...
Related
The code I have should output a jpg from a list of files in a directory however it is not. I have trawled this site and tried different methods but not helped. I am a relative beginner at php so looking for any help at all.
I have tried using img src in the php code but I am trying to get the image to display within a Wordpress post so I cannot echo the img src within the script. I have tried file_get_contents and read file as well but it may be my lack of knowledge holding me back.
<?php
$imagepath = htmlspecialchars($_GET["image"]);
$imagenum = htmlspecialchars($_GET["num"]);
define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );
If(LOCALHOST){
define('PATH_IMAGES', 'this_path');
}else{
define('PATH_IMAGES', '../../../Images/');
}
$arrnum = $GLOBALS[imagenum] - 1;
$dirname = PATH_IMAGES . $GLOBALS[imagepath]."/";
$images = scandir($dirname);
rsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
header('Content-type: image/jpeg');
file_get_contents('$dirname$images[$arrnum]');
}
}
?>
Have you tried readfile(...); should read and output the file. In your example you are not outputting the image data
http://php.net/manual/en/function.readfile.php
How can I get all audio files from a folder and display it in Audio Tags?
What I Have Tried
$sPath = 'music/favorite/*.mp3';
foreach (glob($sPath) AS $mp3) {
print $mp3 . PHP_EOL;
}
You can do it inside of the foreach. If you just want to echo the files, just do it like this:
foreach (glob($sPath) AS $mp3) {
echo '<audio>';
echo '<source src="'.$mp3.'" type="audio/mpeg">';
echo '</audio>';
}
Maybe you need to adjust the path of your $mp3 var.
It's just a sample. Please don't blame me because I didn't tested this.
Hope this helps.
I am trying to load image dynamically using DirectoryIterator. The problem is that the images are getting loaded as expected in google chrome and IE, but in firefox nothing shows up.
Below is the code that I am working on.
$dir = new DirectoryIterator(getcwd()."\emoticons");
foreach($dir as $folder)
{
if(strlen($folder) > 2) // sometimes $folder is blank so.
{
$files = new DirectoryIterator(getcwd()."\emoticons\\".$folder);
echo "<br>".$folder;
foreach($files as $file)
{
echo "<img src = 'emoticons\\".$folder."\\".$file."'></img>"; // doesnt work in FF.
}
}
}
I dont know why this could possibly be happening. If anyone has any experience around this problem then please share.
Regards,
Sameeksha.
Please replace '\' by '/'
Here is the new code:
$dir = new DirectoryIterator(getcwd()."/emoticons");
foreach($dir as $folder)
{
if(strlen($folder) > 2) // sometimes $folder is blank so.
{
$files = new DirectoryIterator(getcwd()."/emoticons/".$folder);
echo "<br>".$folder;
foreach($files as $file)
{
echo "<img src = 'emoticons/".$folder."/".$file."'></img>"; // doesnt work in FF.
}
}
}
Have you got adBlock in your FF ? Sometime adBlock auto hide banner.
Or you can use Firebug plugin to check the image path.
I know it's accessing the directory because the correct number of broken icons are there. Even alt='Image not there' doesn't work, it only widens the element its in but doesn't show any text. I tried other scripts where it displayed the alt='Image not there' but still gave a broken image icon. I also tried to simply echo an image from the folder and still got a broken image icon so what am I doing wrong? Is it even a script thing or what is going on?
<?php
$dirname = "C:/uploads/";
$images = scandir($dirname);
shuffle($images);
$ignore = Array(".", "..");
foreach ($images as $curimg) {
if (!in_array($curimg, $ignore)) {
echo "<li><a href='".$dirname.$curimg."'>
<img src='img.php?src=".$dirname.$curimg."&w=300&zc=1' alt='Image not working' /></a></li>\n ";
}
}
?>
You should provide an URL and not a local path here:
echo "<li><a href='".$dirname.$curimg."'>
<img src='img.php?src=".$dirname.$curimg."&w=300&zc=1' alt='Image not working' /></a>
$dirname is not accessable to the outside world and should be something like:
www.my.domain.com/uploads/
You need to create a folder into your (if you are working in local machine)local server site folder. Because PHP is a server side scripting language. So only the files move into your folder correctly. then you can simply call
<?php
$dirname = "uploads/";
$images = scandir($dirname);
shuffle($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li><a href='".$dirname.$curimg."'>
<img src='img.php?src=".$dirname.$curimg."&w=300&zc=1' alt='Image not working' /></a></li>\n ";
}
}
?>
if you call images from local machine its not a logic. because you are building website. this website not show only your system. it shows all system. also all systems are not contain a uploads folder.
I have a script that scans a directory of thumbnails and echoes them to the page. It works nicely, but the thumbnails are not clickable, and i would really like this to be the case. echo "<img src='$thumbnail' class='resizesmall'>"; is the line where the thumbnails are echoed. I'm not sure how to write the path to the larger image inside the php without breaking it. Maybe this should be done inside the foreach statement? thanks for your help?
$dir = "../mysite/thumbnails/";
$dh = opendir($dir);
// echo "$dh";
$gallery = array();
while($filename = readdir($dh))
{
$filepath = $dir.$filename;
//pregmatch used to be ereg
if (is_file($filepath) and preg_match("/\.png/",$filename))
{
$gallery[] = $filepath;
}
}
sort($gallery);
foreach($gallery as $thumbnail)
{
echo "<img src='$thumbnail' class='resizesmall'>";
}
?>
</div>
<??>
The easiest way would be to setup a situation where your thumbs and your full size images were named the same. So you may have thumbs/image1.png and full/image1.png. Then instead of using $thumbnail use a variable $image, or something similar just so the code reads better. You'll also want to leave the $filepath out of the mix so that $image ends up as just the file name.
foreach($gallery as $image)
{
echo "<a href='full/$image'><img src='thumb/$image' class='resizesmall'></a>";
}
You may want to throw in some checks to make sure there is a matching image just to prevent errors or bad UX. However, the code above should work.