SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
Thread: Seperate directory reading
-
Apr 21, 2009, 17:38 #1
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Seperate directory reading
Good day to you all,
I was wondering how to seperate the result of a direcory read.
Example, I know the files will be images.
How can I show the portrait first and then landscape second ?
here is my code :
PHP Code:
<html>
<head>
<SCRIPT language="JavaScript">
<!--
if (document.images)
{
preload_image = new Image(25,25);
<?PHP
// directory name sent by url
$dir = $_GET['dir'];
$file = $_GET['file'];
//define the path as relative
$path = $dir."".$file;
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "preload_image.src=\"http://test.peuplies.info/PHP/Photos_gallery/".$path."/".$file."";
}
echo "} //-> </script> </head> <body><center>";
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "<img src=\"".$path."/".$file."\" width=\"50px\">";
}
//closing the directory
closedir($dir_handle);
?>
</center>
</body>
</html>
Thanks!
-
Apr 21, 2009, 18:02 #2
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this.
PHP Code:while (false !== ($file = readdir($handle))) {# notice this is different
if ($file != '.' && $file != '..') {
$imginfo = getimagesize($file);
$ratio = $imginfo[0] / $imginfo[1];
if ($ratio < 1) {
$portrait[] = $file;
} else {
$landscape[] = $file;
}
}
}
$images = array_merge($portrait,$landscape);
foreach ($images as $image) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
-
Apr 21, 2009, 18:38 #3
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
it don'twork, or I can't make it work.
here is a way I have tried :
PHP Code:
<html>
<head>
<SCRIPT language="JavaScript">
<!--
if (document.images)
{
preload_image = new Image(25,25);
<?PHP
// directory name sent by url
$dir = $_GET['dir'];
$file = $_GET['file'];
//define the path as relative
$path = $dir."".$file;
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "preload_image.src=\"http://test.peuplies.info/PHP/Photos_gallery/".$path."/".$file."";
}
echo "} //-> </script> </head> <body><center>";
while (false !== ($file = readdir($dir_handle))) {# notice this is different
if ($file != '.' && $file != '..') {
$imginfo = getimagesize($file);
$ratio = $imginfo[0] / $imginfo[1];
if ($ratio < 1) {
$portrait[] = $file;
} else {
$landscape[] = $file;
}
}
}
$images = array_merge($portrait,$landscape);
foreach ($images as $image) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
//closing the directory
closedir($dir_handle);
?>
</center>
</body>
</html>
-
Apr 21, 2009, 18:49 #4
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your $path needs to be the server path to the directory you are opening and reading, and not include a file in it.
Since you are receiving the directory and file values from the GET method, as well as if it was user input, you should validate and filter the variable values.
PHP Code:$dir = $_GET['dir'];
$path = '/server/path/to/'.$dir;
-
Apr 21, 2009, 18:53 #5
*Wonders why one still uses opendir...
-
Apr 21, 2009, 19:17 #6
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
how do i find it ?
-
Apr 21, 2009, 19:20 #7
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
@logic_earth
....because of the dir class?
-
Apr 21, 2009, 19:22 #8
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 21, 2009, 19:31 #9
-
Apr 21, 2009, 19:37 #10
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
can I just change the \ by / ?
-
Apr 21, 2009, 19:44 #11
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 21, 2009, 19:50 #12
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Huh?? Which ones are you referring to?
I noticed a problem with your double quotes not being escaped properly on the line below. As well $path would not work in this line. It would probably have to be directory ($dir).
PHP Code:echo "preload_image.src=\"http://test.peuplies.info/PHP/Photos_gallery/".$dir."/".$file."\"";
PHP Code:echo 'preload_image.src="http://test.peuplies.info/PHP/Photos_gallery/'.$dir.'/'.$file.'"';
-
Apr 21, 2009, 20:30 #13
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no error now, but it don't display anything ?
PHP Code:
<html>
<head>
<SCRIPT language="JavaScript">
<!--
if (document.images)
{
preload_image = new Image(25,25);
<?PHP
// directory name sent by url
$dir = $_GET['dir'];
$path = $_SERVER['DOCUMENT_ROOT'].''.$dir;
$file = $_GET['file'];
//define the path as relative
$path = $dir."".$file;
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "preload_image.src=\"http://test.peuplies.info/PHP/Photos_gallery/".$path."/".$file."";
}
echo "} //-> </script> </head> <body><center>";
while (false !== ($file = readdir($dir_handle))) {# notice this is different
if ($file != '.' && $file != '..') {
$imginfo = getimagesize($file);
$ratio = $imginfo[0] / $imginfo[1];
if ($ratio < 1) {
$portrait[] = $file;
} else {
$landscape[] = $file;
}
}
foreach ($portrait as $port) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
foreach ($landscape as $land) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
}
//closing the directory
closedir($dir_handle);
?>
</center>
</body>
</html>
-
Apr 21, 2009, 20:52 #14
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Let's see if the $file is getting put into the arrays.
PHP Code:while (false !== ($file = readdir($dir_handle))) {# notice this is different
if ($file != '.' && $file != '..') {
$imginfo = getimagesize($file);
$ratio = $imginfo[0] / $imginfo[1];
if ($ratio < 1) {
# $portrait[] = $file;
print $file.' portrait file here<br>';
} else {
# $landscape[] = $file;
print $file.' landscape file here<br>';
}
}
}
/*
$images = array_merge($portrait,$landscape);
foreach ($images as $image) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
*/
PHP Code:echo "Directory Listing of $path<br/>";
-
Apr 21, 2009, 20:57 #15
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
nothing so far, dont even echo "Directory..."
-
Apr 21, 2009, 20:58 #16
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
sorry :
Art/Panama_Feb_2007/Gamboa
-
Apr 21, 2009, 21:06 #17
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If I copy your code :
Notice: Undefined variable: portrait in ....s\PHP\Photos_gallery\folder_view.php on line 62
Warning: Invalid argument supplied for foreach() in ....s\PHP\Photos_gallery\folder_view.php on line 62
Notice: Undefined variable: landscape in ...s\PHP\Photos_gallery\folder_view.php on line 70
Warning: Invalid argument supplied for foreach() in ....s\PHP\Photos_gallery\folder_view.php on line 70
-
Apr 21, 2009, 21:06 #18
- Join Date
- Feb 2006
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:
<html>
<head>
<SCRIPT language="JavaScript">
<!--
if (document.images)
{
preload_image = new Image(25,25);
<?PHP
// directory name sent by url
$dir = $_GET['dir'];
$path = $_SERVER['DOCUMENT_ROOT'].''.$dir;
$file = $_GET['file'];
//define the path as relative
$path = $dir."".$file;
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "preload_image.src=\"http://test.peuplies.info/PHP/Photos_gallery/".$path."/".$file."";
}
echo "} //-> </script> </head> <body>";
echo "Directory Listing of ".$path."<br/><center>";
while (false !== ($file = readdir($dir_handle))) {# notice this is different
if ($file != '.' && $file != '..') {
$imginfo = getimagesize($file);
$ratio = $imginfo[0] / $imginfo[1];
if ($ratio < 1) {
$portrait[] = $file;
} else {
$landscape[] = $file;
}
}
}
foreach ($portrait as $port) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
foreach ($landscape as $land) {
echo '<img src="'.$path.'/'.$image.'" width="50px">';
}
//closing the directory
closedir($dir_handle);
?>
</center>
</body>
</html>
-
Apr 21, 2009, 21:19 #19
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You are assigning a value to $path twice.
PHP Code:// directory name sent by url
$dir = $_GET['dir'];
$path = $_SERVER['DOCUMENT_ROOT'].''.$dir;
$file = $_GET['file'];
//define the path as relative
$path = $dir."".$file;
The path you use to open and read the directory needs to be the server path and cannot contain a file name.
PHP Code:$server_path = $_SERVER['DOCUMENT_ROOT'].'/Art/Panama_Feb_2007/'.$dir;
opendir($server_path);
PHP Code:$url_path = '/Art/Panama_Feb_2007/'.$dir;
echo '<img src="'.$url_path.'" border="0">';
PHP Code:$variable = $value1.$value2;
Bookmarks