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);

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");

}

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.

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

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

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

$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);

 

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.