Search code examples
phpuploadify

Passing file types, dates with PHP echo into table


I'm still new to PHP and I'm trying to echo files in a folder to a table row. I can successfully echo the files on the server, but everything I try for the file type isn't working. So far my code looks like this:

<?PHP 

$folder = "uploads/"; 
$handle = opendir($folder); 


# Making an array containing the files in the current directory: 
while ($file = readdir($handle)) 
{ 
    $files[] = $file; 
} 
closedir($handle); 

#echo the files 
$path_parts = pathinfo($file);
foreach ($files as $file) { 
    echo "

    <tr><td><a href=$folder$file>$file</a></td><td>$path_parts['extension']</td><td>date</td>   </tr>"
    ; 
} 
?>

With that set up the way it is, it gives me a server error. When I take out the path parts/path info code it works fine, but of course it won't echo the file type. Any help with this would be much appreciated, and if anyone knows the best way to implement the file upload date as well that would be great. By the way, using uploadifive as the uploader if that makes any difference.


Solution

  • I have done some slight modifications to your code:

    <?php
    $folder = "uploads/"; 
    $handle = opendir($folder); 
    
    # Making an array containing the files in the current directory: 
    while ($file = readdir($handle)) 
    { 
        $files[] = $file; 
    } 
    closedir($handle); 
    
    foreach ($files as $file) 
    {
        #echo the files 
        $path_parts = pathinfo($file);
        echo "<tr><td><a href='" . $folder . $file . "'>" . 
             $file . "</a></td><td>" . $path_parts['extension'] . 
             "</td><td>date</td></tr>";
    } 
    

    Mostly, the $path_parts = pathinfo($file); has to be in the loop itself. Otherwise, you are executing the pathinfo only on the last $file from the loop.