私のウェブサイトにはたくさんの画像があり、それらの画像の管理編集をしようとすると、1 つの画像だけを編集する必要がある場合でも、すべての画像に対して毎回画像名を入力する必要があります。はタイプ「ファイル」です。タイプ「ファイル」の画像のデフォルト値を入力フィールドに入力するための回避策はありますか? 私のコードは次のとおりです。
if ($this->request->is('post') || $this->request->is('put')) {
$mainimg = $this->request->data['Product']['image'];
$img1 = $mainimg['name'];
if ($mainimg['error'] === UPLOAD_ERR_OK){
move_uploaded_file($mainimg['tmp_name'], APP.'webroot'.DS.'img'.DS.$img1);
}
//the uploaded file renamed
$this->request->data['Product']['image'] = $img1;
これは私の admin_view.ctp です
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
<div class="Products form">
<?php echo $this->Form->create('Product',array('enctype' => 'multipart/form-data'));?>
<legend><?php echo __('Admin Edit Product'); ?></legend>
<fieldset>
<?php
echo $this->Form->input('id');
echo $this->Form->input('name');
echo $this->Form->input('description',array('class'=>'ckeditor'));
echo $this->Form->label('Product Image');
$img=$this->data['Product']['image'];
echo $this->Html->image($img,array('id'=>'blah','width'=>'100px'));?><br>
<div id="dim"><?php echo "Dimension : 170 × 195px";?> </div>
<input type='file' name="data[Product][image]" id="image" onchange='readURL(this);' >
<?php echo $this->Form->end(__('Save'));?>
</fieldset>
</div>
JavaScript は、選択した画像をプレビューするためのものです。このコードでデフォルトの画像をプレビューすることはできますが、編集する画像の名前を取得することはできません..