Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to read images in a directory.

Status
Not open for further replies.

Chelsea7

Programmer
Aug 25, 2008
69
US
Hi,

I'm trying to read the image files such as Jpeg in another directory on a server. The code below will locate and list the files but not the image. This only works if this file is in the same directory as the images. This $mydir="/upload/"; is in another directory. This file is in a subdirectory called /main/ and the /upload/ directory is the subdirectory of the root.

I have a separate directory only for the images. Here's the code;

<?
$mydir="/upload/";
if ($handle = opendir($mydir)) {

echo "Files:\n <table><tr><td>Delete</td><td>Filename</td></tr>";


while (false !== ($file = readdir($handle))) {
if($file!="." && $file!="..")

echo "<tr class='mycell'><td><input type='checkbox' name='files[]' value='$files'></td><td><img src='$files'></td></tr>";


}

echo "</table>";

closedir($handle);
}


?>


Again, it sees the files and gives me a list of the names but does not display the images associated with these files.

What am I missing?
Thanks.
 
in your checkboxes, do not rely on the value being passed in a form other than true/false. not all browsers honour this. it is better to be specific about the element name and check the keys of the returned arrays.

otherwise your main problem was that your src attribute was set to $files (as was the value attribute of the checkbox) and you are not using the variable $files (but $file). also you are not providing the path to the file. you need to prepend the $mydir variable.

this code should work although I have not tested it.

Code:
<?php
$mydir="/upload/";
if ($handle = opendir($mydir)) {
	echo "Files:\n <table><tr><td>Delete</td><td>Filename</td></tr>";
		while (false !== ($file = readdir($handle))) {
			if($file!="." && $file!=".." && !is_dir($mydir . $file)){
				$data = getimagesize($mydir.$file);
				if (!$data){
					//do nothing
					//not a proper image file
				} else {
					echo <<<HTML
					
					<tr class='mycell'>
						<td>
							<input type='checkbox' name='files[$file]' value='on'>
						</td>
						<td>
							<img src='$mydir.$files' {$data[3]}>
						</td>
					</tr>
HTML;
				}
			}

		}
}
echo "\r\n</table>";

closedir($handle);

?>
 
Thanks. But it still does not display the image. It shows the outline or tracing of the image but it does not show it. If I right click it, it then shows those finds in the directory but not the actual image. I'm using Fire Fox version 3.0.15.
 
then there is probably a difference between the webroot and the fileurl you are providing for the img src. remember you are putting the images in the upload directory in the ROOT of the filesystem
Code:
'/upload/';
it is very rare that a webserver process would have the filesystem root as the document root (not to mention almost certainly a massive security hole)

make sure that the upload directory is set relative to the current working directory, or as an absolute. and provide separately a web url to the upload directory. call the weburl $webURL and change this line as follows:
Code:
<img src='$webURL.$files' {$data[3]}>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top