chetan
chetan

Reputation: 1395

Providing download links in php

I recently uploaded videos,audios and pdf files using php to my multimedia folder on my server.

Basically its a site providing tutorials on various engineering subjects. I have kept folders like mechanics, computer programming etc and stored files in them accordingly, now i want to provide a link to the files using hyperlinks so that user can view them.

How can i achieve this?

Upvotes: 1

Views: 276

Answers (4)

user1031030
user1031030

Reputation:

Well, I believe there are two approaches.

One is create a loop for respected directories:

<? php
$yourDirectory = "../path/to/your/directory/";
if (is_dir($yourDirectory )) {
    if ($reading = opendir($yourDirectory)){
        while (($files = readdir($reading)) !== false){
            if( $files != "." && $files != ".." && $files[0] != "." ){
               echo "<a href='fancybox'><img src='$files' alt='' /></a>";
            }
        }
        closedir($reading);
    }
}
?>

by this way you can view your videos in Fancybox. Of course you need to set Fancybox plugin first.

Other way is using FlowPlayer to play content in your pages.

Hope this helps.

Upvotes: 2

Rupesh Pawar
Rupesh Pawar

Reputation: 1917

To show files to user use this.

$dir    = '/mechanics';
$files = scandir($dir);

You will get the files array. And you can acces them as files[0], files[1 ].

Now if you want to give them facility to download then use this.

<?php
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('huge_document.pdf');
?> 

For more detail see this

Upvotes: 4

CodeCaster
CodeCaster

Reputation: 151588

The easiest way to do this is to enable directory listing, by putting Options +Indexes in an .htaccess file. This way, all files in that directory will be shown by your web server as a listing.

You could also look at dir() or DirectoryIterator.

Upvotes: 3

0xDEADBEEF
0xDEADBEEF

Reputation: 3431

http://www.php.net/scandir

with this function you can scan all files and directories in a folder. with the return value you can generate in a for-loop hyperlinks

Upvotes: 2

Related Questions