PHP Programming Help Needed Simple Problem

Discussion in 'PHP' started by amandeepmail, Sep 1, 2008.

  1. #1
    Hello all,
    I am using opendir script from php.net to view all the files in a directory and link to them. I am having 1 problem. The script Displays all the files in array but also uses extension at the end. I want that file name should be displayed but not the extension in anchor text. Can you help me.
    This is the script-

    <?php echo "<b>Individual Songs</b><br/>Right Click and Save Target as to Download<br/><ul>";
    
    function getFiles($path) {
       //Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
       //sorted by the file name (case insensitive).  If two files are identical when compared without case, they will sort
       //relative to each other in the order presented by readdir()
       $files = array();
       $fileNames = array();
       $i = 0;
      
       if (is_dir($path)) {
           if ($dh = opendir($path)) {
               while (($file = readdir($dh)) !== false) {
                   if ($file == "." || $file == "..") continue;
                   $fullpath = $path . "/" . $file;
                   $fkey = strtolower($file);
                   while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
                   $a = stat($fullpath);
                   $files[$fkey]['size'] = $a['size'];
                   if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
                   else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
                   else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
                   else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
                   $files[$fkey]['name'] = $file;
                   $files[$fkey]['type'] = filetype($fullpath);
                   $fileNames[$i++] = $fkey;
               }
               closedir($dh);
           } else die ("Cannot open directory:  $path");
       } else die ("Path is not a directory:  $path");
       sort($fileNames,SORT_STRING);
       $sortedFiles = array();
       $i = 0;
       foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f];
      
       return $sortedFiles;
    }
    
    $files = getFiles("/home/users/web/b84/ipw.amandeepmail/public_html/turboupload/php/$Movie/");
    foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">$file[name]</a></b></li>");
    
    echo "</ul>"; 
    
    print("<b>Full Album (ZIP Format)-</b><br/><a href=\"http://www.turboupload.co.in/$Movie.zip\">Click Here</a>");
    
    ?>
    Code (markup):
     
    amandeepmail, Sep 1, 2008 IP
  2. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    function getFilenameWithoutExt($filename){
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    ?>
    PHP:
    OR

    $arr = explode(".", $allname);
    $filename = $arr[0]; 
    PHP:
    OR, if you have a dotted filename:
    $filename = preg_replace( '/\.[a-z0-9]+$/i' , '' , 'dotted.file.Name' );
    PHP:
    OR

    $FileNameTokens = explode('.', $allname);
    $fileName = implode(".", array_slice($FileNameTokens, 0, count($FileNameTokens) - 1)); 
    PHP:
    JUST CHOOSE THE BETTER YOU FEEL !

    I hope this will help you :)
     
    Ilyes, Sep 1, 2008 IP
  3. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK I used the script in my file now how to implement it. I mean how can I display file name without extension (ie use this function in print?)
     
    amandeepmail, Sep 1, 2008 IP
  4. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">$file[name]</a></b></li>");
    PHP:
    I'm not sure how this script works, so try that:

    foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/" . preg_replace( '/\.[a-z0-9]+$/i' , '' , $file[name] )  . "', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">$file[name]</a></b></li>");
    PHP:
    I hope thiw works :D
     
    Ilyes, Sep 1, 2008 IP
  5. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry to say that but it didn't work.

    The 1st option in which you defined the function
    <?php
    function getFilenameWithoutExt($filename){
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    ?>
    Code (markup):
    Can you tell the way by which I can implement the value generated for filename by this funtion in print where there is anchor text.
     
    amandeepmail, Sep 1, 2008 IP
  6. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry ! I changed the path in the link and not the anchor text !
    Try this:
    foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">" . preg_replace( '/\.[a-z0-9]+$/i' , '' , $file[name] )  . "</a></b></li>");
    PHP:
     
    Ilyes, Sep 1, 2008 IP
  7. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you would like to use the first option:
    Put the function at the beginning of the php code ( after the <?php ).
    then put this:
    foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">" . getFilenameWithoutExt($file[name]) . "</a></b></li>");
    PHP:
    Hope this works :)
     
    Ilyes, Sep 1, 2008 IP
  8. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Nope not working neither the upper one. We are using the quotes so it stops the printing function I think.

    Files are at- http://www.turboupload.co.in/php/download/main.php (Main File which includes script.php)
    http://www.turboupload.co.in/php/download/download.php (This is the script)
     
    amandeepmail, Sep 1, 2008 IP
  9. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I tested it now in my server and it's working. What do you see in the anchoer text ? or there are no anchor text ? Or all the print function don't work ?

    I tested it like that:
    
    <?php
    
    function getFilenameWithoutExt($filename){
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    
    echo "<b>Individual Songs</b><br/>Right Click and Save Target as to Download<br/><ul>";
    
    function getFiles($path) {
       //Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
       //sorted by the file name (case insensitive).  If two files are identical when compared without case, they will sort
       //relative to each other in the order presented by readdir()
       $files = array();
       $fileNames = array();
       $i = 0;
      
       if (is_dir($path)) {
           if ($dh = opendir($path)) {
               while (($file = readdir($dh)) !== false) {
                   if ($file == "." || $file == "..") continue;
                   $fullpath = $path . "/" . $file;
                   $fkey = strtolower($file);
                   while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
                   $a = stat($fullpath);
                   $files[$fkey]['size'] = $a['size'];
                   if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
                   else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
                   else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
                   else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
                   $files[$fkey]['name'] = $file;
                   $files[$fkey]['type'] = filetype($fullpath);
                   $fileNames[$i++] = $fkey;
               }
               closedir($dh);
           } else die ("Cannot open directory:  $path");
       } else die ("Path is not a directory:  $path");
       sort($fileNames,SORT_STRING);
       $sortedFiles = array();
       $i = 0;
       foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f];
      
       return $sortedFiles;
    }
    
    $files = getFiles("Y:/");
    foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">" . getFilenameWithoutExt($file[name]) . "</a></b></li>");
    
    echo "</ul>"; 
    
    print("<b>Full Album (ZIP Format)-</b><br/><a href=\"http://www.turboupload.co.in/$Movie.zip\">Click Here</a>");
    
    ?>
    
    
    PHP:
    Try to copy it ( no problem to copy from Quote), don't forget to change you path !
     
    Ilyes, Sep 1, 2008 IP
  10. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10

    Not working. Please help. It only prints the first echo and no array. The printing function needs a closing quote to stop printning and we have given a closing quote at ile[name]\">
    "
    Code (markup):
    this stops the function and no printing takes place. Please help.
     
    amandeepmail, Sep 1, 2008 IP
  11. ForumJoiner

    ForumJoiner Active Member

    Messages:
    762
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    83
    #11
    The code you posted worked perfectly my computer.

    I use
     $files = getFiles("c:/");
    PHP:
    for the path. Could you, please, reply with the string you used for the path?
     
    ForumJoiner, Sep 2, 2008 IP
  12. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It is not working I use path-
    $files = getFiles("/home/users/web/b84/ipw.amandeepmail/public_html/turboupload/php/$Movie/");
    PHP:
    which is fine and works fine if I remove that remove extension function.
     
    amandeepmail, Sep 2, 2008 IP
  13. ForumJoiner

    ForumJoiner Active Member

    Messages:
    762
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    83
    #13
    What is the value of the variable $Movie, from the end of your path?
     
    ForumJoiner, Sep 2, 2008 IP
  14. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Value of $Movie is Aksar which is given in other php file that is main.php please see my other post above. Now I have removed that
    and replaced it twith $file[name] and it has started working and also displays the expension .php

    My file main.php is at - http://www.turboupload.co.in/php/download/main.php

    Can you suggest some other way to remove that extension because this method dosen't work.
     
    amandeepmail, Sep 2, 2008 IP