• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

php script - download

I have a request where i have to create php file to open /var/www/site1/upload directory and able to download files using php. Please advise.




<html>

<form method="post" action="download.php">
                                <p>These are the files inside the upload directory:</p>

                                <p>

                        <select name="file_in_folder" id="file_in_folder">
                                <option value="">...</option>
                                <option value="sed.pdf">sed</option>
                                <option value="Barnsley_fern_plotted_with_VisSim.PNG">Barnsley_fern_plotted_with_Vis...</option>
                                <option value="sed.pdf">sed</option>
 
                        </select>
                                        <input type="submit" name="download" value="Download" />
                                </p>
                        </form>

</html>

======================================================================


<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/upload/"; // change the path to fit your websites document structure
//$fullPath = $path.$_GET['download_file'];
$fullPath = $path

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
<a href="download.php?download_file=sed.pdf">Download here</a>
?>
0
ittechlab
Asked:
ittechlab
  • 12
  • 8
  • 2
  • +1
1 Solution
 
gr8gonzoConsultantCommented:
It looks fine to me except this line:

$fullPath = $path

That should have a filename on the end of it, like:

$fullPath = $path . "/" . str_replace(array("..","/","\\"),"",$_POST["file_in_folder"]);
0
 
ittechlabLinux SupportAuthor Commented:
actually i removed the $fullPath = $path

still not working.
0
 
ittechlabLinux SupportAuthor Commented:
<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/upload/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
<a href="download.php?download_file=some_file.pdf">Download here</a>
?>
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
philkeeneCommented:
Hi,

Please could you clarify your question. You seem to have a request and an answer in your question.

Do you mean there is a problem with the supplied code, and if so please can you specify what results you are getting?

Or are you asking for a way to do it and giving us an answer to comment on?

I think if you can provide a little more information you will almost certainly get a useful answer to your question.

Thanks,

Phil
0
 
gr8gonzoConsultantCommented:
$fullPath = $path.$_GET['download_file'];
...is not going to work, because you do not use $_GET["download_file"] according to your form.

Your form uses a <select> box with a name of "files_in_folder", so when you submit the form after selecting a file in the dropdown, you are going to end up with your selected item inside the $_POST["files_in_folder"] variable.

So remove:
$fullPath = $path.$_GET['download_file'];

And replace with:
$fullPath = $path . str_replace(array("..","/","\\"),"",$_POST["file_in_folder"]);
0
 
philkeeneCommented:
Ignore my comment there were no other comments when I started writing that...

Good luck...
0
 
Mohamed AbowardaSoftware EngineerCommented:
Are you trying to allow downloading PHP files instead of parsing in certain directory?

If so you can use .htaccess to disable PHP parsing in certain directory:
http://www.electrictoolbox.com/disable-php-apache-htaccess/

Or you can use the file_get_contents() to get the contents of any file:
http://php.net/manual/en/function.file-get-contents.php
0
 
ittechlabLinux SupportAuthor Commented:
i did replace this line still not working.

$fullPath = $path . str_replace(array("..","/","\\"),"",$_POST["file_in_folder"]);


let me clear my objective,

I am trying to access /var/www/mysite folder from web and trying to get a list of files first.

from the list if i select the file , i want to be able to download.

Please assist.
0
 
gr8gonzoConsultantCommented:
So what is the full code at this point?
0
 
ittechlabLinux SupportAuthor Commented:
Here is my download.php file

<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/upload/"; // change the path to fit your websites document structure
$fullPath = $path . str_replace(array("..","/","\\"),"",$_POST["file_in_folder"]);
//$fullPath = $path.$_GET['download_file'];

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
<a href="download.php?download_file=some_file.pdf">Download here</a>
?>
0
 
ittechlabLinux SupportAuthor Commented:
here is my download.html file

<html>

