2

This site has been immenselly helpful to me so far, but it seems I've finally come up against a problem. The question how to put a variable in a html input has been asked and awnsered before, but there's an issue I just can't seem to get to work.

<?php
if(isset($_POST['file_name'])){
    $file = $_POST['file_name'];
    header('Content-Disposition: attachment; filename="'.$file.'"');
    readfile('uploads/'.$file);
    exit();
}
?>
<form action="force_download.php" method="post" name="downloadform">
  <input name= "file_name" type="hidden" value="test.txt" />
  <input type="submit" value="Download">
</form>

The above code is being used to download the the text file "test.txt" One of many text files in my upload folder. My issue? How do I turn the value test.txt into a variable? A previous awnser I've read would be this:

<input type="hidden" name="file_name" value="<?php echo htmlspecialchars($file); ?>" />

The problem is that when I do that, it downloads force_download.php instead of any of my files.

I only started coding a few days ago, so yeah... sorry for the newbie question.

  • Duplicate of: http://stackoverflow.com/questions/171318/how-do-i-capture-php-output-into-a-variable – Rob W Jun 25 '13 at 21:45
  • you mean you want the form action to be the file name inserted in the input by the user? – Sergio Jun 25 '13 at 21:45
  • 1
    not really sure what you are asking but perhaps file_get_contents() –  Jun 25 '13 at 21:48
  • The above code is used to download a file from my website. Right now I'm downloading test.txt. This works fine! I get the test.txt no problem. However, I've got multiple files on my website, and I'd like all of them to have a download button. I figured, to do this I had to turn my value="test.txt" into a value=$file code. I tried doing this with the line you see underneath my main code. However this results in me downloading a "force_download.php" file, instead of any of the files I've uploaded on my website. – Dennis Dekker Jun 25 '13 at 21:53
  • You need to use javascript in order to change a DOM object's property – php_nub_qq Jun 25 '13 at 21:55
  • Oh god, that sounds really complicated. Haven't had java yet. Well thanks for the advice guys. I'm afraid I'm going to fail my grade, but so be it. There's still a lot I need to learn. =) – Dennis Dekker Jun 25 '13 at 21:56
  • What code are we seeing? is it "force_download.php"? – Sergio Jun 25 '13 at 21:57
  • Oh yes indeed it is! My mainfile intranet.php contains this code which links to the force_download.php that you see in my original post. http://pastebin.com/ZDGZ6eH2 As you can (hopefully) see. This code is used to display any uploaded .txt, .docx and .pfd files I've uploaded to my "Uploads" folder. Then at line 17 it referes to force_download.php – Dennis Dekker Jun 25 '13 at 22:03
  • @DennisDekker It just sounds complicated, it really isn't. And note that java and javascript are two entirely different languages :P – php_nub_qq Jun 25 '13 at 22:03
  • Y'know what guys. I spend a good 60 hours over the past 6 days working on this intranet. I've learned so much in this time, but it's past midnight now and the only thing keeping me awake is the stress. I'm going to bed, have my assessement tomorrow and get feedback. With a little luck they'll give me a chance to try again next week, so I'll be sure to keep lurking around this thread. Just wanted to let you know that I really appreciate the quick replies! Makes the love the internet even more! -Dennis – Dennis Dekker Jun 25 '13 at 22:12

2 Answers2

0

Here is one suggestion, you might need to do some ajustments.

In the file calling this one you posted the code to:
(if I understood good: intranet.php)

<form action="force_download.php" method="post" name="downloadform">
<?php
$path = "./filesfolder/"; // this you need to change to your files folder

function createDir($path)
{   
    if ($handle = opendir($path)) 
    {
        while (false !== ($file = readdir($handle))) 
        {
            if (is_dir($path.$file) && $file != '.' && $file !='..')
                printSubDir($file, $path, $queue);
            else if ($file != '.' && $file !='..')
                $queue[] = $file;
        }
        printQueue($queue, $path);
    }
}
function printQueue($queue, $path)
{
    foreach ($queue as $file) 
    {
        printFile($file, $path);
    } 
}
function printFile($file, $path)
{
    if ($file=="thumbs.db") {echo "";}
    else 
    {
    echo "
<input name= \"".$file."\" type=\"hidden\" value=\"".$file."\" />";
    }
}
createDir($path);
?>
<input type="submit" value="Download">
</form>

This will generate the html for all files it finds, with the clickable link to each file. In this case you don't need the in the file you posted (which I think is force_download.php)

Sergio
  • 28,539
  • 11
  • 85
  • 132
0

Here a function I wrote and use for a while, please consider adding the a routine for the correct contect-type as well:

function downloadFile($fullPathToFile, $renameFile=''){
        if(is_file($fullPathToFile)){
            $path_parts = pathinfo($fullPathToFile);
            $dirname =  $path_parts['dirname'];
            $basename = $path_parts['basename'];
            $extension = $path_parts['extension'];
            $filename = $path_parts['filename'];
            $filesize = filesize($fullPathToFile);

            if(!empty($renameFile)){
                $extension = preg_match("/\.([^\.]+)$/", $renameFile, $matches);
                $extension = preg_replace('/\./', '', $matches[0]) ;
            } 
                    /*Change the following one line to type of files or comment out*/
            header("Content-Type: text/plain"); 
            header ("Content-Length: " . $filesize );
            header("Pragma: ");
            header("Cache-Control: ");
            if($renameFile){
                header("Content-Disposition: attachment; filename=\"$renameFile\"");
            }else{
                header("Content-Disposition: attachment; filename=\"$basename\"");
            }
            ob_clean();
            flush();
            readfile($dirname.'/'.$basename);
            die();
        }
        else {
            echo 'no such file or directory';
        }
}
Perb
  • 31
  • 2