Jump to content

[SOLVED] Temporary Downloads


JohnMC

Recommended Posts

Hi, i think ive found a home for my PHP needs.. this forum and site looks very good, i havnt had an extensive look around yet, but i like what ive seen so far, so to whoever is involved, thank you...

 

 

My scripting issue is that im trying to make a downloadable file thats modified based on arguments in its url...

 

for example  myscript.php?data=helloworld    would download myfile.txt  but with "helloworld" apended to the end...

 

my questions are,

 

1. is it most effecient to have the script copy the original file then modify the copy then echo a link to the new file? if so how would i go about making the new file temporary? i dont want it hanging around.

 

2. im having trouble finding information on the system temporary directory, should i use this? i know it gets used for uploads then the file is moved to the requested path, but what about the other way around? copy original > modify copy > move to system temp > initiate download > system automaticly removes file?

 

i hope im asking the right questions! thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/153668-solved-temporary-downloads/
Share on other sites

how holy crap fast reply, thanks!  that looks like exactly what i want...  but how would i initiate i download to a file thats in memory like that? this is something ive never done before, i would normaly just use a html frame or forward to the url, but i dont have a url in this case, right?

Yeah, I don't have much of a life  :-*

Something like this..?

 

<?php
$file = 'php://temp/file.txt';
fopen($file, 'w');
//$file now = the temp 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: text/plain');
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
readfile($file);
?>

lol well thanks, if you every have an interest in Autoit (http://autoitscript.com) let me know, im do rather well at that...

 

so ive been playing around with the code, cant seem to get it to work right, it comes back with: "stat failed for php://temp/about.html" from the second filegetsize, just for test purposes i havnt modified the data, im just trying to pass the file.

 

$file_name="about.html";
$file_template=fopen($file_name,"r");
$data=fread ($file_template,filesize($file_name));
fclose($file_template);

$file_temp = 'php://temp/'.$file_name;
$file_new=fopen($file_temp, 'w');
fwrite($file_new,$data);
fclose($file_new);

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: text/plain');
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_temp));
readfile($file_temp);
exit;

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.