希望这是一个简单的问题。
我有一个 Cakephp MVC 设置来上传图像并将其存储在数据库表中。索引、视图和添加按我的意愿工作。
但是,在“编辑”视图中,我如何指示该记录当前有一个上传的图像,我可以用它来保持更改。
我无法弄清楚在文件输入按钮的属性中设置什么来显示现有文件名。
<div class="imageEdit form">
<?php
echo $this->Form->create('VwImage', array('action' => 'edit', 'type' => 'file') );
echo $this->Form->input('image_category', array(
'options' => $partCat,
'selected' => $this->data['VwImage']['image_category'],
'type'=>'select', 'empty' => '(Choose One)'));
echo $this->Form->input('description');
echo $this->Form->input('image', array( 'value' => $this->data['VwImage']['file_name'],
'type' => 'file'));
echo $this->Form->submit('Save');
echo $this->Form->end();
?>
控制器代码编辑
public function edit( $id = null) {
$this->loadModel('VwPartsCategory');
$partCat= $this->VwPartsCategory->find('list',
array( 'order' => 'short_name ASC' )); // Get parts categories from the database
$this->set('partCat', $partCat);
if(!$id && empty($this->request->data)) {
$this->Session->setFlash('Invalid Id for Image');
$this->redirect(array('action' => 'index'));
}
if (!empty($this->request->data) &&
is_uploaded_file($this->request->data['VwImage']['image']['tmp_name'])) {
$fileData = fread(fopen($this->request->data['VwImage']['image']['tmp_name'], "r"),
$this->request->data['VwImage']['image']['size']);
/** get image information **/
$size = getimagesize($this->request->data['VwImage']['image']['tmp_name']);
$image_width = $size[0];
$image_height = $size[1];
$image_size = $size[3];
$image_type = $size['mime'];
$image_thumb = null;
/** Create a second variable for the thumbnail **/
$thumb_data = $this->request->data['VwImage']['image']['tmp_name'];
$aspect_ratio = (float) ($image_width / $image_height );
$thumb_height = 100;
$thumb_width = $thumb_height * $aspect_ratio;
if($image_type == 'image/jpeg' ) {
$src = ImageCreateFromjpeg($thumb_data);
$destImage = ImageCreateTrueColor($thumb_width, $thumb_height);
ImageCopyResampled($destImage, $src, 0,0,0,0, $thumb_width, $thumb_height, $image_width, $image_height);
ob_start();
imageJPEG($destImage);
$image_thumb = ob_get_contents();
ob_end_clean();
}
if($image_type == 'image/gif' ) {
$src = ImageCreateFromgif($thumb_data);
$destImage = ImageCreateTrueColor($thumb_width, $thumb_height);
ImageCopyResampled($destImage, $src, 0,0,0,0, $thumb_width, $thumb_height, $image_width, $image_height);
ob_start();
imageJPEG($destImage);
$image_thumb = ob_get_contents();
ob_end_clean();
}
if($image_type == 'image/png' ) {
$src = ImageCreateFrompng($thumb_data);
$destImage = ImageCreateTrueColor($thumb_width, $thumb_height);
ImageCopyResampled($destImage, $src, 0,0,0,0, $thumb_width, $thumb_height, $image_width, $image_height);
ob_start();
imageJPEG($destImage);
$image_thumb = ob_get_contents();
ob_end_clean();
}
$this->request->data['VwImage']['file_name'] = $this->request->data['VwImage']['image']['name'];
$this->request->data['VwImage']['file_type'] = $this->request->data['VwImage']['image']['type'];
$this->request->data['VwImage']['size'] = $this->request->data['VwImage']['image']['size'];
$this->request->data['VwImage']['image'] = $fileData;
$this->request->data['VwImage']['id'] = $id;
if(!$image_thumb == null) {
$this->request->data['VwImage']['image_thumb'] = $image_thumb;
$this->request->data['VwImage']['thumb_height'] = $thumb_height;
$this->request->data['VwImage']['thumb_width'] = $thumb_width;
if ($this->VwImage->save($this->request->data)) {
$this->Session->setFlash('This image has been save');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('This Image could not be saved. Please try again.');
}
} else {
$this->Session->setFlash('Unsupported Image Type could not be saved. Please try again.');
}
}
if (empty($this->request->data)) {
$this->request->data = $this->VwImage->read(null, $id);
/** pr($this->request->data); die; **/
}
}
干杯迈克