I am using a simple script that displays images in a jquery slideshow – these image filenames are listed in a .txt file, and change depending on the page you are on (im also using pagination in another script).
If the filename that is listed in the .txt file doesn’t exist, I would like the image ‘unavailable.jpg’ to display instead…
The original script:
<?php
echo"
<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
if($item[0]==$fields[0]){
$photo=trim($item[1]);
echo"<img src='images/work/$photo' alt='' />\n";
}
}
echo"
</div>
</div>
"?>
And here is my try at it…but it doesn’t work properly- instead of the ‘unavailable.jpg’ image being displayed, it shows all of the images in the directory… :S Anyone have any ideas of what I might be doing wrong? :S
<?php
echo"
<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
$photo=trim($item[1]);
if (file_exists("images/work/".$photo)) {
echo"<img src='images/work/$photo' alt='' />\n";
}
else{
echo"<img src='images/work/unavailable.jpg' alt='' />\n";
}
}
echo"
</div>
</div>
"?>
Instead of all the images showing, I only want images for that page to display. Here is an example of my text file:
1|image1.jpg
1|image2.jpg
1|image3.jpg
2|image1.jpg
2|image2.jpg
The 1 and 2 are for the pages 1 and 2, and they display the images that are listed. This all works fine in the above original script that I have posted, but it seems to break when I add the if file_exists.
The path to your images uses a relative path. Are you sure the current working directory is the directory you think it is?
To verify do an
echo 'Current Working Directory: '.getcwd()."<br />\n"and verify what directory you are in.Its probably best to use a full file path to your image’s directory so the script can be placed anywhere on your server.
Now if that is correct then you need to check that your script has permission to your image directory. Typically php runs as
nobody:nobodyorapache:apachedepending on your configuration.The directories above as well as the files should have
644(-rw-r–r–) or at a minimum444permission (-r–r–r–).Try these two things and let us know if that solved your specific problem or not; I hope it does.