PHP code example of simkimsia / upload

1. Go to this page and download the library: Download simkimsia/upload library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simkimsia / upload example snippets


	class User extends AppModel {
		public $actsAs = array(
			'Upload.Upload' => array(
				'photo'
			)
		);
	}

	 echo $this->Form->create('User', array('type' => 'file')); 
 echo $this->Form->input('User.username'); 
 echo $this->Form->input('User.photo', array('type' => 'file')); 
 echo $this->Form->end(); 

	class User extends AppModel {
		public $actsAs = array(
			'Upload.Upload' => array(
				'photo' => array(
					'fields' => array(
						'dir' => 'photo_dir'
					)
				)
			)
		);
	}

In the above example, photo can be a file upload via a file input within a form, a file grabber (from a url) via a text input, OR programatically used on the controller to file grab via a url.

### File Upload Example

     echo $this->Form->create('User', array('type' => 'file')); 
 echo $this->Form->input('User.username'); 
 echo $this->Form->input('User.photo', array('type' => 'file')); 
 echo $this->Form->input('User.photo_dir', array('type' => 'hidden')); 
 echo $this->Form->end(); 
 echo $this->Form->create('User', array('type' => 'file')); 
 echo $this->Form->input('User.username'); 
 echo $this->Form->input('User.photo', array('type' => 'file')); 
 echo $this->Form->input('User.photo_dir', array('type' => 'hidden')); 
 echo $this->Form->end(); 

	class User extends AppModel {
		public $actsAs = array(
			'Upload.Upload' => array(
				'photo' => array(
					'fields' => array(
						'dir' => 'photo_dir'
					),
					'thumbnailSizes' => array(
						'xvga' => '1024x768',
						'vga' => '640x480',
						'thumb' => '80x80'
					)
				)
			)
		);
	}

Multiple files can also be attached to a single record:

	
	class User extends AppModel {
		public $actsAs = array(
			'Upload.Upload' => array(
				'resume',
				'photo' => array(
					'fields' => array(
						'dir' => 'profile_dir'
					),
					'thumbnailSizes' => array(
						'xvga' => '1024x768',
						'vga' => '640x480',
						'thumb' => '80x80'
					)
				)
			)
		);
	}

### PDF Support

It is now possible to generate a thumbnail for the first page of a PDF file. (Only works with the `imagick` `thumbnailMethod`.)

Please read about the Behavior options for more details as to how to configure this plugin.

### Using a Polymorphic Attachment Model for File Storage

In some cases you will want to store multiple file uploads for a multiple models, but will not want to use multiple tables for some reason. For example, we might have a `Post` model that can have many images for a gallery, and a `Message` model that has many videos. In this case, we would use an `Attachment` model:

	Post hasMany Attachment

We could use the following database schema for the `Attachment` model:

	CREATE table attachments (
		`id` int(10) unsigned NOT NULL auto_increment,
		`model` varchar(20) NOT NULL,
		`foreign_key` int(11) NOT NULL,
		`name` varchar(32) NOT NULL,
		`attachment` varchar(255) NOT NULL,
		`dir` varchar(255) DEFAULT NULL,
		`type` varchar(255) DEFAULT NULL,
		`size` int(11) DEFAULT 0,
		`active` tinyint(1) DEFAULT 1,
		PRIMARY KEY (`id`)
	);

Our attachment records would thus be able to have a name and be activated/de-activated on the fly. The schema is simply an example, and such functionality would need to be implemented within your application.

Once the `attachments` table has been created, we would create the following model:

	
	class Attachment extends AppModel {
		public $actsAs = array(
			'Upload.Upload' => array(
				'attachment' => array(
					'thumbnailSizes' => array(
						'xvga' => '1024x768',
						'vga' => '640x480',
						'thumb' => '80x80',
					),
				),
			),
		);

		public $belongsTo = array(
			'Post' => array(
				'className' => 'Post',
				'foreignKey' => 'foreign_key',
			),
			'Message' => array(
				'className' => 'Message',
				'foreignKey' => 'foreign_key',
			),
		);
	}

We would also need to present a valid counter-relationship in the `Post` model:

	
	class Post extends AppModel {
		public $hasMany = array(
			'Image' => array(
				'className' => 'Attachment',
				'foreignKey' => 'foreign_key',
				'conditions' => array(
					'Image.model' => 'Post',
				),
			),
		);
	}

The key thing to note here is the `Post` model has some conditions on the relationship to the `Attachment` model, where the `Image.model` has to be `Post`. Remember to set the `model` field to `Post`, or whatever model it is you'd like to attach it to, otherwise you may get incorrect relationship results when performing find queries.

We would also need a similar relationship in our `Message` model:

	
	class Message extends AppModel {
		public $hasMany = array(
			'Video' => array(
				'className' => 'Attachment',
				'foreignKey' => 'foreign_key',
				'conditions' => array(
					'Video.model' => 'Message',
				),
			),
		);
	}

Now that we have our models setup, we should create the proper actions in our controllers. To keep this short, we shall only document the Post model:

    
    class PostsController extends AppController {
      /* the rest of your controller here */
      public function add() {
        if ($this->request->is('post')) {
          try {
            $this->Post->createWithAttachments($this->request->data);
            $this->Session->setFlash(__('The message has been saved'));
          } catch (Exception $e) {
            $this->Session->setFlash($e->getMessage());
          }
        }
      }
    }

In the above example, we are calling our custom `createWithAttachments` method on the `Post` model. This will allow us to unify the Post creation logic together in one place. That method is outlined below:

    
    class Post extends AppModel {
      /* the rest of your model here */

      public function createWithAttachments($data) {
        // Sanitize your images before adding them
        $images = array();
        if (!empty($data['Image'][0])) {
          foreach ($data['Image'] as $i => $image) {
            if (is_array($data['Image'][$i])) {
              // Force setting the `model` field to this model
              $image['model'] = 'Post';

              // Unset the foreign_key if the user tries to specify it
              if (isset($image['foreign_key'])) {
                unset($image['foreign_key']);
              }

              $images[] = $image;
            }
          }
        }
        $data['Image'] = $images;

        // Try to save the data using Model::saveAll()
        $this->create();
        if ($this->saveAll($data)) {
          return true;
        }

        // Throw an exception for the controller
        throw new Exception(__("This post could not be saved. Please try again"));
      }
    }

The above model method will:

- Ensure we only try to save valid images
- Force the foreign_key to be unspecified. This will allow saveAll to properly associate it
- Force the model field to `Post`

Now that this is set, we just need a view for our controller. A sample view for `View/Posts/add.ctp` is as follows (fields not necessary for the example are omitted):

    
      echo $this->Form->create('Post', array('type' => 'file'));
        echo $this->Form->input('Image.0.attachment', array('type' => 'file', 'label' => 'Image'));
        echo $this->Form->input('Image.0.model', array('type' => 'hidden', 'value' => 'Post'));
      echo $this->Form->end(__('Add'));