464,813 Members | 965 Online
Need help? Post your question and get tips & solutions from a community of 464,813 IT Pros & Developers. It's quick & easy.

Displaying Images form a Directory

 P: n/a Happy New Year, Everyone! I am trying to figure out how to display a bunch of images (mainly JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a local directory on the system. I can do this with the glob() function, but I can't seem to put in a directory other than one within the webroot. For example, I can only put "/uploads" and not "/Volumes/jray/Pictures...". Any ideas how to get around this? If I can't use the glob function, it's fine, but I only want images to be displayed (and not any other file that happpens to be stored in that directory). Thanks in advance! Jan 1 '06 #1
15 Replies

 P: n/a Jan 1 '06 #2

 P: n/a Thanks for the fast response. What I'm trying to do is actually display the images, not list them. What I've got so far (sorry for not posting this earlier) is:

"; } echo "
"; foreach(glob("uploads/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}", GLOB_BRACE) as $images) { echo ""; ?> It works just fine if I want to view images in the "uploads/" folder (which is parallel to this script), but if I want to change it to be "/Volumes/jray/Pictures" it won't work. witkey wrote: Jan 2 '06 #3  P: n/a Well, if the images aren't within the webroot, then the web server won't serve them... Jan 2 '06 #4  P: n/a "Jameson" wrote: Thanks for the fast response. What I'm trying to do is actually display the images, not list them. What I've got so far (sorry for not posting this earlier) is: Use witkey's suggestion with a modification? ' . "\n"; } ?> Jim Carlock Post replies to the newsgroup. Jan 2 '06 #5  P: n/a Chung Leong wrote: Well, if the images aren't within the webroot, then the web server won't serve them... Not true. You can serve them through PHP, e.g. And showimg.php can be something like: header('Content-type: image/gif'); header('Content-length: '.filesize($img_filename)); $file_pointer = fopen('/some/other/directory/img.gif', 'rb'); fpassthru($file_pointer); fclose($file_pointer); The graphic can reside anywhere on the system which is accessible to the Apache process (not just under DocumentRoot). -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. js*******@attglobal.net ================== Jan 2 '06 #6  P: n/a Hi Jim: Thanks for the suggestion. I tried it out, but still got a blank screen. I think Chung might be right, because if I try " wrote: Thanks for the fast response. What I'm trying to do is actually display the images, not list them. What I've got so far (sorry for not posting this earlier) is: Use witkey's suggestion with a modification? ' . "\n"; } ?> Jim Carlock Post replies to the newsgroup. Jan 2 '06 #7  P: n/a Hi Jameson, I feel like I'm talking to a son... (j.k) I didn't test the witkey's code. It appears that filetype() returns "file" or "dir". Try the following. ' . "\n"; } while($file = readdir($dh)); } } closedir($dh); ?> That should work I think. It's air code. I tested it out here but I'm running into a problem where the mime_content_type is a problematic function right at the moment. I read that the glob function is supposed to be a better alternative in the PHP manual. And if you know that all the files in the folder are jpg's then you won't have to do the mime_content_type test. My apologies about not testing witkey's code. Hope this helps. If anyone knows how to get the php_mime_magic.dll file to work on a Windows system, I could use the help. I uncommented the line in php.ini... extension=php_mime_magic.dll and put the php_mime_magic.dll in the system32 folder. Then I stopped apache and restarted apache... net stop apache net start apache Thanks, much. Jim Carlock Post replies to the newsgroup. Jan 3 '06 #8

 P: n/a Hi Jim: Thanks for the new code. I think we're getting closer, but it still doesn't seem to be working. I added the error reporting code below to the file, but it doesn't tell me that anything is wrong. It's strange, because I don't even see an icon from the browser saying that it can't find the image. Do you think I could be missing some component of my PHP installation? I know the directory path is fine, because I can get to it right from the terminal on the computer. ini_set("display_errors",true); error_reporting(E_ALL ); Any more ideas? Do you think if I used the glob() function within the opendir() function, it might work? Thanks again for the help. I really appreciate it! Jan 3 '06 #9

 P: n/a "Jameson" wrote: Thanks for the new code. I think we're getting closer, but it still doesn't seem to be working. Hi Ray, Are you working in the MacIntosh environment? I'm working on with a PC. On the PC, the php.ini file typically gets found in the system32 folder. I'm not sure how things work on a MacIntosh, so perhaps you can explain the php.ini concepts relating to MacIntosh (if that is the case) for my benefit. The php stuff is very new to me. I did get the following code to work here, using glob(). I really like this better than having to do mime_content_type stuff. And so much in the PC and unix world seem very much related, using file extensions to denote the contents of the file. List of Files