<form method="post" action="download.php">
                                <p>These are the files inside the upload directory:</p>

                                <p>

                        <select name="file_in_folder" id="file_in_folder">
                                <option value="">...</option>
                                <option value="sed.pdf">sed</option>
                                <option value="Barnsley_fern_plotted_with_VisSim.PNG">Barnsley_fern_plotted_with_Vis...</option>
                                <option value="back.png">back.png</option>

                        </select>
                                        <input type="submit" name="download" value="Download" />
                                </p>
                        </form>

</html>
0
 
ittechlabLinux SupportAuthor Commented:
please correct the code
0
 
gr8gonzoConsultantCommented:
Okay, what is your folder/file structure look like?
0
 
ittechlabLinux SupportAuthor Commented:
root@ubuntu:/var/www/mysite/download# ls -l
total 188
-rw-r--r-- 1 root root 22304 Aug 21 13:03 apt-get.pdf
-rw-r--r-- 1 root root 44509 Aug 21 13:02 awk.pdf
-rw-r--r-- 1 root root  5941 Aug 21 13:02 cut.pdf
-rw-r--r-- 1 root root 11795 Aug 21 13:02 ls.pdf
-rw-r--r-- 1 root root 47579 Aug 21 13:03 mysql.pdf
-rw-r--r-- 1 root root 13753 Aug 21 13:02 sed.pdf
-rw-r--r-- 1 root root  8919 Aug 21 13:02 sort.pdf
-rw-r--r-- 1 root root 19025 Aug 21 13:03 vi.pdf


root@ubuntu:/var/www/mysite/download# pwd
/var/www/mysite/download

I have many pdf files which i would like to get a listed in drop down box when i go to download.html and able to select the file and download.

download.php code is working but i am not sure how to get a list of files in the dropdown and select to download
0
 
gr8gonzoConsultantCommented:
Okay, change $path to:
$path = "/var/www/mysite/download/";

In your list screen, do something like this:

<form method="post" action="download.php">
                                <p>These are the files inside the upload directory:</p>

                                <p>

                        <select name="file_in_folder" id="file_in_folder">
<?php
$dir = glob("/var/www/mysite/download/*");
$dh = opendir($dir);
while(($file = readdir($dh)) !== false)
{
  if(($file == ".") || ($file == "..")) continue;
  $fullfile = $dir . $file;
  if(!is_dir($fullfile))
  {
    echo "<option value='{$file}'>{$file}</option>\n";
  }
}
closedir($dh);
?>
                        </select>
                                        <input type="submit" name="download" value="Download" />
                                </p>
                        </form>
0
 
ittechlabLinux SupportAuthor Commented:
i used above code, why i am getting list of files inside the /var/www/mysite/download/

i checked the permission and everything looks fine.
0
 
gr8gonzoConsultantCommented:
Isn't that what you are trying to get?
0
 
ittechlabLinux SupportAuthor Commented:
sorry i mean to say i am not getting a list of lists in the drop down list.
0
 
ittechlabLinux SupportAuthor Commented:
I am not getting a list files. please assist me.
0
 
ittechlabLinux SupportAuthor Commented:
Here is the new php script. i want to know how to convert to show drop down menu.


<?php
$pathtodl = 'download';
if ($handle = opendir($pathtodl)) {
  while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..")
        $listoffiles .= '<li><a href="'.$pathtodl.'/'.$file.'">'.$file."</a></li>";
  }
  closedir($handle);
}
?>
<ol>
<?php echo $listoffiles ?>
</ol>
0
 
gr8gonzoConsultantCommented:
<?php
$pathtodl = 'download';
if ($handle = opendir($pathtodl)) {
  while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..")
        $listoffiles .= '<option value="'.$file.'">'.$file."</option>";
  }
  closedir($handle);
}
?>
<select name='file_in_folder'>
<?php echo $listoffiles; ?>
</select>
0
 
ittechlabLinux SupportAuthor Commented:
Thanks
0
 
gr8gonzoConsultantCommented:
Is there a reason you gave a grade of B instead of an A? Anything less than an A usually means that you are dissatisfied with the answer or that it didn't answer the question...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 8
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now