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

  • SEARCH
  • Home
  • 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 8073305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:17:54+00:00 2026-06-05T14:17:54+00:00

I am trying to make a script that will let me download files outside

  • 0

I am trying to make a script that will let me download files outside my web root directory and list the files…It is listing the files of the directory but it does not allow me to download them as when they are clicked it just goes to website.com/clickedlink.jpg but it needs to some how change that to go to the location I would like…

I think I am explaining this a little odd so please tell me if it needs clarification.

I need to download files from /home/files/I/need but the website is stored in /var/website/

so if someone could please help out I’d be 100% thankful!

<html><head><title>Root page</title></head><body>


<? 
// open this directory 
$myDirectory = opendir("/home/files/I/need");

// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
        print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<td>");
        print(filetype($dirArray[$index]));
        print("</td>");
        print("<td>");
        print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}
print("</TABLE>\n");
?>
</body></html>


</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-05T14:17:56+00:00Added an answer on June 5, 2026 at 2:17 pm

    The best solution is to let your web server take care of this by creating a location alias; having the web server perform the downloading for you is much more scalable than doing this in PHP.

    However, since you asked for a PHP solution, add this to this top of your script:

    $basePath = "/home/files/I/need";
    if (isset($_SERVER['PATH_INFO']) && $file = basename($_SERVER['PATH_INFO'])) {
        if (file_exists("$basePath/$file")) {
            // add file size
            header('Content-Length: ' . filesize("$basePath/$file"));
            // write file to output
            readfile("$basePath/$file");
            return;
        }
    }
    

    To construct the URL to make the download:

    /path/to/your/script.php/clickedlink.jpg
    

    The /path/to/your/script.php is the same URL that generates the page; you append a slash, together with the intended filename.

    I’m using basename() to prevent people from accessing ../../../etc/passwd or some other file they shouldn’t be seeing, but that also means you can’t use sub folders. If you need to support sub folders, you have to sanitize the PATH_INFO in a different manner.

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

Sidebar

Related Questions

I'm trying to make a script that will go into a directory and run
Im trying to make a script that looks for all files in a directory
I'm trying to make a script that will prompt the user and wait for
I'm trying to make a PHP script that will take a potentially infinite number
I'm trying to make a PHP script that will check the HTTP status of
OK basicly what I am trying to do is make a script that will
hello everyone I am trying to make a script that steps through a list
I'm trying to write a shell script that will make several targets into several
I'm trying to make a simple login script that will check the inputted username
I'm trying to make a jQuery script that will hide all the visible elements

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.