\n"; echo "filename: $file, filetype: " . filetype($file) . "
\n"; echo '
' . "\n"; } ?>

I added the error reporting code below to the file, but it doesn't tell me that anything is wrong. It's strange, because I don't even see an icon from the browser saying that it can't find the image. When you view the page through the browser, take a look at the source code (the HTML output). The code I posted previously seems to have had a small bug in one of the lines, and I gave up testing it when I couldn't get the php_mime_magic.dll to provide the php_mime_content() function. So I don't know how the Mac environment works, nor the Unix environment. The file name could possibly be of a different extension in each environment. If anyone else here can provide a little help here that would be great. Do you think I could be missing some component of my PHP installation? I know the directory path is fine, because I can get to it right from the terminal on the computer. ini_set("display_errors",true); error_reporting(E_ALL ); Any more ideas? Do you think if I used the glob() function within the opendir() function, it might work? glob() is alot easier... there's no need for opendir() and readdir() when using the glob function. I just tested the following out and it works quite well. \n"; echo "filename: $file, filetype: " . filetype($file) . "
\n"; echo '
' . "\n"; } ?> Thanks again for the help. I really appreciate it! You're welcome. I'm learning as well. So thanks back at you! My mime_content_type() function is failing I think, because the directory path for the php install folder is set to C:\php4 and php isn't installed there. So I guess I need to find the source code for that particular DLL and modify the source code. If anyone knows where to pick up the source code for the php_mime_magic.dll feel free to leave a hint. It's a shame (for me and others) that the people that compiled it used an absolute path inside the DLL file. Jim Carlock Post replies to the newsgroup. Jan 3 '06 #10

 P: n/a Hello again, I finally got the mime_magic stuff to work. I ended up editing php.ini to get it to work. The following line was commented out so all I did was uncomment it. extension=php_mime_magic.dll Also, I had to add a section with the following item: [mime_magic] mime_magic.magicfile = "C:\path\to\PHP\install\extras\magic.mime" The following code works very well, but I still like the glob() function more. // view the mime type $rpn = "23939.jpg"; echo mime_content_type($rpn); returns "image/jpeg". Also, if the file is named 23939 without the .jpg extension, mime_content_type('23939'); also returns "image/jpeg". I tested dropping the extension on a .gif file as well and it correctly returned, "image/gif". Hope this helps. Jim Carlock Post replies to the newsgroup. Jan 4 '06 #11

 P: n/a header ( 'Content-Type: ' . image_type_to_mime_type ( $img ) ); readfile ($img ); $img is the relative path to the image file. Let's say you use this tree: /directory_for_everything /directory_for_everything/directoryforstoringimages /directory_for_everything/directoryhttpexposed If your script is stored in the last directory, then$img = '../directoryforstoringimages/nameofimage'; Hope it helps. feo "Jameson" escribió en el mensaje news:11**********************@g44g2000cwa.googlegr oups.com... Happy New Year, Everyone! I am trying to figure out how to display a bunch of images (mainly JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a local directory on the system. I can do this with the glob() function, but I can't seem to put in a directory other than one within the webroot. For example, I can only put "/uploads" and not "/Volumes/jray/Pictures...". Any ideas how to get around this? If I can't use the glob function, it's fine, but I only want images to be displayed (and not any other file that happpens to be stored in that directory). Thanks in advance! Jan 4 '06 #12

 P: n/a Hi Jim: Thanks a million for all of the help. I think my PHP installation isn't set up right, because it seems like anything that I try do do with images doesn't work very well. I might try making a symbolic link (like a shortcut on Windows) in the directory. I know it isn't as ideal as if I could get the directory parth to wrok with the glob() function, but it might be all I've got at this point. I'm glad you got your mime_content_type to work. Sometimes we get lucky and it turns out to be as simple as uncommenting a line. Thanks again for all of the help! Jameson Jim Carlock wrote: Hello again, I finally got the mime_magic stuff to work. I ended up editing php.ini to get it to work. The following line was commented out so all I did was uncomment it. extension=php_mime_magic.dll Also, I had to add a section with the following item: [mime_magic] mime_magic.magicfile = "C:\path\to\PHP\install\extras\magic.mime" The following code works very well, but I still like the glob() function more. // view the mime type $rpn = "23939.jpg"; echo mime_content_type($rpn); returns "image/jpeg". Also, if the file is named 23939 without the .jpg extension, mime_content_type('23939'); also returns "image/jpeg". I tested dropping the extension on a .gif file as well and it correctly returned, "image/gif". Hope this helps. Jim Carlock Post replies to the newsgroup. Jan 5 '06 #13

 P: n/a Hi Jim: Sure, I'd be happy to keep looking into this. Maybe we'll end up getting it to work. First off, I am doing this on a Mac. Apache comes built in to OS X (I believe as a service), and I installed PHP from an install package. There is a PHP ini file on the Mac, although I've never had to edit it. I used to run a Windows 2000 server with PHP on it, and I had to edit the PHP.ini file quite a few times. Restarting Apache on the Mac can either be done through the command line or System Preferences. I tend to do it from the GUI, but I think that "apachectl restart" will restart it from the command line. There was actually something that I was wondering about your last version of the script. If we take a look at it... \n"; echo "filename: $file, filetype: " . filetype($file) . "
