Search code examples
phpcakephpfile-ioeditview

Cakephp Version 2.x Unable to Show selected file name in edit view


Hopefully this is a simple issue.

I have a Cakephp MVC set to upload an image and store it in a database table. Index, View and Add are working as I want.

However, in the Edit view how can I indicate that the record currently has an uploaded image that I may with to keep of change .

I have not been able to figure out what to set in the attributes of the File Input Button to show the existing file name.

<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();
?>

Controller Code for edit

    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; **/
    }
}

Cheers Mike


Solution

  • The Problem:

    According to the 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.

    While I'm sure there are ways you could hack it (with javascript), it's non-standard practice to try to set a default into that field. When you select a file via a "file" input, it's inserting a path to a local file. How would you know what path that is?


    The Solution:

    Instead, add a line above or below that shows "previous filename" with it's filename, or if it's an image, show a thumbnail of previous file.