Jump to content

Image Directory


garyblackpool
 Share

Recommended Posts

hi i am trying to get the images from a directory, as far as i can tell the code is correct and shomd is set to 777 on both the php file and the directory.Please could you help? Thanks

<html><head><title> imageIndex</title><head><body><?//image index//generate an index image containing all images in a particular directory//point to whatever directory you wish to index//index will be written to this directory as imageIndex.html$dirName = " /learning/ ";$dp = opendir($dirName);chdir($dirName);//add all files in directory to the $theFiles arraywhile ($currentFile !==false) {	$currenfile = readDir($dp);	$currentFile = readDir($dp);	$theFiles[] = $currentFile;} //end wile// extrac gif and gpg images$imageFiles = preg_grep("/jpg$|gif$/", $theFiles);$ourput ="";foreach ($imageFiles as $currentFile){	$ourput .= <<<HERE	<a href =$currentFile>	<img src = "$currentFile"	height = 50	width = 50>	</a>HERE;} //endforeach//save the index to the local file system$fp = fopen ("imageIndex.html", "w");fputs ($fp, $output);fclose($fp);//readFile("imageIndex.html");print "<a href = $dirName/imageInde.html>image index</a>\n";?></body></html>	

Warning: opendir( /learning/ ) [function.opendir]: failed to open dir: No such file or directory in /home/gary/public_html/Personal/getimage.php on line 13Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /home/gary/public_html/Personal/getimage.php on line 14Warning: readdir(): supplied argument is not a valid Directory resource in /home/gary/public_html/Personal/getimage.php on line 18Warning: readdir(): supplied argument is not a valid Directory resource in /home/gary/public_html/Personal/getimage.php on line 19Warning: fopen(imageIndex.html) [function.fopen]: failed to open stream: Permission denied in /home/gary/public_html/Personal/getimage.php on line 39Warning: fclose(): supplied argument is not a valid stream resource in /home/gary/public_html/Personal/getimage.php on line 41image index

Link to comment
Share on other sites

Your server can't find the directory. What's with the spaces in " /learning/ " ? The first error message looks like PHP is in fact looking for something that matches the spaces.When you get that straightened out, you can greatly simply your code using the glob() function. http://us2.php.net/manual/en/function.glob.php

Link to comment
Share on other sites

Well, there are other problems. Your while loop is one of them:while ($currentFile !==false) {You will never enter the loop with this, since $currentFile is not initiated, and has no value at all, when the loop first tests it. (In fact, I'm surprised it doesn't generate a warning for that reason.) You would want a do...while loop instead. More common is to put the readdir statement inside the test, like so: while ($file = readdir($dir) !== false) {//etcThe simplest of all would be the glob statement I told you about:$dir = "mydir";chdir ($dir);$files = glob("*.{jpg,gif}", GLOB_BRACE);Now the $files array contains the names of your gifs and jpgs, and we didn't have to loop, didn't have to test, etc. I admit the syntax is not obvious, and the manual should have an example that uses the braces.This is also a problem:<a href =$currentFile>Needs to be:<a href ="$currentFile">so the result is correct HTML.

Edited by Deirdre's Dad
Link to comment
Share on other sites

The code was very close. We learn by making mistakes and working them out. Don't feel bad if it doesn't come out perfect the first time. Programmers spend most of their time debugging and improving.Don't worry about using the readdir() technique. For some applications, many programmers continue to use a while loop for grabbing files in a directory, as you can see from this manual explanation: http://us2.php.net/manual/en/function.readdir.php . It's perfectly correct.And it's probably good to master the basics first, anyway. readdir() is a old technique that corresponds to a traditional Unix system command. It's what lies under the hood of something like glob(). Knowing how that works helps you understand the big picture. Functions like glob() are what we call high-level. This means that they exist for your convenience, but what's going on is really just a combination of lower-level statements. (I'm oversimplifying a bit, but it's close enough.)Your use of preg_grep looks correct, and it's a good thing to experiment with, since regular expressions are difficult to master but an integral part of serious programming.Your heredoc technique also looks correct, and from what I gather it was in fact outputting. That's another technique that's good to know, since it outputs well-formatted HTML without using escape characters, and it's handy for interpolating a lot of variables with having to escape a lot of quotation marks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...