Php Misconfiguration Please help

Discussion in 'PHP' started by amandeepmail, Aug 31, 2008.

  1. #1
    I have two php files-

    1st file (script.php) -
    <?php echo "<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("./../$Movie/");
    foreach ($files as $file) print "<li><b><a href=\"http://www.turboupload.co.in/$Movie/$file[name]\">$file[name]</a></b></li>";
    
    echo "</ul>"; ?>
    Code (markup):
    The file script.php is stored at 'http://www.turboupload.co.in/php/script.php' .

    Second File (main.php) -
    <?php
    
    $Movie = 'Apne';
    
    include 'script.php';
    
    ?>
    Code (markup):
    main.php is stored at 'http://www.turboupload.co.in/php/main.php' and 'http://www.turboupload.co.in/main.php'

    Now when I execute main.php from /php/ Directory (http://www.turboupload.co.in/php/main.php) it works fine. ie it bring the list of files from directory /Apne/ but when I execute main.php from home directory (http://www.turboupload.co.in/main.php) it dosen't work. script.php dosen't take the value of $Movie and displays content of home directory.

    Please help me out guys I am trying to solve it for last 5-6 Hours..
     
    amandeepmail, Aug 31, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ("./../$Movie/");
    This is your problem. you should have something like
    ("/path/to/home/dir/$Movie/")
     
    JAY6390, Aug 31, 2008 IP
  3. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried "http://www.turboupload.co.in/$Movie"

    but I get error that http://www.turboupload.co.in/Apne is not a valid directory
     
    amandeepmail, Aug 31, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's because that is a URL, not a directory path
    If you are on a linux machine it will look like
    /home/yourusername/www/
    or
    /home/yourusername/public_html/
     
    JAY6390, Aug 31, 2008 IP
  5. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok I changed it to
    $files = getFiles("/home/users/web/b84/ipw.amandeepmail/public_html/turboupload/$Movie/");
    Code (markup):
    and the file main.php is in folder turboupload with contents
    <?php
    
    $Movie = 'Apne';
    
    include 'http://www.turboupload.co.in/php/script.php';
    
    ?>
    Code (markup):
    It is still not working
     
    amandeepmail, Aug 31, 2008 IP
  6. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    One thing I noticed that when I use 'script.php' in main.php include I get the desired results. When I use http://fullpath I don't get the results.
     
    amandeepmail, Aug 31, 2008 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    you CANNOT use a URL to include a php file. change the include in the main.php so it is
    include '/home/users/web/b84/ipw.amandeepmail/public_html/turboupload/php/script.php';
    PHP:
     
    JAY6390, Aug 31, 2008 IP
    amandeepmail likes this.
  8. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It worked. Can you also tell me one more thing. I want to display only links to MP3 Files instead of all files in directory. Is this possible?
     
    amandeepmail, Aug 31, 2008 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    check out php.net/glob
     
    JAY6390, Aug 31, 2008 IP