choosing one file from dir

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JaNE

    choosing one file from dir

    hello and please excuse my clumsy english.
    I'm really fresh in php (just want to make one small script for myself,
    haven't fount such on net) and now I'm struggling with "calling one file
    from some directory".

    here is case:
    have 10 dirs with picts, every dir have one subdir with thumbs.
    have file with name of dir and owner of pics.

    $f = fopen('autori.i nc', 'r');
    while ($line = fgets($f, 4096)) {
    list($dir, $ime) = split("\|", htmlspecialchar s($line));
    $album = $dir;
    $autor = $ime;

    now, I would like to print one picture form each directory above the
    name of the owner...

    $handle = fopen("$album/thumb/*.jpg", "wb");

    and, as can you see, I have no idea how to get that file... better to
    say, I have ideas, but wrong.

    here came place where I will use that image.

    echo "<p class=\"image\" ><img src=\"$handle\" alt=\"$autor\"
    height=\"64\" border=\"0\"></p>\n";
    echo "<p class=\"autor\" ><a class=\"autor\"
    href=\"album.ph p?album=$album& amp;autor=$auto r\">$autor</a></p>\n";


    any suggestion? tip?

    tnx.

    Janko

    --
    Jan_ko?
    --
  • JaNE

    #2
    Re: choosing one file from dir

    > $handle = fopen("$album/thumb/*.jpg", "wb");[color=blue]
    >[/color]

    huuuuh.... ok, that won't be "fopen" and definitely won't be "w"...
    at least I have learned how to create "empty jpeg" :))

    --
    Jan_ko?
    --

    Comment

    • Yang Li Ke

      #3
      Re: choosing one file from dir

      see readdir instead of fopen





      --


      "JaNE" <not_me@mail.do t> wrote in message
      news:1g8l3kk.gu doce1rr09iwN%no t_me@mail.dot.. .[color=blue][color=green]
      > > $handle = fopen("$album/thumb/*.jpg", "wb");
      > >[/color]
      >
      > huuuuh.... ok, that won't be "fopen" and definitely won't be "w"...
      > at least I have learned how to create "empty jpeg" :))
      >
      > --
      > Jan_ko?
      > --[/color]


      Comment

      • Shawn Wilson

        #4
        Re: choosing one file from dir

        JaNE wrote:[color=blue]
        >
        > hello and please excuse my clumsy english.
        > I'm really fresh in php (just want to make one small script for myself,
        > haven't fount such on net) and now I'm struggling with "calling one file
        > from some directory".
        >
        > here is case:
        > have 10 dirs with picts, every dir have one subdir with thumbs.
        > have file with name of dir and owner of pics.
        >
        > $f = fopen('autori.i nc', 'r');
        > while ($line = fgets($f, 4096)) {
        > list($dir, $ime) = split("\|", htmlspecialchar s($line));
        > $album = $dir;
        > $autor = $ime;
        >
        > now, I would like to print one picture form each directory above the
        > name of the owner...
        >
        > $handle = fopen("$album/thumb/*.jpg", "wb");
        >
        > and, as can you see, I have no idea how to get that file... better to
        > say, I have ideas, but wrong.
        >
        > here came place where I will use that image.
        >
        > echo "<p class=\"image\" ><img src=\"$handle\" alt=\"$autor\"
        > height=\"64\" border=\"0\"></p>\n";
        > echo "<p class=\"autor\" ><a class=\"autor\"
        > href=\"album.ph p?album=$album& amp;autor=$auto r\">$autor</a></p>\n";[/color]



        $f = fopen('autori.i nc', 'r');

        while ($line = fgets($f, 4096)) {
        list($albun, $autor) = split("\|", htmlspecialchar s($line));

        $thumbimage = "";

        $d = opendir("$album/thumb/"); //open directory
        while($filename = readdir($d)){ //read files
        if (preg_match("/\.jpg$/i", $filename)) { //get first jpeg
        $thumbimage = $filename;
        break; //stop going through directory if jpeg is found
        }
        }
        closedir($d);

        //write image code if jpg was found
        if (strlen($thumbi mage) > 0)
        echo "<p class=\"image\" ><img src=\"$album/thumb/$thumbimage\"
        alt=\"$autor\" height=\"64\" border=\"0\"></p>\n";

        //link to gallery (NOTE: &amp; has been replaced with "&" in link
        echo "<p class=\"autor\" ><a class=\"autor\"
        href=\"album.ph p?album=$album& autor=$autor\"> $autor</a></p>\n";
        }

        This UNTESTED code should work, I think. You should read up on any functions
        you're not familiar with and add some proper error handling. (i.e. in the code
        above you'll get an error with closedir() if there was a problem with
        opendir()).

        Regards,
        Shawn
        --
        Shawn Wilson
        shawn@glassgian t.com

        Comment

        • JaNE

          #5
          Re: choosing one file from dir

          Shawn Wilson <shawn@glassgia nt.com> wrote:[color=blue]
          >
          > This UNTESTED code should work, I think. You should read up on any
          > functions you're not familiar with and add some proper error handling.
          > (i.e. in the code above you'll get an error with closedir() if there was a
          > problem with opendir()).[/color]

          Tnx Shawn. that helped a lot.
          and yes... I should read, but what if I don't understand explanation of
          function? I know I need to start from the basics, if I want to be php
          programer, but case is that I'm photographer that just want to have one
          small simple script... have found a lot of them on net, but none
          satisfied me, so... here it is: me coding... (and *that* is joke:))

          tnx again

          --
          Jan_ko?
          --

          Comment

          • Shawn Wilson

            #6
            Re: choosing one file from dir

            JaNE wrote:[color=blue]
            >
            > Shawn Wilson <shawn@glassgia nt.com> wrote:[color=green]
            > >
            > > This UNTESTED code should work, I think. You should read up on any
            > > functions you're not familiar with and add some proper error handling.
            > > (i.e. in the code above you'll get an error with closedir() if there was a
            > > problem with opendir()).[/color]
            >
            > Tnx Shawn. that helped a lot.
            > and yes... I should read, but what if I don't understand explanation of
            > function? I know I need to start from the basics, if I want to be php
            > programer, but case is that I'm photographer that just want to have one
            > small simple script... have found a lot of them on net, but none
            > satisfied me, so... here it is: me coding... (and *that* is joke:))[/color]

            If you have trouble understanding a function, I find it helps to read the
            comments on the manual page, try the function in test scripts and/or ask about
            it here. Good luck with your script,

            Shawn
            --
            Shawn Wilson
            shawn@glassgian t.com

            Comment

            • JaNE

              #7
              Re: choosing one file from dir

              Shawn Wilson <shawn@glassgia nt.com> wrote:
              [color=blue]
              >
              > If you have trouble understanding a function, I find it helps to read the
              > comments on the manual page, try the function in test scripts and/or ask about
              > it here. Good luck with your script,
              >[/color]

              tnx it is working fine...
              temporary exhibition is on-line (agram.cc/galeria). now just some more
              css fine-tuning and will be ready to be shifted on my real site.
              the only problem left is how to made that script working as module for
              cms I did choose...

              --
              Jan_ko?
              --

              Comment

              • Shawn Wilson

                #8
                Re: choosing one file from dir

                JaNE wrote:[color=blue]
                >
                > Shawn Wilson <shawn@glassgia nt.com> wrote:
                >[color=green]
                > >
                > > If you have trouble understanding a function, I find it helps to read the
                > > comments on the manual page, try the function in test scripts and/or ask about
                > > it here. Good luck with your script,
                > >[/color]
                >
                > tnx it is working fine...
                > temporary exhibition is on-line (agram.cc/galeria). now just some more
                > css fine-tuning and will be ready to be shifted on my real site.
                > the only problem left is how to made that script working as module for
                > cms I did choose...[/color]

                Glad it's working for you. I forgot to mention you should use url_encode() on
                variables you're sending in a link. Otherwise, if there are spaces, &s, ?s,
                etc. you'll get an error on some browsers.

                //write image code if jpg was found
                if (strlen($thumbi mage) > 0)
                echo "<p class=\"image\" ><img
                src=\"".url_enc ode($album)."/thumb/".url_encode($t humbimage)."\"
                alt=\"$autor\" height=\"64\" border=\"0\"></p>\n";

                //link to gallery (NOTE: &amp; has been replaced with "&" in link
                echo "<p class=\"autor\" ><a class=\"autor\"

                href=\"album.ph p?album=".url_e ncode($album)." &autor=".url_en code($autor)."\ ">$autor</a></p>\n";

                Shawn
                --
                Shawn Wilson
                shawn@glassgian t.com

                Comment

                Working...