White screen when output buffer php on some smartphone only - php

I am writing a website using bootstrap 4, html 5 and php 7.
The website handle a folder with many pdf files inside.
In order to control the access to the pdf file, i use a php file that output the pdf wanted.
The folder of pdf is protected with a htaccess file.
Everything is working perfectly expect on some smartphones which display a white screen instead of the pdf file.
Same for the download option.
For debugging, I added logs at each steps,
In the normal situation, where i get the pdf, i got "-9-13-"
In the situation with the white screen, i got "-9-13-16", so something weird with the "exit"
Any ideas ?
if (file_exists($file))
{
//log_dbg('9-');
if(isset($_POST['download']))//download
{
//log_dbg('10-');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $chapter_file_name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
readfile($file);
//log_dbg('11-');
exit;
//log_dbg('12-');
}
else //Stream
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $chapter_file_name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
readfile($file);
//log_dbg('13-');
exit;
//log_dbg('14-');
}
}
else
{
//log_dbg('15-');
echo 'Fichier '.$file.' introuvable';
}
//log_dbg('16-');
Do you have any idea what can be the problem ?
You can try in real here :
http://laguildedesecrivains.fr/story.php -> click on "Chapitre 1"

Related

display csv in browser with charset utf-8, using php and headers

In a cloud-like web application I want to display files directly in browser, which works perfectly for .PDF and images using this code:
if (file_exists($serverfile)) {
header('Content-Type: ' . mime_content_type ($serverfile) . '; charset=utf-8');
header('Content-Disposition: inline; filename="' . $origname . '"');
header('content-Transfer-Encoding: binary');
header('Content-length: ' . filesize($serverfile));
header('Accept-Ranges: bytes');
readfile($serverfile);
//... more unrelated code
exit;
}
but I have scrumbled characters e.g. with German Umlauts, in case the file is a .CSV
I also set ini_set('default_charset', 'utf-8'); on top of the script.
What am I missing ?

Display a pdf with PHP not displayed correctly

I have some pdf files on a storage server and i want to show them to the users without showing them the real file path, and i want to do this with PHP.
I tryed this:
$file = 'https://storage.server_test.com/agc/catalogs/catalog1.pdf';
$filename = 'catalog.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
But the pdf is not displayed at all and i get this error: This PDF document might not be displayed correctly.
What am i doing wrong?
Have you tried embedding it using html object tag?
<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf">
<embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" />
</object>
Alternatively in php, try this header:
header('Content-Disposition: attachment; filename="' . $filename . '"');
Finally, if #readfile($file) isn't working, try:
echo #file_get_contents($file);
After trying this myself locally, I think I managed to get it working. Try this:
<?php
$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);
?>
Output:
I think what was the problem is that filesize($file) was failing for remote file. After removing that and header('Accept-Ranges: bytes');, it works.

PHP download script shows error while opening from mobile

I am developing a small website where user can download the mp3 files. The download script written in PHP & Codeigniter is
public function index($id)
{
$item = $this->music_m->get($id);
if(count($item)){
$path = $item->upload_path;
if (file_exists($path)) {
$item->noOfDownloads = $item->noOfDownloads + 1;
$this->music_m->save($item, $id);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;
}else{
echo "File doesnot exist";
exit;
}
}else{
echo "Requested music item doesn't exist in database";
exit;
}
}
The URL of the download link is in the format http://yourdomain.com/download/63 where 63 is item id.
Now when I click the link from desktop, it will download the file without any error. But when I click same link from smart phones(android) it gives me following error
ob_clean(): failed to delete buffer, no buffer to delete.
From some mobile, it gets downloaded but cann't open the file. Anybody knows where I am doing wrong?

PHP forced download going to webpage

I have an issue with the force download of a file.
I'm using PHPExcel to create a xls file based on information extracted from our database. This all works fine and the Excel file works as required.
I then attempt to force download of created file using the following function. However it downloads webpage rather than as a file.
Currently developing on Win XP SP3, Notepad++, XAMPP (Apache 2.4.3, PHP 5.4.7).
**function following
public function downloadfile($file){
if(file_exists($file) && is_file($file)){
//ob_start();
echo $file;
echo "in file exists";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
//ob_end_flush();
}else
echo "file or location does not exist <br>";
echo $file;
}
Any help would be appreciated.
Thanks in advance
If its a excel file then just redirect to that url and by default it will ask for file download.
header("Location: http://site.com/path/to/file/filename.xlsx");
/* Make sure that code below does not get executed when we redirect. */
exit;
If in javascript give
location.href = 'http://site.com/path/to/file/filename.xlsx';
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $fileName . ".xls\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filePath);
try this

Download file once - Corrupt file error

What I want to do is to allow only one download per user (one access to a href) For that I have a variable in the user's table, which I do change when the link has been clicked.
I use "download.php?file=file.xxx" for doing that.
download.php
$file= basename($_GET['file']);
$root = "documents/rece/";
$path= $root.$file;
echo $path;
if (is_file($path))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path);
}
else
echo "File error";
exit();
?>
I also update the DDBB and that works. After that I can show or hide the link. The problem is that the downloaded file is corrupted and can't be opened. I will use it with pdf or doc, maybe zip.
Could it be because of the path?
PDF files, as far as I know, start with something like this:
%PDF-1.4
Yours start with 4 blank lines plus documents/rece/Form.pdf%PDF-1.4. You're obviously printing it yourself somewhere before the code you've posted.

Categories