I am currently working on a project that has files stored in a DB as blobs. I need to attach the file to an e-mail and send it out via PHPMailer. I am familiar with $mail->addAttachment(), however, this function seems to take in a file path only, which I don't have. I was wondering if there is any way to manipulate the blob and feed to this function ?
I appreciate any suggestions, thanks in advance!
The following successfully creates a 'Save As' dialog of the file I need to attach:
header("Content-disposition: attachment; filename={$filename}.{$file_ext}");
header("Content-type: application/octet-stream");
echo $pdf['data'];
exit;
The addStringAttachment method is capable of handling such case. According to its doc:
* Add a string or binary attachment (non-filesystem).
* This method can be used to attach ascii or binary data,
* such as a BLOB record from a database.
Related
I recently had a asked a question very similar to this one, however after evaluating that I did not explain it in the best way I have come back once again explaining it in a greater manner.
So, I am creating a system that will gather data from a MySQL database and use a unique id to download a file, however depending on the value of a column within that database called type, this file could be anything from a png file to an xml file. What I am currently doing is trying to download these files WITHOUT any extension.
As an example to maybe make this easier to understand, a file named image.png would be converted to just image and then downloaded.
With this you could rename the file to image.png again on the local machine and view the image.
This may seem very inefficient to most reading this but for my current situation it's all that will work.
How could I remove a files extension and then download it? (in php)
Thank you in advance.
Just use headers to specify response type.
$filepath = '/wherever/the/file/is.png';
$filename = 'new-cool-name';
header('Content-Type: whatever/content-type-is');
header("Content-disposition: attachment;filename=$filename");
readfile($filepath);
This basically sends a response with specified content-type as an attachment and the body of the attachment contains the file contents. If you never sure what's the content type is, then just use application/octet-stream
Usually when you set out to push a file for downloading from a serverside script, you do so by utilizing http headers like https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
The filename of the downloadable file is specified in that header
Okay so to remove an extention from a file you could do is
$withoutExtion = preg_replace('/\\.[^.\\s]{3,4}$/', '', $youfilename);
...followed by your file download code
I've got a variable that gets the pdf as a binary file into the website
<?=$calculationData->PDF_file?>
How do I show the pdf as a link, instead of binary text?
Note: I'm a newbie at this and don't know a lot about php! :-)
I assume $calculationData is an object from a database and the PDF content is stored there as binary data. Then you can do something like this:
Link to a PHP file that loads the content and sends it to the browser.
<a href="show_pdf.php?id=555">
Where id is the id of your calculationData somewhere in the database in this example. You need something to identify it.
show_pdf.php:
<?php
/* load your calculation data here ... */
header("Content-type: application/pdf");
header("Content-Disposition: attachment;filename='mywhateverfile.pdf'");
echo $calculationData->PDF_file;
exit();
In that way you can send the binary data to the browser and telling it with the correct headers, what to do with that data.
I have to encrypt a file and save it in mysql as a blob, then decrypt it and make it available for download.
I save the file in a blob like so:
$certificate_tmp=$_FILES['certificate']['tmp_name'];
$certificate=openssl_encrypt(file_get_contents($certificate_tmp),$ciphers,$password_tmp);
$wpdb->insert("my_table",array("certificate"=>$certificate));
Note: I've cut unrelated code, the table is not just certificate, but I don't want this to get confusing.
This is the download php page:
$password_tmp=$_SESSION['pwd']; //decrypt password
global $wpdb; //wordpress db conncetion
$results_file = $wpdb->get_row("select * from my_table where id='$id'",ARRAY_A); //i get the id from wp's curr_user
$m_file = openssl_decrypt($results_file['certificate'],"AES-128-CBC",$password_tmp);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\certificate".$id."\"");
header('Content-Transfer-Encoding: binary');
print_r($m_file);
And everything works perfectly with text files, but the result is empty with binary files, although in the blob the binary file is there.
EDIT
My guess is, as soon as I decrypt the file from the db blob, php or html (because of print_r) understands that it is a binary file and doesn't let you show it because of security reasons. You can't execute programs on the web like .exe or .bin, although the files I upload, either binary or text have no extension.
From what I understand php treats binary files as strings, but file_get_contents is binary safe.
I think not using blobs would be the best approach for this, but I cannot do that, I have to use blobs.
EDIT 2
The problem seems to be openssl which doesn't seem to like binary data, I've tried the same code using mcrypt and it works perfectly.
Make sure you are decrypting the data using the same key.
Why do you use print instead of print_r?
Try to add the content length:
header('Content-Length: '.strlen(YourFileData));
For more information please visit:
http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/
First post. I'm working on a project for a client where they have pdf files uploaded to a file structure (LAMP Stack) but the files have no extensions on them. Under the assumption that those files have to be PDF how would I get the browsers to understand that, and open them accordingly? Obviously with adding the file extensions this would suddenly work but I can't change the way their system works, it would result in too many changes and they are on a tight deadline. As for saving a temporary copy somewhere, I could do that, but I was hoping for a better solution. Is there a way to suggest to the browsers that they open a file a certain way?
Any thoughts guys/gals?
You just set the application type and file name in the headers, like so:
// This points to the file in question, note that it doesn't
// care whether it has an extension on the name or not.
$filePathOnDisk = '/path/to/your/pdffile';
// You can make this whatever you like, it doesn't have to
// be the same as the file name on the disk! This is the name of the file your end
// user will see when they are asked if they want to save. open, etc in the browser.
$fileName = 'file.pdf';
$data = file_get_contents($filePathOnDisk);
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=$fileName");
echo $data;
See PHP: stream remote pdf to client browser and Proper MIME media type for PDF files for reference as well.
Tested
You can use the following which will prompt the user to save the (PDF) file on their computer.
Notice the different file names.
One is the file that will be uploaded/prompted to the user download_example.pdf, while the other is the file without an extension as set in readfile('example');
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download_example.pdf"');
readfile('example');
?>
I am using PHP 5.3.5 and postgreSQL. I am storing and extracting images from database.
For storing i am doing this:
$escaped_data = base64_encode(file_get_contents($_FILES['fileUpload']['tmp_name']));
$fileModel->setBinary_Data($escaped_data);
It's working, i received the image in my database (Bytea field).
The problem is to extract this, i am trying this code to extract my images:
$file_info = $fileModel->getBinary_Data($id_file); // This function return the binary_data of the image
header('Content-Type: image/jpeg;base64');
header('Content-Disposition: attachment; filename=' . $file_info['_name']);
base64_decode($file_info['binary_data']));
When i download the image, i can't see the image...
With echo in:
echo base64_decode($file_info['binary_data']);
This happen:
http://imageshack.us/f/18/encodez.jpg/
After, i am trying using the function stream_get_contents inside base64_decode, but doens't work.
Someone know how i can download my images with php?
Thanks anyway...
Well obviously your custom getBinary_Data() returns $file_info['binary_data'] as a resource, not a string.. so either change the function or use base64_decode(file_get_contents($file_info['binary_data'])));
And try to use fclose($file_info['binary_data']); too then.
P.S. Wasn't there a blob type for binary data in postgre?