SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post 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!

  2. #2
    SitePoint Addict
    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">';


  3. #3
    SitePoint Enthusiast
    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>
    thanks!

  4. #4
    SitePoint Addict
    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

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    *Wonders why one still uses opendir...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do i find it ?

  7. #7
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @logic_earth

    ....because of the dir class?

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spiderling View Post
    @logic_earth

    ....because of the dir class?
    Nah glob, a lot nicer to work with.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Nah glob, a lot nicer to work with.
    I'm always open to new and different ways. Thanks.

  10. #10
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Peuplarchie View Post
    how do i find it ?
    PHP Code:
    echo $_SERVER['DOCUMENT_ROOT']; 

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can I just change the \ by / ?

  12. #12
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Peuplarchie View Post
    can I just change the \ by / ?
    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."\""
    or for it to be easier to read.
    PHP Code:
    echo 'preload_image.src="http://test.peuplies.info/PHP/Photos_gallery/'.$dir.'/'.$file.'"'

  13. #13
    SitePoint Enthusiast
    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>

  14. #14
    SitePoint Addict
    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">';

    */ 
    What does path echo as in this line?
    PHP Code:
    echo "Directory Listing of $path<br/>"

  15. #15
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nothing so far, dont even echo "Directory..."

  16. #16
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry :

    Art/Panama_Feb_2007/Gamboa

  17. #17
    SitePoint Enthusiast
    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

  18. #18
    SitePoint Enthusiast
    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>

  19. #19
    SitePoint Addict
    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); 
    The path you use for HTML is different.
    PHP Code:
    $url_path '/Art/Panama_Feb_2007/'.$dir;
    echo 
    '<img src="'.$url_path.'" border="0">'
    Also, when you concatenate two variables you don't need the empty quotes in between.
    PHP Code:
    $variable $value1.$value2


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •