Jump to content

Opening video file from folder


captain_scarlet87

Recommended Posts

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!  :confused:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.