Jump to content

Trying to configure download from a specific directory


r00t0101

Recommended Posts

Hello all,

  I am having a difficult time trying to download a file from a directory. I have tried to research this issue but each time I keep getting an empty text file instead of a ~2mb file. What I would like to have is to place my file (new.txt) in a separate folder. As of now it is under the root directory and it works fine but when I include a path then I get an error file not found.

 

Thanks

 

#########################################

#########################################

$file='/path/new.txt';

$type='text/plain';

 

header("Pragma: public");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false);

header("Content-Type: ".$type);

header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($file));

 

readfile($file);

Link to comment
Share on other sites

What happens when you do this?

 

$file='/path/new.txt';

$type='text/plain';

 

if(file_exists($file)) {

header("Pragma: public");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false);

header("Content-Type: ".$type);

header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($file));

 

readfile($file);

}

else {

die("FILE $file does not exist");

}

Link to comment
Share on other sites

Just use the full path to the site root, or use the relative path.

 

So if the file is in the same directory as your php script calling it just use

 

read("myFilename.txt");

 

Or if it's in the parent directory...

 

read("../myFielname.txt");

 

Get it?

 

If not let me know where your file is in repect to the site root and where your php file is in respect to the server root and I'll tell you what to do.

Link to comment
Share on other sites

excellent, thank you for the help. My layout looks similar to the layout below. Within the example.com domain is the download.php and the file new.txt is in the download folder.

 

/root

    /public_html

        -> /example.com

                  -> /download.php

                  -> /download

                            -> new.txt

Link to comment
Share on other sites

#########################################

#########################################

$file='download/new.txt';

$type='text/plain';

 

header("Pragma: public");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false);

header("Content-Type: ".$type);

header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($file));

 

readfile($file);

 

Link to comment
Share on other sites

excellent, thank you kevinkorb! Ah yea I thought the /download would point to the site_root instead of the server_root. Quick question, Is it possible to create a global site_root variable for this?

 

for example:

/root

    /public_html

        -> /example.com

                  -> /download.php

                  -> /download

                            -> new.txt

 

Could this be done. I would create a global variable named base_dir=/example.com and use $base_dir/download. My goal is to re-do the code, clean it up a bit and to re-organize the source and files into more meaningful names. By cleaning up and updating the code with global variables this would make the source more manageable.

 

so with your update I would like if this could be done. This is probably a bad example but if lets say for example your adding global variables to a source file full of image links where instead of changing all paths you just need to change it in once at the top.

 

base_dir=/example.com

$file='$base_dir/download/new.txt';

$type='text/plain';

 

header("Pragma: public");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false);

header("Content-Type: ".$type);

header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($file));

 

readfile($file);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.