bensonang Posted March 5, 2007 Share Posted March 5, 2007 the code below displays all images located in the current directory. my problem is, if i specify a directory name to $filedir, example $filedir = "pictures"; the only thing that would display are blank boxes with an X (i think it means unloaded picture). anyone had this kind of problem before? need suggestions please. thanks. <?php // Set $filedir to '.' if in current directory $filedir = "."; $handle = opendir($filedir); echo $filedir; while ($filename = readdir($handle)) { // One way to get the file extension $ext = strtolower(substr(strrchr($filename, '.'),1)); if ($ext == 'jpeg' || $ext == 'jpg') { echo "<img src='$filename'>"; } } closedir($handle); ?> note: that pictures folder is located in the same directory as the php file. Link to comment https://forums.phpfreaks.com/topic/41218-need-help-please-open-directory-of-images-but-images-wont-display/ Share on other sites More sharing options...
HaLo2FrEeEk Posted March 5, 2007 Share Posted March 5, 2007 Remember that you need to put the slashes in the path, otherwise you would get something like this for your url: http://example.com/picturepicture1.jpg when really you want this: http://example.com/picture/picture1.jpg Make sense? Also, for browsing current directory, you should use ./, and not just . So if the folder you wanted to browse was called pictures, use this for $filedir: $filedir = './pictures/'; And finally, I didn't test your way of getting the extension, but this is the one I wrote: $filetype = substr(strtolower($file), strrpos($file, ".")+1, strlen($file)); this gets everything in the now lowercase name called $file, after the last occurance of a dot in the filename, and up to the length of the file. Link to comment https://forums.phpfreaks.com/topic/41218-need-help-please-open-directory-of-images-but-images-wont-display/#findComment-199651 Share on other sites More sharing options...
bensonang Posted March 5, 2007 Author Share Posted March 5, 2007 Remember that you need to put the slashes in the path, otherwise you would get something like this for your url: http://example.com/picturepicture1.jpg when really you want this: http://example.com/picture/picture1.jpg Make sense? Also, for browsing current directory, you should use ./, and not just . So if the folder you wanted to browse was called pictures, use this for $filedir: $filedir = './pictures/'; yep, however i tried it already and it still wont work. i'v tried using $filedir = "./pictures/"; $filedir="pictures/"; and i get the same results. the output shows white boxes with corresponding size to the images in the pictures folder with a red x mark. however, i noticed that if the pictures folder is empty, the screen would remain blank. does this mean the script really read the dir? if i do use $filedir = "."; and put images in the same directory as the script, the output is correct (images displayed and all) im pretty confused right now. ??? ??? Link to comment https://forums.phpfreaks.com/topic/41218-need-help-please-open-directory-of-images-but-images-wont-display/#findComment-199658 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 12, 2007 Share Posted March 12, 2007 I'm working on it, will edit, hopefully. Try this: <?php $path = "./pictures/"; $handle = opendir($path); while(($file = readdir($handle)) !== FALSE) { $ext = substr(strtolower($file), strrpos($file, ".")+1, strlen($file)); $filename = substr($file, 0, strrpos($file, ".")); $filename = str_replace(' ', '%20', $filename); if ($ext == 'jpeg' || $ext == 'jpg') { echo "<img src=" . $path . "" . $filename . "." . $ext . "><br />"; } } closedir($handle); ?> The only problem I found was when there are spaces in the filename, for some reason, replacing them with %20 doesn't work, idk why, but other than that, as long as your foldername doesn't have a space, you'll be fine. Link to comment https://forums.phpfreaks.com/topic/41218-need-help-please-open-directory-of-images-but-images-wont-display/#findComment-205788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.