php - CakePHP: File replacement doesn't work -
php - CakePHP: File replacement doesn't work -
i'm new on cakephp , seek create edit function file replacement isn't working. if file exists error message.
this admin_edit.ctp code:
<td> <?php if (!empty($this->data['stock']['filepath'])): ?> <div class="input"> <label>uploaded file</label> <?php echo $this->form->input('filepath', array('type'=>'hidden', 'label' => false)); echo $this->html->link(basename($this->data['stock']['filepath']), $this->data['stock']['filepath']); ?> </div> <?php else: ?> <?php echo $this->form->input('filename',array('type' => 'file', 'label' => false)); ?> <?php endif; ?> </td> here below stock.php validation code public $validate = array(
'filename' => array( // http://book.cakephp.org/2.0/en/models/data-validation.html#validation::uploaderror 'uploaderror' => array( 'rule' => 'uploaderror', 'message' => 'something went wrong file upload - filename error', 'required' => false, 'allowempty' => true, ), // http://book.cakephp.org/2.0/en/models/data-validation.html#validation::mimetype 'mimetype' => array( 'rule' => array('mimetype', array('image/gif','image/png','image/jpg','image/jpeg')), 'message' => 'invalid file, images allowed', 'required' => false, 'allowempty' => true, ), // custom callback deal file upload 'processupload' => array( 'rule' => 'processupload', 'message' => 'something went wrong processing file - process error', 'required' => false, 'allowempty' => true, 'last' => true, ) processupload :
public function processupload($check=array()) { // deal uploaded file if (!empty($check['filename']['tmp_name'])) { // check file uploaded if (!is_uploaded_file($check['filename']['tmp_name'])) { homecoming false; } // build total filename $filename = www_root . $this->uploaddir . ds . inflector::slug(pathinfo($check['filename']['name'], pathinfo_filename)).'.'.pathinfo($check['filename']['name'], pathinfo_extension); // @todo check duplicate filename // seek moving file if (!move_uploaded_file($check['filename']['tmp_name'], $filename)) { homecoming false; // file uploaded } else { // save file path relative www_root e.g. uploads/example_filename.jpg $this->data[$this->alias]['filepath'] = str_replace(ds, "/", str_replace(www_root, "", $filename) ); } } homecoming true; } error message :"something went wrong file upload - filename error"
before saving :
public function beforesave($options = array()) { // file has been uploaded grab filepath if (!empty($this->data[$this->alias]['filepath'])) { $this->data[$this->alias]['filename'] = $this->data[$this->alias]['filepath']; foreach (array_keys($this->hasandbelongstomany) $model){ if(isset($this->data[$this->name][$model])){ $this->data[$model][$model] = $this->data[$this->name][$model]; unset($this->data[$this->name][$model]); } } } homecoming parent::beforesave($options); does help me find error ?
php cakephp file-upload
Comments
Post a Comment