Cakephp 版本 2.x 无法在编辑视图中显示选定的文件名
Cakephp Version 2.x Unable to Show selected file name in edit view
希望这是一个简单的问题。
我有一个 Cakephp MVC 设置来上传图像并将其存储在数据库中 table。
索引、查看和添加正在按我的意愿工作。
但是,在“编辑”视图中,我如何指示该记录当前有一张我可能会保留更改的已上传图片。
我一直无法弄清楚在文件输入按钮的属性中设置什么来显示现有文件名。
<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; **/
}
}
干杯
麦克
问题:
根据 CakePHP 2.0 book:
Due to the limitations of HTML itself, it is not possible to put
default values into input fields of type ‘file’. Each time the form is
displayed, the value inside will be empty.
虽然我确定您可以通过多种方式破解它(使用 javascript),但尝试在该字段中设置默认值是非标准做法。当您通过 "file" 输入 select 文件时,它会插入本地文件的路径。你怎么知道那是什么路径?
解决方法:
相反,在上方或下方添加一行显示 "previous filename" 及其文件名,或者如果是图像,则显示上一个文件的缩略图。
希望这是一个简单的问题。
我有一个 Cakephp MVC 设置来上传图像并将其存储在数据库中 table。 索引、查看和添加正在按我的意愿工作。
但是,在“编辑”视图中,我如何指示该记录当前有一张我可能会保留更改的已上传图片。
我一直无法弄清楚在文件输入按钮的属性中设置什么来显示现有文件名。
<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; **/
}
}
干杯 麦克
问题:
根据 CakePHP 2.0 book:
Due to the limitations of HTML itself, it is not possible to put default values into input fields of type ‘file’. Each time the form is displayed, the value inside will be empty.
虽然我确定您可以通过多种方式破解它(使用 javascript),但尝试在该字段中设置默认值是非标准做法。当您通过 "file" 输入 select 文件时,它会插入本地文件的路径。你怎么知道那是什么路径?
解决方法:
相反,在上方或下方添加一行显示 "previous filename" 及其文件名,或者如果是图像,则显示上一个文件的缩略图。