PHP - Warning: getimagesize failed to open stream: HTTP request failed - php

I am getting this error:
Warning: getimagesize(http://dev.clearcaresolutions.local/viewimage.php?file=screenshot_from_2016-11-29_12-12-06.png&type=form):
failed to open stream: HTTP request failed! HTTP/1.0
500 Internal
Server Error in /var/www/vhosts/dev.clearcaresolutions.local/current/modules/tasks/edit_log.php on line 845
This is the response code (please don't judge, I didn't write this, I have to live with it :/ ):
$fSize = filesize($dataPath.'/'.$file);
$fType = cFile::getMimeType($file);
// write the HTML headers
header ("X-Frame-Options: sameorigin");
#header("X-Frame-Options: deny"); # on
header ("X-XSS-Protection: 1; mode=block"); # off
header ("X-Content-Type-Options: nosniff"); # on
// BEGIN extra headers to resolve IE caching bug (JRP 9 Feb 2003)
header("Pragma: "); # on
header("Cache-Control: "); # on
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); # on
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); # on
header("Cache-Control: no-store, no-cache, must-revalidate"); //HTTP/1.1 # on
header("Cache-Control: post-check=0, pre-check=0", false); # on
// END extra headers to resolve IE caching bug
header("MIME-Version: 1.0");
header("Content-Description: File Transfer");
header("Content-Length: {$fSize}"); # ---
header("Content-Type: {$fType}"); # on
header("Content-Transfer-Encoding: binary"); # ---
header("Content-disposition: {$mode}; filename={$file}"); # on
echo readfile($dataPath.'/'.$file);
$fSize and $fType are correct, so this is not the issue.
This is how I call the response:
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';
(line 845) list($width, $height) = getimagesize($protocol.$_SERVER['SERVER_NAME'].$field['fieldsrc']);
When I use $field['fieldsrc'] (which looks something like this:
/viewimage.php?file=screenshot_from_2016-11-29_12-12-06.png&type=form
as a data for src inside image tag, the image is shown properly, so I am guessing the response is correct. Any idea what is wrong here?

readfile already outputs the content of the file. Remove the echo in front of it. It should be only
readfile($dataPath.'/'.$file);

Related

Download excel with php and zend framework 1.12

in my application i need to download a excel file created with some data in a db(postgresql), when i force the download sending headers
function createExcel($filename, $arrydata) {
$excelfile = "xlsfile://tmp/".$filename;
$fp = fopen($excelfile, "wb");
if (!is_resource($fp)) {
die("Error al crear $excelfile");
}
fwrite($fp, serialize($arrydata));
fclose($fp);
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel; charset=utf-8");
header ("Content-Disposition: attachment; filename=\"" . $filename . "\"" );
readfile($excelfile);
error_log("headers");
}
it downloads but it reads it as a cvs file and ask me for formating, but when i go to /tmp in my browser and click the file it downloads it as an excel file and shows the data just as i have put it in the code

download file with php

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

Generate a Doc File with an Image

<?php
$docName = "testdoc";
header("Expires: Mon, 26 Jul 2020 05: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");
header("Content-Type: application/msword; charset=ISO-8859-1");
header("Content-Disposition: attachment; filename=\"".$docName.".doc");
#/ Create and format your text using HTML, its simple stuff ... trial and error ;)
echo '<img src="logo.gif" />';
?>
Soumya Sarkar
Hi I got this following code from google to generate a Doc file by php. But I am unable to put any image there. Can anybody help me with it?
It requires the link, you will need to host the images online in order for the image to be shown on the doc.
Try this and it is working:-
echo "<img src='http://i.imgur.com/MJ5Sa.jpg' />";

create javascript code in a php file - SAFARI/CHROME doesn't work

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

header() problem in IE

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.

Categories