Edit file download info PHP/JS - php

Hey, I'm trying to change the name of a file being downloaded through my site off an external server.
Currently whenever I modify the header information, it just takes a reallllllyyyyyy long time for the download to start, so is there any way to do it without changing the headers? Or if not any reason why it would be taking forever to load with my headers?
This is what I'm doing
#header("Cache-Control: no-cache, must-revalidate");
#header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
#header("Content-Length: ".getfilesize($url));
#header("Content-Disposition: attachment; filename=".$name);
#header("Content-type: audio/mpeg;\r\n");
die(#readfile($url));
Could it be done in javascript or something instead? And could i make it so all downloads start without opening in like windows media player, quicktime etc.
Thanks :)

It takes a "reallllllyyyyyy long time" because your loading the file to your server first and then send it to the client everytime someone is clicking the download link.

Related

Do I need to define a mime type in this header?

I am working with scanning images.
Today I made a step forward but I think that I need something more in the header maybe?
Currently a scan request is POSTed to app running on Apache server and PHP 7.0. I receive that post and today I figured out I needed to reply with a custom 201 header.
I have the following custom headers set in PHP
header('HTTP/1.1 201', Created);
header('Location: /eSCL/Scans');
header('Cache-Control: no-cache, no-store, must-revalidate');
After doing I was able to see that a request is now made for an image, from the app to Apache server to /eSCL/Scans/NextDocument. "NextDocument" is actually a softlink to a .jpg, (but may also later be a PDF). The app GETs this file, I can also see that the server replies with the JPG file . That image should display in the scan app that made the request, but never does.
I think however I need something in the header to tell it it is a JPG , especially given it loads from a softlink with no extension . I tried the following with the same result
header('HTTP/1.1 201', Created);
header('Content-type:image/jpeg');
header('Location: /eSCL/Scans');
header('Cache-Control: no-cache, no-store, must-revalidate');
There must be a magic soup that causes the image to display in the app.I have tried both VueScan and Mopria android app with the same results
Thanks for any ideas in advance

Turn off caching of web service response

I crete a web service in php and which returns JSON. It works fine but it seems to be cache results I changed values in the table and the service doesn't reflect this change. I included the following code but it doesn't seem to work. in my browser I am still getting the old values. How can this be rectified?
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
You may want to check your Ajax library (client) documentation see if you can disable its caching mechanism.
If you wish to disable cache on server side, refer to "How to control web page caching, across all browsers?"

Weird thing is happening on Live Server

I am facing a weird thing when i move the file to live server. Actually i have an XML file. It is read from Jquery and the contents are displayed in the HTML Page. Yesterday i made some changes in the XML file and updated in the Live server. It works perfectly in the local. But in Live server it is returning old XML file values only. I totally removed the file and moved the new file.
I thought it is referring from somewhere else. So i deleted the file and checked. But it shows error on that time. So it refers the same file only. I opened the file in the live server itself. Everything is perfect. But it still shows old content. I don't know what is the problem happening on live server.
Can anyone help me to figure out?
try adding no-cache header in your php, like:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
Or try accessing your xml file adding some random integer, like
your_xml_file.xml?id=<?php echo time(); ?>
Or if in js, use url like:
var url = "http://www.somesite.com/your_xml.xml?"+new Date().getTime();

Can HTTP 304 message be avoided?

When using Codeigniter or a simple php page, loadin my test page once I get the normal 200 status but if it is cached I get the 304 message. I realize this is just a signal that the resource is cached but could I avoid it?
If so, how?
Thank you.
... or you could just do
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
in your PHP files right at the top. That way you have control over which pages not to cache and leave Apache running as it should.
If you don't want the page to be cached by browsers you need to configure the apache server to send the appropriate headers.

how we can delete cache forcefully when user logs-in using javascript

Can anybody tell me how we can delete browser cache using javascript. I want this because I am giving user, file for download with url ('http://www.example.com/docs/doc1.xlsx'). and this files are accessible for that specific user only.
I am checking with htaccess redirect to other action which redirect to that specific file url if user does not have access then Access Denied page come.
But problem is when valid user download that file and logs out from application and copied above url and hit enter on browser file gets for download without accessing to server, which happens due to caching in browser.
So I want to delete cache when user logs out of system.
Alternative solutions are most welcome.
In short, you can't (or, at least, I have never seen a way of doing it).
You'll need to do it on the server side by sending the correct cache-busting headers. Something like:
Cache-Control: no-cache, must-revalidate, max-age=0
You can do this using (to steal an example from the PHP documentation):
header("Cache-Control: no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
This is not possible. Php only works serverside and using javascript is not working because of security issues. ActiveX is not an option eigther i guess.
What you can do is to attach a no-cache header for a page that must reload each time.
Rather than giving direct access to the file (as you mentioned "http://www.example.com/docs/doc1.xlsx"), I think you should read the the file in php and give it for download after checking for the valid user..
Example taken from php.net
<?php
// downloading a file
$filename = $_GET['path'];
/**
* YOU CAN CHECK YOUR VALIDATIONS HERE..
*
*
*
*/
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");
/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
#readfile($filename);
exit(0);
?>
Hope this helps..
Thanks...
I have resolved issue without major modifications.
Firstly I tried by setting header as you people suggested. Before redirecting for file to download I tried to set header for cache-control, but it doesn't work at all.
So I stretch my mind more and find simple solution by setting headers in .htaccess file which is located in folder where all downloadable files are located. That means cache-control header is set to download files response.
But still one thing in mind why above solutions not working.
And one more thing Pragma: "no-cache" not working for IE. that is it gives error while downloading requested file as "requested site is unavailable or cannot be found".
So I have set it to Pragma: public. But I doubt whether it is secure.

Categories