Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Uploading images in Phalcon

Ok guys...

There are really not many tutorials on how to make a good photo upload service with Phalcon.

Is it better to use mysql/nosql for saving a uploaded image's location?

If using mysql, is it better to add photos to some model (User: id, photo1, photo 2...) or to create a new model for photos(Photos: id, userId, name)? The second versions seems to me much slower if large amount of images..

Is it better to use some real names in image name (for SEO) or to md5 a name of image ?

The official documentations gives us only this : http://docs.phalconphp.com/en/latest/reference/request.html#uploading-files but I dont find that very useful if i want to give every photo to unique place in one model (like User: id, photo1, photo 2...).

I, and many future learners would appreciate if some of you could post your examples here. Im sure quite many people google for ''image upload phalcon'' and the results are... well.. here we are :) Every example is good, from general to handling file sizes, file types, and so on :)



31.6k
Accepted
answer

The way how you store your data, it is usually your "problem" or your choice. Of course you will needto store the information somewhere if you need it later.

If you want an opinion, here is and example of what i do:

Table structure:

CREATE TABLE IF NOT EXISTS `media` (
  `id` bigint(20) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `obect_id` bigint(20) unsigned zerofill NOT NULL,
  `type` tinyint(1) NOT NULL COMMENT '0 = photo, 1 = video, 2 = audio',
  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `description` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'eg: photo.jpg',
  `media_source` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'eg: http://cdn1.mediastorage.tld/',
  `media_source_raw` text CHARACTER SET utf8 COLLATE utf8_bin COMMENT 'eg: embeded youtube code',
  `width` smallint(4) DEFAULT NULL,
  `height` smallint(4) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime NOT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `object_id` (`object_id`),
  KEY `is_primary` (`is_primary`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

The object_id can be a reference to an article table, user table or anykind of object. I always save the name of the file as a combination of timesptamp and md5. The SEO matters if you want google to search for your images. But if it will index your website, you will have the image alt attribute, with the clean name as saved in DB.

Can you please provide me controller code?



9.4k

Is it better to use mysql/nosql for saving a uploaded image's location?

for location only and not blob on database, i believe, that is the best way.

// NOT TESTED
use Phalcon\Mvc\Controller;

class PostsController extends Controller
{

    public function uploadAction()
    {
        // Check if the user has uploaded files
        if ($this->request->hasFiles() == true) {
            $baseLocation = 'files/';


            // Print the real file names and sizes
            foreach ($this->request->getUploadedFiles() as $file) {
                $photos = new Photo();              
                $photos->name = $file->getName();
                $photos->size = $file->getSize();
                $photos->save();

                //Move the file into the application
                $file->moveTo($baseLocation . $file->getName());
            }
        }
    }

}

Anyone more who wishes to contribute ? :)



5.2k

Sum writed a the best solution.