I want to output a "Please wait" message before a PHP script runs but it is output at the same time together with the other echo messages.
The purpose is to inform the user that the page is loading and that one is not tempted to think that the page is not loading.
MRE:
header('Content-type: text/html; charset=utf-8');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Mon, 24 Sep 2012 04:00:00 GMT");
ob_start();
echo "Please wait";
ob_flush();
flush();
sleep(1);
run_a_function();
function run_a_function() {
// execute some php code, may take some time
echo "progress..";
// execute some php code, may take some time
echo "progress..";
// more..
}
Related
i want to create a link to download an excel file from the root in server computer, using php. so i wrote a simple code as below,
<?php
header("Content-disposition: attachment; filename=example.xls");
header("Content-type: application/vnd.ms-excel");
readfile("example.xls");
?>
it then can be downloaded however when i want to open it, i got the error saying the file i downloaded is in a different format than specified by the file extension. i also tried the same method with jpeg file and didnt get the same error but when i click it, it shows nothing. can someone help me? im not very good with programming. thank you in advance!
Try this
$file='example.xls'; $filesize=filesize($file);
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: inline; filename="'.basename($file).'"');
header("Content-Length: " . $filesize);
$fh = fopen("$file, "rb");
// output file
while(!feof($fh))
{
# output file without bandwidth limiting
print(fread($fh, filesize($file)));
}
fclose($fh);
I have a jQuery script in my header that calls a php file.
The php file contains data pulled from a remote xspf file, and the output presented in jQuery for refresh purpose. i have 2 echo rules, one for title and one for listeners. right now jQuery calls the file itself which makes the output appear together on a single line.
how can i make jQuery present these echoes separately so i can control their design (CSS)?
PHP:
<?php
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$xml = simplexml_load_file("http://mysitehere:8000/live.xspf");
foreach ($xml->trackList->track as $data) {
$radio = $data->location;
$song = $data->title;
$info = $data->listeners;
}
echo $song;
echo $info;
?>
jQuery:
<script language="javascript">
jQuery(document).ready(function(){
//Carga al comienzo
jQuery('#salida').load('reader.php');
setInterval(function() {
jQuery('#salida').load('reader.php');
}, 2000);
})
</script>
<div id=salida> </div>
Put the echoed info into separate elements
echo '<div class="Song">'.$song.'</div>';
echo '<div class="Info">'.$info.'</div>';
then setup classes in your css for Song, Info (or whatever names you want to give them)
I've created a .php file that write out js code like that:
<?
//my_js.php
// javascript header
header('Content-type: text/javascript');
// Date in the past
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// always modified
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// HTTP/1.1
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
?>
//
// js code here
//
then i include the script above in my index.php file like this:
<script type="text/javascript" src="my_js.php?id=<? echo $id ?>">
</script>
This works perfect in Firefox, but SAFARI and CHROME doesn't include my_js.php file at all!
What i'm doing wrong?
**edit:
this is the rendered html in index.php:
<script type="text/javascript" src="my_js.php?id=new"></script>
and this is the my_js.php code:
(it's a very big file so i write out only the first few lines)
var g = { sitepath: "myNullUrl" }
function getBrowserWidth(){
if (window.innerWidth){
return window.innerWidth;}
else if (document.documentElement && document.documentElement.clientWidth != 0){
return document.documentElement.clientWidth; }
else if (document.body){return document.body.clientWidth;}
return 0;
}
that's a strange problem 'cos while i'm viewing source code from Crome/Safari i can access the js file and it seems to be error free!
I'm using Chrome 6.04 and Safari 5, both for mac.
It may be because it is expecting the file-extension to be my_js.js. If this is the case, save your PHP file as my_js.js, then, assuming you're using Apache, use the Apache Directive: ForceType:
Like so:
<Location /your/path/my_js.js>
ForceType application/x-httpd-php
</Location>
Good luck!
Maybe set Content-Disposition: inline; header?
<?php
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Content-type: text/javascript");
header("Content-Disposition: inline; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
} else {
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
}
header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
}
include("my-file.js
");
?>
It should work. Or you can change this:
header('Content-type: text/javascript');
to this
header('Content-type: application/javascript');
Note:
application/javascript: JavaScript; Defined in RFC 4329 but not accepted in IE 8 or earlier
text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support
I have a function for outputting documents, images etc:
public function direct($theMimeType, $thePath)
{
header('Content-type: '.$theMimeType);
ob_clean(); // clean output buffer
flush(); // flush output buffer
readfile($thePath);
exit;
}
It works great in Firefox. The file opens whether it is PDF, DOCX or any other file. However, in IE it freezes and nothing shows up.
What could cause this?
EDIT:
I have added few other headers:
public function direct($theMimeType, $thePath)
{
$aSize = filesize($thePath);
$aBegin = 0;
$aEnd = $aSize;
$aFilename = end(explode('/', $thePath));
$aTime = date('r', filemtime($thePath));
$aContentDisposition = ('application/pdf' === $theMimeType) ? 'inline' : 'atachment';
header('HTTP/1.0 200 OK');
header("Content-Type: $theMimeType");
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Length:'.($aEnd-$aBegin));
header("Content-Range: bytes $aBegin-$aEnd/$aSize");
header("Content-Disposition: $aContentDisposition; filename=$aFilename");
header("Content-Transfer-Encoding: binary\n");
header("Last-Modified: $aTime");
header('Connection: close');
ob_clean(); // clean output buffer
flush(); // flush output buffer
readfile($thePath);
exit;
}
Well, it works in IE now but still it opens the file much slower than Firefox. There seems to be few seconds freeze up before the IE browser opens the file.
Make this file download directly, not using any scripts
make sure it works in IE
in firefox, use LiveHTTPHeaders to watch what headers being sent by web-server
in firefox, use LiveHTTPHeaders to watch what headers being sent by your script
make your script's headers the same as web-server's
Most of those headers aren't really necessary. I prefer to keep things simple:
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public');
header ('Content-Type: '.$theMimeType);
header ('Content-Disposition: '.$aContentDisposition.'; filename="'.$aFilename.'"');
header ('Content-Transfer-Encoding: binary');
header ('Content-Length: '.$aSize);
Watch out for the \n at the end of your Content-Transfer-Encoding header.
The 'Pragma: public' is a workround specifically to handle a problem with IE and https connections. The other key difference is $aFilename in quotes.
Not sure if the title makes sense sorry... basically I'm generating Word documents that I wanted to open automatically on the client's machine, but it seems this isn't possible, so instead of just presenting a list and having them manually click on each one, I was wondering if at the very least it could bring up the 'Would you like to save or open this file' dialogue :(
Long shot, but I know a lot of sites will do this when you download stuff... re-directing to download server etc
Thanks
The best you can do is provide a "hint" with the Content-disposition header.
http://php.net/manual/en/function.header.php
Specifying something like
<?php
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// output content of document
?>
should cause most browsers to prompt the user to download the document, while this:
header('Content-Dispotion: inline')
should usually cause the browser to show the file in the existing window.
You can set the contenttype and content-disposition header to open the open/save dialog box in browser.
See
Header Field Definition
for details.
You need to send the correct header and so on.
I found this that seems good:
<?php
function send_file($name) {
ob_end_clean();
$path = "protected/".$name;
if (!is_file($path) or connection_status()!=0) return(FALSE);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($path)));
header("Content-Disposition: inline; filename=$name");
header("Content-Transfer-Encoding: binary\n");
if ($file = fopen($path, 'rb')) {
while(!feof($file) and (connection_status()==0)) {
print(fread($file, 1024*8));
flush();
}
fclose($file);
}
return((connection_status()==0) and !connection_aborted());
}
?>
And here's an example of using the function:
<?php
if (!send_file("platinumdemo.zip")) {
die ("file transfer failed");
// either the file transfer was incomplete
// or the file was not found
} else {
// the download was a success
// log, or do whatever else
}
?>