\n"; echo '
' . "\n"; } ?> ....Let's say we set the directory to $dir = '/Users/jray/Pictures'; (a path that I can browse to just fine from the command line). Where is the$dir variable used in the rest of the script? If a directory is specified in the glob() function, I believe it just defaults to the directory that the script is in. Did you have a different experience on your end? Thanks again for sticking with this. Sometimes is is good to have an understanding of how a function works across different operating systems. Jameson Jan 6 '06 #15

 P: n/a "Jameson" wrote: If we take a look at it... \n"; echo "filename: $file, filetype: " . filetype($file) . "
\n"; echo '
' . "\n"; } ?> ...Let's say we set the directory to $dir = '/Users/jray/Pictures'; (a path that I can browse to just fine from the command line). Where is the$dir variable used in the rest of the script? I used the $dir in one of the scripts and forgot to take it out this one, or perhaps left it on purpose (unknown reason). :-) The single dot indicates the current directory, two dots indicate the parent directory of the current directory.$dir = ".."; You could also use it as the path to the folder you want to parse and applie the file exstension inside of it... $dir = "../*.jpg"; If a directory is specified in the glob() function, I believe it just defaults to the directory that the script is in. That's exactly the way I see it. No difference. I find it much better to use relative paths right at the moment, using "../images/*.jpg" to get to the folder in question, rather than absolute paths, "/images/*.jpg" The script would read... \n"; echo "filename:$file, filetype: " . filetype(\$file) . "
\n"; echo '
' . "\n"; } ?> As long as the web-root is set up properly, the "absolute path" works fine on a website... let your root path be '/Users/jray/' and the php file resides in, '/php', the images reside in, '/images/'. So a php file in the php folder could reference image files by using '../images/img.jpg' or '/images/img.jpg'. If you employed Explorer on a Windows system, you could change from the currently viewed folder to another folder by typing in ..\WINDOWS, or "..\Program Files\", or ...\WINDOWS\system32\ into the address bar. Someone had a patent or some such on using the slash to denote paths. Thus Microsoft patented the backslash concept (don't quote me though as I can't identify the source read years ago and maybe my mind plays tricks on me). It works the same way on Unix systems but you change the backslashes to forward slashes. I imagine it works similar on a Mac, but I'll let someone with Mac experience confirm it. Let me know. Thanks again for sticking with this. Sometimes is is good to have an understanding of how a function works across different operating systems. Yes, especially when it all jives together and works in the same or similar manners. Hope that helps. Jim Carlock Post replies to the newsgroup. Jan 6 '06 #16