We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 : https://docs.phalcon.io/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 :)



51.1k
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: https://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.



17.0k

Can you please provide me controller code?



16.3k

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());
            }
        }
    }

}


17.0k

Anyone more who wishes to contribute ? :)

Sum writed a the best solution.

Check out the following series of posts on uploading images and displaying images with phalcon https://www.learnphalcon.com/post/show/34/adding-user-uploaded-images---step-1---storing-images-in-the-database