SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Function not behaving...

    In my root directory, I have a folder named 'd'. Without going into semantics, it simply stands for "Downloads".

    Having said that, I have a function which is included on my resume page of the following code:

    PHP Code:
    function pdfGen1(){
        
    $active basename($_SERVER['PHP_SELF']);
        
    $dir './d/';
        if (
    $handle opendir($dir)){
            echo 
    '<ul>';
            while(
    false !== ($file readdir($handle))){
                if(
    substr($file,-4) == '.pdf'){
                    
    $displayName=ucfirst($file);
                    echo
    '<li><span class='.$spanClass.'><a href="'.$file.'">'.$displayName.'</a></span></li>';
                 }
                 elseif(
    substr($file,-4) == '.doc'){
                    
    $displayName=ucfirst($file);
                    echo
    '<li><span class='.$spanClass.'><a href="'.$file.'">'.$displayName.'</a></span></li>';

                 }
            }
            echo 
    '</ul>';
            
    closedir($handle);
        }

    For the life of me, I cannot seem to grasp why I continually fail to make the function look in the folder 'd'. I'm thinking it's because of the way I have the $dir setup, but what other way is there?

  2. #2
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the directory is in root shouldn't it be -
    $dir = '/d';

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The variable
    PHP Code:
    $spanClass 
    is never used, nor is
    PHP Code:
    $active 

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by catweasel View Post
    If the directory is in root shouldn't it be -
    $dir = '/d';
    Yep, ./ is "current directory";

    Quote Originally Posted by SilverBulletUK View Post
    The variable
    PHP Code:
    $spanClass 
    is never used, nor is
    PHP Code:
    $active 
    $active wont matter if its used or not as it's in the scope of the function but $spanClass will throw an error
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, got rid of $active and $spanClass... Still no go. The error that's thrown is the following:

    "Warning: opendir..."

    This is what I have now:

    PHP Code:
    function pdfGen1(){
        
    $dir '/d';
        if (
    $handle opendir($dir)){
            echo 
    '<ul>';
            while(
    false !== ($file readdir($handle))){
                if(
    substr($file,-4) == '.pdf'){
                    
    $displayName=ucfirst($file);
                    echo
    '<li><span><a href="'.$file.'">'.$displayName.'</a></span></li>';
                 }
                 elseif(
    substr($file,-4) == '.doc'){
                    
    $displayName=ucfirst($file);
                    echo
    '<li><span><a href="'.$file.'">'.$displayName.'</a></span></li>';
                 }
            }
            echo 
    '</ul>';
            
    closedir($handle);
        }


  6. #6
    Made with a Mac! philm's Avatar
    Join Date
    Sep 2001
    Location
    Portsmouth, UK
    Posts
    735
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure that's the right server path for the directory?

    On every server I've used it's more like this "/var/www/html/d/".

  7. #7
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by philm View Post
    Are you sure that's the right server path for the directory?

    On every server I've used it's more like this "/var/www/html/d/".
    In all honesty - no. I've always had problems understanding subtle differences between addressing and relative locations on systems because of the way you have to think about them.

    Resume.php is in my website's root or primary "master" folder, not my hosting space root folder (the folder on the server that would be something like "www" or "public_html"). Maybe I was stupid for not explaining that? I interpret "root" as being the first level of file / folder system which pertains to the actual hosted website at hand...

    Get this - when I use "./d", it reads the files, but doesn't apply the correct linkage to the actual names of the files within the hyperlink. It's truly odd. The way I set that script up, it's supposed to read the files, output their names and link them to the files. How can it read the files but not apply the right linkage if the URL is off?

  8. #8
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh ok.. so you need to look in document root not the system root directory.
    Have you tried using glob() ? This function will print out a list of pdf, doc files in the directory where it is kept.
    PHP Code:
    <?php

    function fileList() {
        
    $files glob("{*.pdf,*.doc}",GLOB_BRACE );
        if (!empty(
    $files)) {
            
    $out '<ul>';
            foreach (
    $files as $filename) {
                
    $out .= '<li><span><a href="'.$filename.'">'.ucfirst($filename).'</a></span></li>';
            }
            
    $out .= '</ul>';
            return 
    $out;
        }
    }
    echo 
    fileList();
    ?>
    The GLOB_BRACE flag won't work on some systems though.

  9. #9
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by catweasel View Post
    ahh ok.. so you need to look in document root not the system root directory.
    Have you tried using glob() ? This function will print out a list of pdf, doc files in the directory where it is kept.
    PHP Code:
    <?php

    function fileList() {
        
    $files glob("{*.pdf,*.doc}",GLOB_BRACE );
        if (!empty(
    $files)) {
            
    $out '<ul>';
            foreach (
    $files as $filename) {
                
    $out .= '<li><span><a href="'.$filename.'">'.ucfirst($filename).'</a></span></li>';
            }
            
    $out .= '</ul>';
            return 
    $out;
        }
    }
    echo 
    fileList();
    ?>
    The GLOB_BRACE flag won't work on some systems though.
    Close... I have to look in a folder within document root named 'd'. How would I modify the above to look in that folder?

  10. #10
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $_SERVER['DOCUMENT_ROOT'] . '/d/'
    Note that the $_SERVER['DOCUMENT_ROOT'] is defined in Apache, so it could have the trailing slash or could have it left off. Adjust your code as needed (you'll need to remove the slash in front of /d/ if it does)


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
  •