0

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" />';
?>
9
  • Does your layout have: <?php echo $this->fetch('content'); ?> Commented Dec 17, 2012 at 14:21
  • How are you trying to load the image into a browser? data urls? image display script called from a src="..."? Commented Dec 17, 2012 at 14:22
  • @MarcB could you please be more specific? If i would use scr shouldn't be to a directory folder and not database? Commented Dec 17, 2012 at 14:27
  • you did not specify your cake php version. please do so - always. Commented Dec 17, 2012 at 14:31
  • <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. Commented Dec 17, 2012 at 14:31

1 Answer 1

3

Saving images in a database is generally not a good idea.

You are far better off (and I assume you have done this for 'security') saving the files in a folder that is not web accessible and using Media Views to render them. This way you can still keep your security checks as you are using a controller to render the image.

Cakes media views are designed for streaming files to the browser and can easily be configured for images.

2.3 has a new feature for this

If you are not doing this for security reasons just save your self the problems and put them in webroot.

Sign up to request clarification or add additional context in comments.

4 Comments

i was saving them on the database, so i was not saving them in some folder but well, i'm trying change to that way and only use a reference to the url of the image in the database table to later show the images on the browser pages, that's what i'm triyng right now, but having some difficulties
Use this plugin github.com/josegonzalez/upload, it has good docs and works out the box.
@Ross had already recommend them, and i confess during my research i found that page. Well, i will try and say something later
i was able to upload the images to the webroot/files/user/photo/.. But now, how can i through the table that was them referenced, show the images on the web page of the application?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.