PHP Script

Discussion in 'Software' started by Yzaraf, Aug 3, 2005.

  1. Yzaraf

    Yzaraf Private E-2

    I am looking for a PHP script that can search in a folder and create links to each of the items with specific extensions in the folder


    For instance

    folder contains

    Hello.pdf
    BobSmells.pdf
    Picture.jpg

    The page will generate the html:
    Code:
    <a href="Hello.pdf">Hello</a>
    <a href="BobSmells.pdf">BobSmells</a>
    

    I am also looking for a script that will do the same thing except:

    the file names will be

    hello_bob.pdf

    and they will be displayed as Hello Bob (where the _ becomes a space and the first letter of each word is capitalized)

    Thanks!
     
  2. goldfish

    goldfish Lt. Sushi.DC

    I'm not going to write it for you, but I'll give you some links and pointers to how to do it yourself. Its quite simple and it is a good little script to get you started if you havn't already :)

    Linkage:
    http://www.w3schools.com/php/default.asp
    http://www.php.net/

    You're going to need to work out how to list a directory, enumerate the files within that directiory (using a for loop probably), and then take the filenames and apply some string formatting. The output is quite simple really, you'd use a statement like
    Code:
    echo "<a href='$filename'>$formatted_filename</a>";
    
    For formatting the string, you'll need to write a function which will cut off the extension, then go through a string and capitalise letters after underscores, and replace underscores with spaces. I expect you'd explode() the string, use that array to manipulate it, then implode() it back into a string again.

    If you don't fancy all that work I could write some of it for you (that capitalising function I could do if required).
     
  3. Yzaraf

    Yzaraf Private E-2

    lol... I was just being a bit lazy...

    What functions deal with the contents of a directory?
    Should it be an iterator of somekind? If so what?
     
  4. goldfish

    goldfish Lt. Sushi.DC

    You'll find this very fun: http://www.php.net/opendir
    Expecially the code example ;)

    PHP:
    <?php
    $dir 
    "/etc/php5/";

    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
       if (
    $dh opendir($dir)) {
           while ((
    $file readdir($dh)) !== false) {
               echo 
    "filename: $file : filetype: " filetype($dir $file) . "\n";
           }
           
    closedir($dh);
       }
    }
    ?> 
    Add some filtering of some kind using functions like strstr() to check whether certain strings are present, or strlen(), even simple conditionals (like if ($filename == "." || "..") ) to get what you want.

    All you need to do then is format the output to how you need, and you're done! :)

    To think, you could have downloaded a 20kb file to do that and another 50 functions you'd never need... for you :p
     
  5. Yzaraf

    Yzaraf Private E-2

    Thanks a lot!

    One more question though,

    Would it just be smarter to database all the files into MySQL?

    Then I could add keywords, search items, and other things.
     
  6. Yzaraf

    Yzaraf Private E-2

    Wait... Something isnt working.

    I have the file index.php at localhost/jiftedland/mu_alpha_theta/

    The directories I want read are at:

    localhost/jiftedland/mu_alpha_theta/Algebra I/
    PHP:
          <?php
    $dir 
    "/Algebra%20I/";
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
       if (
    $dh opendir($dir)) {
           while ((
    $file readdir($dh)) !== false) {
               echo 
    "<a href='$file'>$file</a>";
           }
           
    closedir($dh);
       }
    }
    ?> 

    Should that get me where I want to go? Because I am getting nothing!
     
  7. goldfish

    goldfish Lt. Sushi.DC

    Thats because its looking at the root of the filesystem /, when you're working with directories you need to have an absolute path to them, which you might do thus:
    PHP:
    $dir $_SERVER['DOCUMENT_ROOT']."jiftedland/mu_alpha_theta/Algebra%20I/";
    This does require that the document root variable is correctly configured in the php.ini file.. you can check this with phpinfo() fairly easily.
     
  8. Yzaraf

    Yzaraf Private E-2

    Is there a way to stop the script from going down levels?
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds