captain_scarlet87 Posted February 3, 2010 Share Posted February 3, 2010 Hey, I've created a website whereby videos files (avi) are uploaded to a folder. I now need a php script to display all the video files in the folder to the user who can then click on and open the files. I've done a number of google searches and have got this script to display all the links to the files but currently can't open them: <?php $current_dir = "$DOCUMENT_ROOT"."includes/videos"; //Put in second part, the directory - without a leading slash but with a trailing slash! $dir = opendir($current_dir); // Open the sucker echo ("<p><h1>List of available files:</h1></p><hr><br />"); while ($file = readdir($dir)) // while loop { $parts = explode(".", $file); // pull apart the name and dissect by period if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part $extension = end($parts); // set to we can see last file extension if ($extension == "avi" OR $extension == "zip") // is extension ext or EXT ? echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want } } echo "<hr><br />"; closedir($dir); // Close the directory after we are done ?> Any help would be great, although try to keep it simple as i'm no mastermind at this stuff! Quote Link to comment Share on other sites More sharing options...
jammesz Posted February 3, 2010 Share Posted February 3, 2010 try hard coding: if ($extension == "avi" OR $extension == "zip") // is extension ext or EXT ? echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; It should look like this: <?php $current_dir = "./"; //Put in second part, the directory - without a leading slash but with a trailing slash! $dir = opendir($current_dir); // Open the sucker echo ("<p><h1>List of available files:</h1></p><hr><br />"); while ($file = readdir($dir)) // while loop { $parts = explode(".", $file); // pull apart the name and dissect by period if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part $extension = end($parts); // set to we can see last file extension if ($extension == "avi" OR $extension == "zip"){ // is extension ext or EXT ? echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want } } } echo "<hr><br />"; closedir($dir); // Close the directory after we are done ?> I've tested this on my server and it works fine. note: you need to change the $current_dir back to what you have Quote Link to comment Share on other sites More sharing options...
captain_scarlet87 Posted February 3, 2010 Author Share Posted February 3, 2010 It's picking up the file names from correct folder (includes/videos/) however the file links are only pointing to the current directory and therefore the files cannot be found. What do I need to change to link to the sub folders (includes/videos/)? Thanks. Quote Link to comment Share on other sites More sharing options...
importune Posted February 3, 2010 Share Posted February 3, 2010 Hey guys! Jammesz's update works fine if your in the same directory as the video files, because of the fact that the url doesn't need the call to the directory. Heres an updated version for you, this should fix your problem : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>my php playground</title> </head> <body> <?php $current_dir = "images"; //Put in second part, the directory - without a leading slash but with a trailing slash! $dir = opendir($current_dir); // Open the sucker echo ("<p><h1>List of available files:</h1></p><hr><br />"); while ($file = readdir($dir)) // while loop { $parts = explode(".", $file); // pull apart the name and dissect by period if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part $extension = end($parts); // set to we can see last file extension if ($extension == "avi" OR $extension == "zip"){ // is extension ext or EXT ? echo "<a href=\"$current_dir/$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want } } } echo "<hr><br />"; closedir($dir); // Close the directory after we are done ?> </body> </html> Notice the change on the echo line. Tested this and works great with a list of png images (when I change the file extension, of course). Let us know if there are any more problems! Quote Link to comment Share on other sites More sharing options...
captain_scarlet87 Posted February 3, 2010 Author Share Posted February 3, 2010 Thanks importune, works exactly how I want it to now. 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.