php script that displays last uploaded jpg in a directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robinpg.SHU@gmail.com

    php script that displays last uploaded jpg in a directory

    As title really, does anyone know of (or could write?) a php file which
    in a directory of .jpg files display the most recent added to that
    /dir?

    Can anyone help?

  • Andy Hassall

    #2
    Re: php script that displays last uploaded jpg in a directory

    On 6 Feb 2005 09:36:21 -0800, robinpg.SHU@gma il.com wrote:
    [color=blue]
    >As title really, does anyone know of (or could write?) a php file which
    >in a directory of .jpg files display the most recent added to that
    >/dir?
    >
    >Can anyone help?[/color]

    <?php
    $ctime = 0;
    foreach (glob('*.jpg') as $file)
    {
    if ($ctime > filectime($file ))
    {
    $match = $file;
    $ctime = filectime($file );
    }
    }

    print $file;
    ?>

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Geoff Berrow

      #3
      Re: php script that displays last uploaded jpg in a directory

      I noticed that Message-ID:
      <1107711381.614 337.85260@f14g2 000cwb.googlegr oups.com> from
      robinpg.SHU@gma il.com contained the following:
      [color=blue]
      >As title really, does anyone know of (or could write?) a php file which
      >in a directory of .jpg files display the most recent added to that
      >/dir?[/color]

      This would be useful



      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • arepeejee

        #4
        Re: php script that displays last uploaded jpg in a directory

        I think i'll have to elaborate on my original question a little
        further, when I say display I mean literally display the image in an
        img src HTML tag.

        Cheers

        Comment

        • Andy Hassall

          #5
          Re: php script that displays last uploaded jpg in a directory

          On 6 Feb 2005 11:13:24 -0800, "arepeejee" <robinpg.SHU@gm ail.com> wrote:
          [color=blue]
          >I think i'll have to elaborate on my original question a little
          >further, when I say display I mean literally display the image in an
          >img src HTML tag.[/color]

          Well then, instead of:

          print $file;

          ... then ...

          print '<img src="' . htmlentities($f ile) . '" alt="whatever"> ';

          --
          Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

          Comment

          • arepeejee

            #6
            Re: php script that displays last uploaded jpg in a directory

            Hmm, I still cant get it to work. It doesnt appear to like the $ in
            line16.

            Comment

            • Matthias Esken

              #7
              Re: php script that displays last uploaded jpg in a directory

              arepeejee wrote:
              [color=blue]
              > I think i'll have to elaborate on my original question a little
              > further, when I say display I mean literally display the image in an
              > img src HTML tag.[/color]

              With the code from Andy you get the file name. What else do you need?

              Matthias

              Comment

              • arepeejee

                #8
                Re: php script that displays last uploaded jpg in a directory

                Well, ive got a random php script (below) which I can literally have it
                as an image:

                <img src=http://whatever.com/random.php>

                and I want a similar thing to instead display the last updated image
                from the dir.

                [code:

                <?php
                $fileList = array();
                $folder = ".";
                $handle = opendir($folder );
                while (
                false !== ( $file = readdir($handle ) )
                ) {
                if (
                substr($file, -4) == ".gif" ||
                substr($file, -4) == ".jpg"
                ) {
                $fileList[count($fileList )] = $file;
                }
                }
                closedir($handl e);
                $randNum = rand( 0, (sizeOf($fileLi st) -1) );
                if (
                substr($fileLis t[$randNum], -4) == ".gif"
                ) {
                header ("Content-type: image/gif");
                } elseif (
                substr($fileLis t[$randNum], -4) == ".jpg"
                ) {
                header ("Content-type: image/jpeg");
                }
                readfile($fileL ist[$randNum]);
                ?>

                [end code]

                Comment

                • Andy Hassall

                  #9
                  Re: php script that displays last uploaded jpg in a directory

                  On 6 Feb 2005 11:40:01 -0800, "arepeejee" <robinpg.SHU@gm ail.com> wrote:
                  [color=blue]
                  >Hmm, I still cant get it to work. It doesnt appear to like the $ in
                  >line16.[/color]

                  What line 16? I posted 13 lines of code.

                  --
                  Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
                  <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

                  Comment

                  • arepeejee

                    #10
                    Re: php script that displays last uploaded jpg in a directory

                    How odd, thats what it said (and still says) here

                    Comment

                    • arepeejee

                      #11
                      Re: php script that displays last uploaded jpg in a directory

                      I have managed to find a solution, and for anyoen who may want
                      something similar, thanks to all above:

                      <?php

                      $path = './';
                      // with trailing slash, for images in other dirs: $path =
                      './foldername/'



                      unset($fpath, $time, $time, $ext, $type);

                      $type = array
                      (
                      // ---------------------------
                      // ext mime type

                      'png' => 'image/png',
                      'jpeg' => 'image/jpeg',
                      'jpg' => 'image/jpeg',
                      'jpe' => 'image/jpeg',
                      'gif' => 'image/gif',
                      'tiff' => 'image/tiff',
                      'tif' => 'image/tiff',
                      );

                      if ( is_dir($path) )
                      {
                      if ( $dh = @opendir($path) )
                      {
                      while ( ($file = readdir($dh) ) !== false )
                      {
                      if ( $file == '.' or $file == '..' )
                      {
                      next;
                      }
                      // echo $file . " :";

                      // Var with full path+name
                      $fpath = $path.$file;

                      // Skip if dir
                      if ( is_dir($fpath) )
                      {
                      next;
                      }

                      // echo $fpath . " ::";

                      // Get file extension
                      $ext = substr ( $fpath, strrpos($fpath, '.') + 1 );

                      // Should get most recent file
                      if ( isset($type[$ext]) )
                      {
                      $mtime = filemtime($fpat h);

                      if ( $mtime > $time )
                      {
                      $img = array ( 'fpath' => $fpath, 'name' => $file, 'ext' => $ext
                      );

                      $time = $mtime;
                      }
                      }
                      }
                      }

                      closedir($dh);

                      if ( $img['name'] != '' )
                      {
                      if ( $fh = @fopen($img['fpath'], "rb") )
                      {
                      @header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT'); //
                      Date in the past
                      @header( 'Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); //
                      always modified
                      @header( 'Cache-Control: no-cache, must-revalidate'); //
                      HTTP/1.1
                      @header( 'Pragma: no-cache'); //
                      HTTP/1.0
                      @header( 'Content-Disposition: inline; filename="'.$im g['name'].'"'
                      );
                      @header( 'Content-transfer-encoding: binary');
                      @header( 'Content-Length: '.(string)(file size($img['fpath'])) );
                      @header( 'Content-Type: '.$type[ $img['ext'] ] );

                      // @header( 'Content-Type: '.$type[ $img['ext'] ] );
                      // @header( 'Content-Disposition: inline;
                      filename="'.$im g['name'].'"' );
                      // @header( 'Content-Length: '.(string)(file size($img['fpath'])) );

                      while ( ! feof($fh) )
                      {
                      echo fread($fh, 8192);
                      }
                      @fclose($fh);

                      exit;
                      }
                      }
                      }




                      $filedata =
                      base64_decode(' R0lGODlhAQABAIA AAMDAwAAAACH5BA EAAAAALAAAAAABA AEAAAICRAEAOw== ');
                      $filesize = strlen($filedat a);

                      @header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date
                      in the past
                      @header( 'Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); //
                      always modified
                      @header( 'Cache-Control: no-cache, must-revalidate'); //
                      HTTP/1.1
                      @header( 'Pragma: no-cache'); //
                      HTTP/1.0
                      @header( 'Content-disposition: inline; filename=blank. gif');
                      @header( 'Content-transfer-encoding: binary');
                      @header( "Content-Length: $filesize");
                      @header( 'Content-type: image/gif');

                      echo $filedata;
                      exit;

                      ?>

                      Comment

                      Working...