1

私のウェブサイトにはたくさんの画像があり、それらの画像の管理編集をしようとすると、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 &#215; 195px";?> </div>

<input type='file' name="data[Product][image]"  id="image" onchange='readURL(this);' >

        <?php echo $this->Form->end(__('Save'));?>
    </fieldset>

</div>    

JavaScript は、選択した画像をプレビューするためのものです。このコードでデフォルトの画像をプレビューすることはできますが、編集する画像の名前を取得することはできません..

4

2 に答える 2

0

PHP の $_FILES のスーパー グローバルは、ファイル名に基づいて画像に自動的に名前を付けるのに十分な情報を提供するはずです。

追加してみる

if ($this->request->is('post') || $this->request->is('put')) {
   pr($_FILES);
   ...

$_FILES グローバルを介して渡されるデータを確認し、それが提供する名前を使用するには、コントローラ関数に追加します。のようなものになります$_FILES['image'][0]['name'];。ファイルのサイズ、寸法など、データベースに保存するためのあらゆる種類の情報もあります。

于 2013-07-09T13:04:40.677 に答える
0

私の問題は、誰かがテキストを編集するたびに、ファイルを選択して画像を選択する必要があることでした。ユーザーが画像を変更する必要がない限り、ユーザーが画像を選択しなくても現在の値が存在するという解決策を見つけました。次のコードをコントローラーに追加しました

$product=$this->Product->read(NULL, $id);
$currentimg= $product['Product']['image'];
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;
}
 elseif($mainimg['error'] === UPLOAD_ERR_NO_FILE){
                    $this->request->data['Product']['image']=$currentimg;
                }
于 2013-08-01T02:24:17.663 に答える