Since it's not a large number of images that my db will take, i'm uploading them directly to database. However, i'm having problems displaying them, i don't want to download them, i want to see them on the page. I'm trying to display with the following code but it's not working:
function display() of MyFiles Controller:
function display($id)
{
$file = $this->MyFile->findById($id);
$this->set('image',$file['MyFile']['data']);
}
MyFile Model:
<?php
class MyFile extends AppModel {
var $name = 'MyFile';
}
?>
function add() of MyFilesController
function add() {
if (!empty($this->data) &&
is_uploaded_file($this->data['MyFile']['File']['tmp_name'])) {
$fileData = fread(fopen($this->data['MyFile']['File']['tmp_name'], "r"),
$this->data['MyFile']['File']['size']);
$this->request->data['MyFile']['name'] = $this->data['MyFile']['File']['name'];
$this->request->data['MyFile']['type'] = $this->data['MyFile']['File']['type'];
$this->request->data['MyFile']['size'] = $this->data['MyFile']['File']['size'];
$this->request->data['MyFile']['data'] = $fileData;
$this->MyFile->save($this->request->data);
$this->redirect(array('controller'=>'posts','action'=>'index'));
}
}
EDIT:
display.ctp of MyFiles View
<?php
echo '<img src="/MyFilesController/display/4" />';
?>
src="..."?<img src="somescript.php?imageID=xxx">or<img src="data:image/jpeg;base64,....">? Somehow you've got to get your image from PHP to the browser in a form that the browser recognizes.