简体   繁体   中英

Turning a html input value into a $variable for a download function

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.

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)

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';
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM