I'm trying to create a 'download' page for my website. I'm using a url like this: http://example.com/download?file=21. The actual location of the file on the server is stored in a database along with the file ID. I would like the download page to display the name of the file that is being downloaded along with some other HTML. I'm having trouble figuring out how to both download a file and display output on the same PHP page.
All the advice I've seen for actually downloading a file with PHP requires changing the headers. Do I need to display the download page and then download the file with something like AJAX?
I would do it without ajax.
I would create a script to show the file with other html info http://example.com/file?id=21 and if the download param is set ( http://example.com/file?id=21&download=1 ) that script only prints the headers and sends the file.
Then, in http://example.com/file?id=21 I would create an empty iframe (in the code or dynamically with javascript) and then when a user wants to download the file you just have to set the url of that iframe to http://example.com/file?id=21&download=1 and the file will start to download. No need of ajax or reloading the website, only javascript to set the iframe url at the moment the user wants to download the file.
It's not the only way but I think it's a good one.
Related
Using Codeigniter, I'm trying to do something basic.
The site has a form where users upload files for processing. After the files are processed, new files are created and saved in a zip file. Then I use $this->zip->download() to start the download.
But after that I can't do anything to refresh the upload form. And if I reload the view first, the download doesn't work.
All I want to do is upload the files, create the zip, download the zip and wind up with a clean upload form.
Any suggestions how to approach this?
I don't think you will be able to do it on the same page because of missmatched headers. You could try the following: when the zip is ready save it and name it some random/unique id/md5 anything and print the download link in the view with "click here to download your file" with target="_blank" param so it opens in a new tab. (usually browsers are smart enough that it will auto close it when you accept the file). And so you can reset the form in the view as well.
Another way could be with ajax and js i guess but that would be much longer to write.
Ps.: just dont forget to delete the files after x amount of time (if you dont need them)
I am trying to generate a report based on user input from a form in html. As of right now, everything works. Except when I generate the report, the url of the newly opened tab contains the location and the name of my php file. Obviously, this report won't be accessible once the user closes that tab.
Is there any way I can store this file on my server and specify a path for it(for each user)?
Or just generate a pdf file out of it? I tried using some libraries but they don't fully support bootstrap.
How to save a web page using php similar to save as in Browser -> file -> save page as
I need to save web page dynamically when url is given as parameter. Containing the images in related folder.
Thanks in advance :)
You would need to use php curl to download the HTML from that page.
After that you need to scrape the content of images, css, js and other files and download and save them conserving the path, by using readfile() function.
I have a PHP script that downloads a PDF file from the server and prompts you to either open it, or save it. The script accepts a one time token, which is used in place of a file name, to hide the file name.
If you go to the actual php page, http://example.com/files/download/token the script works fine and it downloads the PDF.
I could just send people to that page with a standard link tag, but once the file downloads I need to update content on that page which is returned through that download script.
Is there any way to have ajax call open up a new window where the file will download and then return the data that I need to update the current page?
There is more to the download script, but the main piece is the actual downloading part:
header("Content-type: application/pdf");
$this->load->helper('file');
readfile("static/temp_statements/".$local_file_name);
unlink("static/temp_statements/".$local_file_name);
One trick that I have used in the past that might be useful to you is
Supply a query param in your ajax call to download the PDF. This will be a unique name.
The server process that streams the PDF for download sets a cookie with this unique name.
You poll in your page waiting for this cookie to appear.
When the cookie appears you can assume the file has downloaded, and you can do your contingent action.
And you do not need to open a window to make this happen. You could just append an invisible iframe like this:
$(some selector).append($("<iframe width='1' height='1' frameborder='0' src='" + url + "'></iframe>"));
In the interests of honesty and transparency, I originally found this idea from this SO answer and it worked for me: Detect when browser receives file download
How to load javascript contents using php. Suppose when php saves a file using, file get contents and file put contents, JavaScript contents like Google current ads is never loaded but the google adsense codes are loaded.
Can php "run" the JavaScript code on a saved page in order to save the dynamic content that the code generates?
Already got the answer is NO.
Is there any way or no Way?
You are asking whether or not PHP can "run" the JavaScript code on a saved page in order to save the dynamic content that the code generates. The answer is no, it cannot. Or at least, not without building your own JavaScript interpreter.