Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: read directory

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default read directory

    Anyone know of a php or java script that will read a directory and give all the files in it a name with a number after it. I'm trying to make a simple image gallery for someone that doesnt know html or any coding just how to put images into an ftp.
    ok back to the code

    in the html file i want it to write something like <a href="$file1.jpg"><img src="$file1.jpg"></a>
    $file1.jpg being the first file in the directory and then continue writing those until it gets to the last image in the directory.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP:
    Code:
    <?php
      define('IMG_DIR', '/image/directory');
    
      function is_image($fn) {
        return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
      }
    
      $imgs = array_filter(scandir(IMG_DIR), 'is_image');
      for ($i = 0; $i < count($imgs); ++$i)
        $imgs[$i] = IMG_DIR . $imgs[$i];
    ?>
    <!-- ... -->
    <?php foreach ($imgs as $img) { ?>
      <a href="$img">
        <img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
      </a>
    <?php } ?>
    Last edited by Twey; 10-05-2008 at 07:15 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    This is bringing back an error message each time i try it here http://www.bluewalrus.net/testing.php.

    It says "
    Parse error: parse error, unexpected ';' in /hsphere/local/home/crazychr/bluewalrus.net/testing.php on line 17
    "
    Which is in dreamweaver for me foreach ($i = 0; $i < count($imgs); ++$i)

    Any idea on that?
    Thanks

  4. #4
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try
    Code:
    <?php
      define('IMG_DIR', '/image/directory');
    
      function is_image($fn) {
        return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
      }
    
      $imgs = array_filter(scandir(IMG_DIR), 'is_image');
      for ($i = 0; $i < count($imgs); ++$i)
        $imgs[$i] = IMG_DIR . $imgs[$i];
    ?>
    <!-- ... -->
    <?php foreach ($imgs as $img) { ?>
      <a href="$img">
        <img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
      </a>
    <?php } ?>
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oops, aye. Started off with a foreach then switched to a for

    Edited to avoid confusion.
    Last edited by Twey; 10-05-2008 at 07:14 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Brings up this now:
    Fatal error: Call to undefined function: scandir() in /hsphere/local/home/crazychr/bluewalrus.net/testing.php on line 16

  7. #7
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I wrote the code for PHP5, but it seems that you are still using PHP4. I strongly suggest you upgrade: PHP4 is no longer supported by the PHP team.

    For the meantime, here is the PHP4 version:
    Code:
    <?php
      define('IMG_DIR', '/image/directory');
    
      function is_image($fn) {
        return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
      }
    
      $imgs = array();
    
      $dir = opendir(IMG_DIR);
      while ($ff = readdir($dir))
        if (is_image($f))
          $imgs[] = IMG_DIR . '/' . $f;
      closedir($dir);
    ?>
    <!-- ... -->
    <?php foreach ($imgs as $img) { ?>
      <a href="$img">
        <img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
      </a>
    <?php } ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    hmm i don't know where the php control setting is to up it to 5 yet. So right now i have the 4 up on my page but its not writing the images still for some reason. When i look at the source in a web browser it just has a comments tag (<!-- ... -->).
    http://www.bluewalrus.net/testing.php
    I only changed the directory in the php code so that it would link correctly.
    I'll put in the whole pages html in case there's something I've put in wrong in the validation side as well.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
      define('IMG_DIR', 'images/');
    	function is_image($fn) {
        return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
      }
      $imgs = array();
      $dir = opendir(IMG_DIR);
      while ($ff = readdir($dir))
        if (is_image($f))
          $imgs[] = IMG_DIR . '/' . $f;
      closedir($dir);
    ?>
    <!-- ... -->
    <?php foreach ($imgs as $img) { ?>
      <a href="$img">
        <img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
      </a>
    <?php } ?>
    </body>
    </html>
    Just went into my host and i think i had php only set to 3 so i think i just added 4 and 5 if this is what that looks like?
    Code:
    <IfModule mod_php4.c>
    AddType application/x-httpd-php .php3
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php5
    AddType application/x-httpd-php .php4
    Last edited by bluewalrus; 10-05-2008 at 07:55 PM.

  10. #10
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    For those looking for this there's a closer script in the php board under my name that also went unanswered after the second post but that one got one image to display.

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
  •