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?
Related
please I need help I'm trying to create a SEPA XML file so I downloaded the sepa-xml with coenter image description heremposer in my project then I make the same code in the documentation to test if work or not so when I enter image description hereadded the code and generate the file I get an empty file. but when I dump the variable I get a result .
I get this page
I referred here. Looks like you need to download the generated xml code.
[Git link][1].
Code will be like this.
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
echo $directDebit->asXML();
exit;
In this case the generated code will be saved as newfile.xml and download the file automatically. Hope it solves your problem
[1]: https://github.com/php-sepa-xml/php-sepa-xml/blob/master/doc/direct_debit.md
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/
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.
I have a button. By clicking the button, I want to export some data shown on the webpage to a file for downloading.
I am doing this in this way: I have a export.php. I send the data as the parameter to PHP file (another parameter is filename), and PHP server create a file and write the data, and then send file. Code is like:
$filename = $_GET['filename'] . '.csv';
$export = $_GET['export'];
$writer = fopen($filename, 'w') or die('cannot create');
fwrite($writer, $export . "\n");
fclose($writer);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.basename($filename));
readfile($filename);
unlink($filename);
exit();
For cases that the data are short, it works fine. But if the data are long, since the data are passed as part of the URL, I will get "Request-URI Too Large" error.
Is there any alternative way to do that? Is it possible to directly write data using JavaScript?
It sounds like you are sending to export.php with a GET, when you should be using a POST. GET is limited to 2048 characters, while a POST is not limited.
You'll need to POST the data to the server. Change the METHOD in your FORM tag to POST (in your html not your php code).
Each browser limits the size of the query string / length of the URL and the limit is browser dependent. You can POST a very large amount of data to the server however. The only limit is how fast is the user's upstream bandwidth and their patience.
Instead of passing the data as a query string, use javascript to create an iframe and build a form which then posts to the php file.
IF your using jquery there's a good tutorial http://tutorialzine.com/2011/05/generating-files-javascript-php/
I'm using http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation to access private files using php. I can get the data of the file by saying $object->body. I actually want to see the image in the browser or play the video in a video player. Is there a way to do that?
I think I need something like readfile. The problem is readfile is I need the path to the file. The path is private so I cannot use that. Is there a way to do a readfile of the binary data?
I put this in the php thinking this would help but it still displays the binary data.
header('Content: image/jpeg');
header('Content-Disposition: inline; filename=IMAG0108.jpg');
echo $object->body;
You just set the content-type header and output the readfile to the browser. What I do is create a new php file, like "showimage.php", that accepts an ID or some such to know what image to display. Then I use it in a browser page: .
In showimage.php, something like:
<?php
header('Content-type: image/png');
readfile('/var/images/' . $_GET['id'] . '.png');
// or
// echo $object->body;
?>
That would read a file from the local system and output it as an image. Off the top of my head, so I might have messed up that code!
header('Content: image/jpeg');
echo $object->body;
Should work fine (for JPEGs), you need know what filetype is in question and then send appropriate content headers.