Talon21 Posted June 1, 2008 Share Posted June 1, 2008 Hi all, I'm new to PHP, at the moment I'm working on a script that will scan my Movies folder for example and will create a table with the necessary information. such as a link to IMDB etc... I want to add a new <td> with the File Type information. here is the code: <html> <head> <style> .imdb{ font-family: verdana; font-size:14px; } .imdb td{ border: 1px solid #EFEFEF; } .imdb th { border: 1px solid #EFEFEF; } </style> </head> <center> <body> <table class="imdb"> <tr> <th>Movie</th> <th>IMDB</th> <td> <?php //scaning the movies folder $dir = 'G:\Home\Movies'; $files1 = scandir($dir); $files2 = scandir($dir, 1); for ($a = 0 ;$a < count($files1); $a++) //if conditinon to delete the "." and ".." folders from the table if ($files1[$a] == ".") echo ""; else if ($files1[$a] == "..") echo ""; else //echo the movie table echo '<tr><td>'."$files1[$a]".'</td> <td><a href="http://www.imdb.com/find?q='."$files1[$a]".'&sourceid=mozilla-search">IMDB</a></td></tr>'; ?> </tr> </td> </table> </body> </center> </html> Quote Link to comment Share on other sites More sharing options...
Orio Posted June 1, 2008 Share Posted June 1, 2008 What do you mean by "File type"? It's extension? It's MIME type? Be a bit more specific please. Orio. Quote Link to comment Share on other sites More sharing options...
Talon21 Posted June 1, 2008 Author Share Posted June 1, 2008 Sorry I meant file file extension for example : .avi , .srt , .mp3 etc... Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 1, 2008 Share Posted June 1, 2008 Use pathinfo(): <html> <head> <style> .imdb{ font-family: verdana; font-size:14px; } .imdb td{ border: 1px solid #EFEFEF; } .imdb th { border: 1px solid #EFEFEF; } </style> </head> <center> <body> <table class="imdb"> <tr> <th>Movie</th> <th>IMDB</th> <td> <?php //scaning the movies folder $dir = 'G:\Home\Movies'; $files1 = scandir($dir); $files2 = scandir($dir, 1); for ($a = 0 ;$a < count($files1); $a++) //if conditinon to delete the "." and ".." folders from the table if ($files1[$a] == ".") echo ""; else if ($files1[$a] == "..") echo ""; else $info = pathinfo($files1[$a]); //echo the movie table echo '<tr><td>'."$files1[$a]".'</td> <td><a href="http://www.imdb.com/find?q='."$files1[$a]".'&sourceid=mozilla-search">IMDB</a></td> <td>Extension: '.$info['extension'].'</td></tr>'; ?> </tr> </td> </table> </body> </center> </html> Edit: Don't know why the layout between the [ code ] tags got so messed up. Quote Link to comment Share on other sites More sharing options...
Talon21 Posted June 2, 2008 Author Share Posted June 2, 2008 Hi the badbad I copy and paste the code to my script and it's seems that it's not working all I'm getting is an empty table. Movie IMDB IMDB Extension: Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 2, 2008 Share Posted June 2, 2008 Is it just the extension code that returns blank, or is the whole thing broken? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 2, 2008 Share Posted June 2, 2008 Just found you had some errors in your table construction. You really should indent your code too. I cleaned it up a bit, try this: <html> <head> <style type="text/css"> .imdb{ font-family: verdana; font-size:14px; } .imdb td{ border: 1px solid #EFEFEF; } .imdb th{ border: 1px solid #EFEFEF; } </style> </head> <body> <center> <table class="imdb"> <tr> <th>Movie</th> <th>IMDB</th> <th>Extension</th> </tr><?php //scaning the movies folder $dir = 'G:\Home\Movies'; $files1 = scandir($dir); for ($a = 0 ;$a < count($files1); $a++) { //if conditinon to delete the "." and ".." folders from the table if ($files1[$a] == "." || $files1[$a] == "..") {continue;} //load file info into $info $info = pathinfo($files1[$a]); //echo the movie table echo ' <tr> <td>'.$files1[$a].'</td> <td><a href="http://www.imdb.com/find?q='.urlencode($files1[$a]).'&sourceid=mozilla-search">IMDB</a></td> <td>Extension: '.$info['extension'].'</td> </tr>'; } ?> </table> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
Talon21 Posted June 2, 2008 Author Share Posted June 2, 2008 It's working now, the problem is that it's not giving me the extension for the files inside the folder. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 2, 2008 Share Posted June 2, 2008 Can't see why it shouldn't work. $files1[$a] contains something similar to 'filename.avi', right? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 2, 2008 Share Posted June 2, 2008 Else you can use explode to get the extension: <html> <head> <style type="text/css"> .imdb{ font-family: verdana; font-size:14px; } .imdb td{ border: 1px solid #EFEFEF; } .imdb th{ border: 1px solid #EFEFEF; } </style> </head> <body> <center> <table class="imdb"> <tr> <th>Movie</th> <th>IMDB</th> <th>Extension</th> </tr><?php //scaning the movies folder $dir = 'G:\Home\Movies'; $files1 = scandir($dir); for ($a = 0 ;$a < count($files1); $a++) { //if conditinon to delete the "." and ".." folders from the table if ($files1[$a] == "." || $files1[$a] == "..") {continue;} //split string at dots to get extension $info = explode('.', $files1[$a]); //echo the movie table echo ' <tr> <td>'.$files1[$a].'</td> <td><a href="http://www.imdb.com/find?q='.urlencode($files1[$a]).'&sourceid=mozilla-search">IMDB</a></td> <td>Extension: '.end($info).'</td> </tr>'; } ?> </table> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
Talon21 Posted June 2, 2008 Author Share Posted June 2, 2008 I think that the reason that's not working it's because inside the Movie folder $dir = 'G:/Home/Movies' each movie is located on a separate folder. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 2, 2008 Share Posted June 2, 2008 Oh, I didn't know that. So the title you're echoing is from the directory I see. To get the extension, we then have to scan each directory separately. I guess each movie directory contains a bunch of files (movie + subtitles and such), so we have to distinguish the movie file from the rest of the files. I would check if the current "file" is a directory (is_dir()); if yes then run another scandir(), then run through each file (foreach()) and set the extension if it's one of the following: avi, mpg, mpeg, mp4, mov, wmv (just tell me if you want help adding more types): <?php //get file extension of video file inside the movie directory if (is_dir($dir.'\\'.$files1[$a])) { $files = scandir($dir.'\\'.$files1[$a]); foreach ($files as $file) { $info = explode('.', $file); $ext = strtolower(end($info)); if (in_array($ext, array('avi', 'mpg', 'mpeg', 'mp4', 'mov', 'wmv'))) { $extension = $ext; break; } } } ?> Whole script: <html> <head> <style type="text/css"> .imdb{ font-family: verdana; font-size:14px; } .imdb td{ border: 1px solid #EFEFEF; } .imdb th{ border: 1px solid #EFEFEF; } </style> </head> <body> <center> <table class="imdb"> <tr> <th>Movie</th> <th>IMDB</th> <th>Extension</th> </tr><?php //scaning the movies folder $dir = 'G:\Home\Movies'; $files1 = scandir($dir); for ($a = 0 ;$a < count($files1); $a++) { //if conditinon to delete the "." and ".." folders from the table if ($files1[$a] == "." || $files1[$a] == "..") {continue;} //get file extension of video file inside the movie directory if (is_dir($dir.'\\'.$files1[$a])) { $files = scandir($dir.'\\'.$files1[$a]); foreach ($files as $file) { $info = explode('.', $file); $ext = strtolower(end($info)); if (in_array($ext, array('avi', 'mpg', 'mpeg', 'mp4', 'mov', 'wmv'))) { $extension = $ext; break; } } } //echo the movie table echo ' <tr> <td>'.$files1[$a].'</td> <td><a href="http://www.imdb.com/find?q='.urlencode($files1[$a]).'&sourceid=mozilla-search">IMDB</a></td> <td>Extension: '.$extension.'</td> </tr>'; } ?> </table> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
Talon21 Posted June 3, 2008 Author Share Posted June 3, 2008 Solved! Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.