I have a base64 encoded string converted with the php function below.
public function convert_image_to_base64($file_path){
$mime = get_mime_by_extension($file_path);
$data = file_get_contents($file_path);
return 'data:'. $mime . ';base64,' . base64_encode($data);
}
I want to download the file in a browser with the below html
Download
When I click, the link downloads a zip file.
This is the start of the resulting base64 string
data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBBQABgAIAAAAI
I want to download the exact .docx or .doc file uploaded
I searched on stackoverflow and other forums but none of them address this exact problem. I also don't want to decode the file before submitting the html.
So the problem was the file extension of the downloaded file was not added in the base64 file downloaded. I solved the problem by forcing the name of the downloaded file using the html below from this link. Reference here
Download
Related
I have tried to send image and video file as response with the code as per CakePHP 4 documentation: https://book.cakephp.org/4/en/controllers/request-response.html#sending-files
Below is my function to access file from url.
public function noteFileAccess() {
$filename = $this->request->getParam('filename');
$id = $this->request->getParam('id');
$path = Configure::read('FilePath.NoteFiles');
$file = new File($path . $id . DS . $filename);
if(!$file->exists()) {
throw new ForbiddenException;
}
$this->response = $this->response->withFile($file->path);
//Also tried to set mime type like below commented line
//$this->response = $this->response->withType($file->mime())->withFile($file->path);
return $this->response;
}
By using above code, it is sending pdf, zip, csv files properly. But images & videos files are not working. Its showing white square in chrome browser. And if downloaded it is showing format not supported in windows system.
Only different I see in downloaded image file vs original image file is image dimensions and some other things like in screenshot below
Edit: File Content Difference added below
I have checked with file path & mime type of the image file is good.
Do you think I am missing anything here?
I have a String representing a pdf file encoded in base64.
I can decode the string like this:
function get_file_from_b64($b64_file) {
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
return $pdf;
}
I would like to create a page that download the file but I don't want to save the file on the filesystem read it in a php page and then send it back to the browser.
There is a way to send back the file directly without saving it on the filesystem?
For example I can send a file back to the browser like this:
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=downloaded.pdf");
readfile("test/file.pdf");
This implies that the decoded file is saved on test/file.pdf.
The readfile command simply echos the content of the file. Instead of doing that echo the variable with your contexts in it.
You will base to base64 decode the PDF if it has been base64 encoded
For example
echo base64_decode($pdf_content);
I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP
I have objects containing images as base 64 strings, the object also contains file names for the images, and file types (jpeg, png, gif & bmp) for the images.
The base 64 strings have already had the tags (e.g. "data:image/png;base64" removed from the beginning.
The format for the objects ($myImg) are as follows:
$myImg->fileName contains the name that the converted image should be saved under.
$myImg->fileType describes the format that the file is supposed to be saved as - this is used to specify the path extension in the fopen() function.
$myImg->b64 contains the 64 bit binary string which represents the image.
The code for my function is as folows:
function toImg(ImageString $myImg){
//Output file is in the same directory as the PHP script.
//Uses the object's filetype attribute as the file extension.
$outputFile = fopen($myImg->fileName . "." . $myImg->fileType, "w");
$image = base64_decode($myImg->b64);
fwrite($outputFile, $image);
fclose($outputFile);
}
The function creates the image files, but I get errors when trying to view them in Xubuntu Image Viewer. The errors are as follows:
Error interpreting JPEG image file (Not a JPEG file: starts with 0x14 0x00)
Fatal error reading PNG image file: not a PNG file.
File does not appear to be a GIF file.
BMP image has bogus header data.
I've looked through and followed guides for base64 to image conversion, but none of them have encountered these errors.
Try to show the image inline in the browser, like this:
<img src="data:image/png;base64,the-base64-string" />
(change png to the correct image format)
If the image is still broken, then the image data is invalid.
You can decode the image from base64 like this:
function base64_to_jpeg_img($base64_img_string, $output_img) {
$input_file_open = fopen($output_img, "wb");
$data = explode(',', $base64_img_string);
fwrite($input_file_open, base64_decode($data[1]));
fclose($input_file_open);
return $output_img;
}
Hope this helps!
I'm struggling with my XML to XLS code in PHP.
I have PHP code that exports MySql data to XML and I have code that writes XML to XLS.
This all works fine, except that when I open the xls file, I get an error:
"The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
my code:
<?php
$file = 'filename';
$url = "$file.xml"; // from xml file
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=$file.xls" ); // to xls file
if (file_exists($url)) {
$xml = simplexml_load_file($url);
echo 'Voornaam'."\t" . 'Achternaam'."\t" . 'Geslacht'."\t" . 'Instrument'."\t\n";
foreach($xml->lid as $lid)
{
echo $lid->voornaam."\t" . $lid->achternaam."\t" . $lid->geslacht."\t" . $lid->instrument."\n";
}
}
?>
I found that it has something to do with the MIME_Types https://filext.com/faq/office_mime_types.php
The header("Content-Type: application/vnd.ms-excel"); is for the older versions of excel. But when I use the xlsx type, the file won't open at all.
Like I said, this code runs fine, downloads the Excel file in xls format to my computer. But how can I open it without the error?
I would also like to upgrade the file to xlsx format if someonw knows how to open the file in that format.
Well, your header and filename are both saying "this is an XLS file" - but it isn't, it's actually a TSV file (Tab-Separated Values). So yes, the message is accurate: the file extension is misleading.
To write an actual, valid XLS (or XLSX), it is easiest to use a library which does this for you - both these formats are complex and tricky. I've had best results using PhpExcel (example code).
When Excel opens an .xls file, it expects it to be in normal old binary Excel format. You seem to be outputting tab separated text (.tsv format), which Excel understands but doesn't want to open without issuing a warning. I think this warning was introduced with Excel 2007, and that earlier versions opened happily without warnings. Unfortunately I have no sources handy for this info, only personal experience.