PHP code example of josegonzalez / cakephp-upload-with-rename

1. Go to this page and download the library: Download josegonzalez/cakephp-upload-with-rename 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/ */

    

josegonzalez / cakephp-upload-with-rename example snippets



CakePlugin::load('Upload');


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

 echo $this->Form->create('User', array('type' => 'file')); 


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

 echo $this->Form->create('User', array('type' => 'file')); 

 echo $this->Form->create('User', array('type' => 'file')); 


$this->User->set(array('photo' => $image_url));
$this->User->save();


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


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


class User extends AppModel {
	public $actsAs = array(
		'Upload.Upload' => array(
			'resume' => array(
				'fields' => array(
					'dir' => 'resume_dir',
					'type' => 'resume_type',
					'size' => 'resume_size',
				)
			),
			'photo' => array(
				'fields' => array(
					'dir' => 'photo_dir',
					'type' => 'photo_type',
					'size' => 'photo_size',
				)
			)
		)
	);
}


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',
		),
	);
}


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


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


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


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


	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'));


class User extends AppModel {
	public $name = 'User';
	public $actsAs = array(
		'Upload.Upload' => array(
			'photo' => array(
				'thumbnailSizes' => array(
					'big' => '200x200',
					'small' => '120x120'
					'thumb' => '80x80'
				)
			)
		)
	);
}


public $validate = array(
	'photo' => array(
		'rule' => 'isUnderPhpSizeLimit',
		'message' => 'File exceeds upload filesize limit'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'isUnderFormSizeLimit',
		'message' => 'File exceeds form upload filesize limit'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'isCompletedUpload',
		'message' => 'File was not successfully uploaded'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'isFileUpload',
		'message' => 'File was missing from submission'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'isFileUploadOrHasExistingValue',
		'message' => 'File was missing from submission'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'tempDirExists',
		'message' => 'The system temporary directory is missing'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('tempDirExists', false),
		'message' => 'The system temporary directory is missing'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'isSuccessfulWrite',
		'message' => 'File was unsuccessfully written to the server'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isSuccessfulWrite', false),
		'message' => 'File was unsuccessfully written to the server'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => 'noPhpExtensionErrors',
		'message' => 'File was not uploaded because of a faulty PHP extension'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('noPhpExtensionErrors', false),
		'message' => 'File was not uploaded because of a faulty PHP extension'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isValidMimeType', array('application/pdf', 'image/png')),
		'message' => 'File is not a pdf or png'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isValidMimeType', array('application/pdf', 'image/png'), false),
		'message' => 'File is not a pdf or png'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isWritable'),
		'message' => 'File upload directory was not writable'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isWritable', false),
		'message' => 'File upload directory was not writable'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isValidDir'),
		'message' => 'File upload directory does not exist'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isValidDir', false),
		'message' => 'File upload directory does not exist'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isBelowMaxSize', 1024),
		'message' => 'File is larger than the maximum filesize'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isBelowMaxSize', 1024, false),
		'message' => 'File is larger than the maximum filesize'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isAboveMinSize', 1024),
		'message' => 'File is below the mimimum filesize'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isAboveMinSize', 1024, false),
		'message' => 'File is below the mimimum filesize'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isValidExtension', array('pdf', 'png', 'txt')),
		'message' => 'File does not have a pdf, png, or txt extension'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isValidExtension', array('pdf', 'png', 'txt'), false),
		'message' => 'File does not have a pdf, png, or txt extension'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isAboveMinHeight', 150),
		'message' => 'File is below the minimum height'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isAboveMinHeight', 150, false),
		'message' => 'File is below the minimum height'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isBelowMaxHeight', 150),
		'message' => 'File is above the maximum height'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isBelowMaxHeight', 150, false),
		'message' => 'File is above the maximum height'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isAboveMinWidth', 150),
		'message' => 'File is below the minimum width'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isAboveMinWidth', 150, false),
		'message' => 'File is below the minimum width'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isBelowMaxWidth', 150),
		'message' => 'File is above the maximum width'
	)
);


public $validate = array(
	'photo' => array(
		'rule' => array('isBelowMaxWidth', 150, false),
		'message' => 'File is above the maximum width'
	)
);


echo $this->Form->create('Model', array('type' => 'file'));
echo $this->Form->input('Model.file.remove', array('type' => 'checkbox', 'label' => 'Remove existing file'));