Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:43:12+00:00 2026-06-11T19:43:12+00:00

I am having trouble writing a single PHP script that displays an HTML text-box

  • 0

I am having trouble writing a single PHP script that displays an HTML text-box that will search for a file in the /tmp directory of a Linux machine. Once the user types the string into the text box and presses the submit button, matching hyperlinks will be presented underneath the textbox with names of the files outside the web directory (in /tmp).

When I click the hyperlink, I want to download the file from the /tmp directory. Unfortunately, because I need to use a single php script, HTML content is being appended to the file since I am using the php header command.

Is there any way I can download the file without having html from my php page appended to the file and without using more than a single php script?

<?php


function displayfiles()
{
    echo '<ul>';

    if ($handle = opendir('/tmp')) 
    {
        while ($file = readdir($handle))
        {
            if ($file != "." && $file != ".." && (strstr($file, $_POST['searchbox'])))
            {
                echo '<li><a href="search.php?file='.$file.'">'.$file.'</a></li>';
            }
            else if ($file != "." && $file != ".." && (eregi($file, $_POST['searchbox'])))
            {
                echo '<li><a href="search.php?file='.$file.'">'.$file.'</a></li>';  
            }
        }
        closedir($handle);
    }
    echo '</ul>';
}

function downloadfile($filename)
{            
    header("Content-disposition: attachment; file='/tmp/$filename' filename=$filename");
        header("Content-Length: " . filesize("/tmp/$filename"));
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile("/tmp/$filename");
    exit(); 
}


echo 
"<html>
<head>
<title>File Search</title>
</head>
<body>
        <form method='POST'>
        <input id='searchbox' name='searchbox' id='searchbox' type='textbox' />
        <input type='submit' name='submit' />
        </form>";

                if (isset($_POST['submit']))
                {
                    displayfiles();        
                }
                else if (isset($_GET['file']) && file_exists("/tmp/".$_GET['file']))
                {
            downloadfile($_GET['file']);
        }

 echo "</body></html>";
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:43:14+00:00Added an answer on June 11, 2026 at 7:43 pm

    You need to handle the output of the file-download before outputting anything else, quick and dirty example (even only with one function instead of two as suggested in the comment, I think you get the idea):

    <?php
    
    function action_download() {
        if (!isset($_GET['file'])) return FALSE;
        $file = sprintf('/tmp/%s', $_GET['file']);
        $file = realpath($file);
        if (substr($file, 0, 5) !== '/tmp/') return FALSE;
        if (!is_file($file) || !is_readable($file)) return FALSE;
    
        header("Content-disposition: attachment; filename='".basename($file)."'");
        header("Content-Length: " . filesize($file));
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile($file);
        return TRUE;
    }
    
    if (action_download()) return;
    
    ?>
    <head>
    <title>File Search</title>
    </head>
    <body>
            <form method='POST'>
            <input id='searchbox' name='searchbox' id='searchbox' type='textbox' />
            <input type='submit' name='submit' />
            </form>
    <?php
                    if (isset($_POST['submit']))
                    {
                            echo '<ul>';
    
                             if ($handle = opendir('/tmp')) {
                                    while ($file = readdir($handle))
                                    {
                                      if ($file != "." && $file != ".." && (strstr($file, $_POST['searchbox'])))
                                      {
                                            echo '<li><a href="search.php?file='.$file.'">'.$file.'</a></li>';
                                      }
                      else if ($file != "." && $file != ".." && (eregi($file, $_POST['searchbox'])))
                      {
                            echo '<li><a href="search.php?file='.$file.'">'.$file.'</a></li>';  
                      }
                                    }
                              closedir($handle);
                              }
                            echo '</ul>';
                    }
    ?>
    </body></html>
    

    Also take care that there can be data inside /tmp/ that should not be offered for download as it can be used to attack applications, their users or even the system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble writing a regular expression (suitable for PHP's preg_match()) that will parse
I am having trouble writing C++ code that uses a header file designed for
I am having some trouble writing a regex expression that will strip outer brackets
I am having trouble writing query so that I can query the content of
I'm having trouble reading and writing QByteArray data to a file. My goal is
I am having some trouble with writing a unit test that checks my custom
I am writing a code generation tool that will take in a XSD file
I'm having trouble writing an XPath expression to select nodes that contain certain elements,
I'm using SQL Server MS. I'm having trouble writing this script: CREATE VIEW rental_view
I am having trouble writing a sql query and I was hoping that someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.