www.webdeveloper.com
Results 1 to 10 of 10

Thread: Help with PHP

  1. #1
    Join Date
    Dec 2002
    Location
    Montana
    Posts
    24

    Help with PHP

    I have a simple stupid quetion that I have been fighting with for two days now. All I need to do is get a script that will look in a directory for files or a specific kind of file, like a '.pdf' file and return the contents of the directory in an array. I have fought with it for two days. I can use readdir() and get it to work but the results are not in an array so I can't sort them alpha-numerically.
    Here is a copy of the script that I use for a readir() function
    PHP Code:
    <?
    $dirname 
    "/path/to/httpdocs/newsletters";
    $dh opendir($dirname) or die ("couldn't open directory");
    while (!((
    $file readdir($dh)) === false) ) {
        if (
    strpos($file,'.pdf') ) {
            
    $base basename($file'.pdf');
              echo 
    "<a HREF='http://www.mydomain.com/newsletters/$file'>$base<br></a>";
           }
        elseif (
    strpos($file,'.PDF') ) {
              echo 
    "<a HREF='http://www.mydomain.com/newsletters/$file'>$file<br></a>";
           }
    }
    closedir($dh);
    ?>
    This script works fine but I cannot sort alpha-numerically.
    Thanks for all your help,
    You can see this script in action at www.lionsdistrict37a.org/news.php

  2. #2
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    See the glob function. It does exactly what you want.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Dec 2002
    Location
    Montana
    Posts
    24

    Still isn't working

    I went to php.net and looked at the glob function. None of the books that I have mention it and the tried to use it exactly the way they were using it at php.net. I get a "Error, call to undefined funtion glob() in "filemene line X"

    Maybe my version of PHP is too old but it is the version that came with Redhat 8!?!?!?!?

    The glob() function doesn't work on my real world server either. I get the same error.

    If you can think of anything else I would be happy to hear about it.

    PS Here is how I used the glob() function

    PHP Code:
    <?php
    foreach(glob("newsletters/*.pdf") as $filename) {
         echo 
    "$filename\n";
    }
    ?>
    Now I know that script isn't written perfectly but I still get an undefined function error.

    I will also try searching google for the function to see if I can get a better explanation.

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    You may want to try something like this to put the files into an array:

    PHP Code:
    $folder "yourfolder";
    $handle opendir($folder);
    while (
    $file readdir($handle))
    {
        
    $array[] = $file;
    }
    closedir($handle); 
    and then you can sort your array with sort($array)

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  5. #5
    Join Date
    Dec 2002
    Location
    Montana
    Posts
    24

    Thanks pyro

    Thanks pyro. I have used the script that you gave me for a base on what I am doing. Of course I modified it a little to make it fit my needs specifically, like I added a strpos() function to search for specific file types and I added a basename() function to strip off the file extension to the display. But you certainly got me on the right track. Thanks a million.

  6. #6
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    Pyro, I'm curious if you had an explanation for why the documented glob function didn't work properly?
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  7. #7
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    My guess would be that lomfs24 is using an older version of PHP. I believe that glob was introduced in PHP 4.3, so that could be the explaination...

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  8. #8
    Join Date
    Dec 2002
    Location
    Montana
    Posts
    24

    PHP Version

    I am not sure what version of PHP I am running. I have a newer version installed on my laptop but I am mainly developing on my Linux machine which is running Redhat 8 with all of the newest updates. It is whatever version comes with that and I am not too sure which version it is or how to check it.

  9. #9
    Join Date
    Dec 2002
    Location
    Montana
    Posts
    24

    Version continued

    I am also developing on my server at home and when i get all the bugs worked out of a script and get it to do what I want I then upload it to my real server. The other server does not support the glob() function either so I would rather develop without it.

  10. #10
    Join Date
    Dec 2002
    Location
    Montana
    Posts
    24

    One more post

    I just ran the phpinfo() function and at the top it says version 4.2.2 so you are right, I am running a version prior to 4.3.
    OK, I will shut up now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles