0

In the cakephp 2.0 application i'm developing i will need a HABTM relationship between a table with "normal" data and one with stored images.

After a search, i came to conclude that the best way of implementing this was to store the images in the directory of the application, and in the table "images" a reference to the url of each image so that i can display them later on the application. Please, if you think this is not the best implementation please say so.

Looking into this, i would like to know how can i do this, i mean, how can i store the images on directory and store a reference in a database in the same function of a controller? I did some searchs but i found only uploading to folders or only storing the images on database.

Thank you!

user1511579
  • 1,517
  • 6
  • 26
  • 42
  • Try to combine both of the methods you found :) Really. We can not do it all for you... – Alvaro Dec 17 '12 at 11:52
  • I understand Steve. Maybe since it's just a few images to be upload, i guess i will store the images on database, what do you think Steve? – user1511579 Dec 17 '12 at 12:11
  • 1
    there are plenty of great plugins that do this out of the box, (https://github.com/josegonzalez/upload) I have used with great success previously. It offers schema for polymorphic behaviour, or a simple "attachments" table. – Ross Dec 17 '12 at 14:08
  • @Ross thank you for the link, i had already checked him during my research, but there's something missing there, how would i display the images on the page and not download them? – user1511579 Dec 17 '12 at 14:32
  • the plugin simply uploads images to predefined directories and saves the path/file name. You just output the image as normal: `$this->Html->image('../files/model/field/' . $a['Model']['dir'] . DS . $a['Model']['file']);`, for example. There's also a helper floating around – Ross Dec 17 '12 at 14:40
  • 1
    I see your other question - if you are storing the image as a blob (whcih is not recommended) you need to output the necessary headers. It is generally preferred to store the path to the file, and just output using the image helper. Read http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay for a good discussion – Ross Dec 17 '12 at 14:47
  • @Ross that's what i originally pretended. Create a table with just the reference for the image stored in the directory of the apllication, but since i'm new on cakephp and having to much difficulties to adapt, since i found some code to upload to db the images (and i'm just missing the part of display them) i went to that, because i couldn't found a way/a tutorial explaining how to store the images in a folder "inside" cakephp and then retriving them to the output page through the link on the database table. – user1511579 Dec 17 '12 at 15:01

1 Answers1

0

My FileStorage plugin https://github.com/burzum/FileStorage in combination with the https://github.com/CakeDC/Imagine plugin will exactly do what you want.

All file data is stored in the database, the files itself in the local file system or any of the other adapters. It's also handling generating versions of the image after upload.

Read the readme.md it is exactly doing what you want and I'm already using it in different projects, also for a gallery and user avatars.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • thank you for the suggestion, i will take a look and say something later :P – user1511579 Dec 17 '12 at 21:53
  • i finally could take a look at your suggestions. For what i understood, i guess the second plugin would be enough since i have another one to store the images. So, my problem now is with the Imagine Helper: $url = $this->Imagine->url( array( 'controller' => 'images', 'action' => 'display', 1), array( 'thumbnail' => array( 'width' => 200, 'height' => 150))); echo $this->Html->image($url); Shouldn't i use this code on the controller of the model i want? That's what i tryed to do but couldn't since doesn't recognize Imagine – user1511579 Dec 20 '12 at 17:48