如何显示图像进行编辑?

时间:2011-10-22 13:26:21

标签: cakephp cakephp-1.3 cakephp-1.2

**我为dvds库构建dvd脚本..当我想编辑dvds详细信息我已经上传的图像不显示但是当我看到数据库表我看到它已经进入这个表,我在webroot中找到图像img文件夹(dvds)..那么如何显示图像进行编辑呢?

Dvds控制器

    <?php


class DvdsController extends AppController {
    // good practice to include the name variable
    var $name = 'Dvds';

    // load any helpers used in the views
    var $helpers = array('Html', 'Form', 'Javascript', 'Misc');

    // global ratings variable
    var $ratings = array('0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9', '10'=>'10');

    /**
     * index()
     * main index page for dvds
     * url: /dvds/index
     */
    function index() {
        // get all dvds from database where status = 1
        //$dvds = $this->Dvd->find("Dvd.status=1", null, "Dvd.name");
     $dvds = $this->Dvd->find('all', array('conditions' => array('Dvd.status' => '1')));
        // save the dvds in a variable for the view
        $this->set('dvds', $dvds);
    }

    /**
     * view()
     * displays a single dvd and all related info
     * url: /dvds/view/dvd_slug
     */
    function view($slug) {
        // if slug is null
        if(!$slug) {
            // set a flash message
            $this->Session->setFlash('Invalid slug for DVD', 'flash_bad');
            // redirect user
            $this->redirect(array('action'=>'index'));
        }

        // find dvd in database
        $dvd = $this->Dvd->findBySlug($slug);

        // if dvd has been found
        if(!empty($dvd)) {
            // set the dvd for the view
            $this->set('dvd', $dvd);
        } else {
            // set a flash message
            $this->Session->setFlash('Invalid slug for DVD', 'flash_bad');
            // redirect user
            $this->redirect(array('action'=>'index'));
        }
    }

    /**
     * admin_index()
     * main index page for admin users
     * url: /admin/dvds/index
     */
    function admin_index() {
        // get all dvds from database where status = 1, order by dvd name
       //   $dvds = $this->Dvd->findAll("Dvd.status=1", null, "Dvd.name");
        $dvds = $this->Dvd->find('all', array('conditions' => array('Dvd.status' => '1')));

        // save the dvds in a variable for the view
        $this->set('dvds', $dvds);
    }

    /**
     * admin_add()
     * allows an admin to add a dvd
     * url: /admin/dvds/add
     */
    function admin_add() {
        // if the form data is not empty
        if (!empty($this->data)) {
            // check for image
            $image_ok = $this->_upload_image();

            // if the image was uploaded successfully
            if($image_ok) {
                // initialise the Dvd model
                $this->Dvd->create();

                // create the slug
                $this->data['Dvd']['slug'] = $this->slug($this->data['Dvd']['name']);

                // check for a dvd with the same slug
                $dvd = $this->Dvd->find('first', array(
                    'conditions' => array(
                        'Dvd.slug'=>$this->data['Dvd']['slug'],
                        'Dvd.status' => '1'
                    )
                ));

                // if slug is not taken
                if(empty($dvd)) {
                    // try saving the dvd
                    if ($this->Dvd->save($this->data)) {
                        // set a flash message
                        $this->Session->setFlash('The DVD has been saved', 'flash_good');

                        // redirect
                        $this->redirect(array('action'=>'index'));
                    } else {
                        // set a flash message
                        $this->Session->setFlash('The DVD could not be saved. Please, try again.', 'flash_bad');
                    }
                } else {
                    // set a flash message
                    $this->Session->setFlash('The DVD could not be saved. The Name has already been taken.', 'flash_bad');
                }
            }
        }

        // find dvd options in a list format
        // new 1.2 feature, can also have 'count' and 'first'
        $formats    = $this->Dvd->Format->find('list');
        $types      = $this->Dvd->Type->find('list');
        $locations  = $this->Dvd->Location->find('list');
        $ratings    = $this->ratings;

        // set the variables so they can be accessed from the view
        $this->set(compact('formats', 'types', 'locations', 'ratings'));
    }

    /**
     * admin_edit()
     * allows an admin to edit a dvd
     * url: /admin/dvds/edit/id
     */
    function admin_edit($id = null) {
        // if the id is null and the form data empty
        if (!$id && empty($this->data)) {
            // set a flash message
            $this->Session->setFlash('Invalid Dvd', 'flash_bad');
            // redirect the admin
            $this->redirect(array('action'=>'index'));
        }

        // if the form was submitted
        if (!empty($this->data)) {
            // check for image
            $image_ok = $this->_upload_image();

            // if the image was uploaded successfully
            if($image_ok) {
                // create the slug
                $this->data['Dvd']['slug'] = $this->slug($this->data['Dvd']['name']);

                // check for a dvd with the same slug
                $dvd = $this->Dvd->find('first', array(
                    'conditions' => array(
                        'Dvd.slug'=>$this->data['Dvd']['slug'],
                        'Dvd.status' => '1'
                    )
                ));

                // if slug is not taken
                if(empty($dvd) || $dvd['Dvd']['id'] == $id) {
                    // try to save the Dvd
                    if ($this->Dvd->save($this->data)) {
                        // set a flash message
                        $this->Session->setFlash('The Dvd has been saved', 'flash_good');
                        // redirect the admin
                        $this->redirect(array('action'=>'index'));
                    } else {
                        // set a flash message
                        $this->Session->setFlash('The Dvd could not be saved. Please, try again.', 'flash_bad');
                    }
                } else {
                    // set a flash message
                    $this->Session->setFlash('The DVD could not be saved. The Name has already been taken.', 'flash_bad');
                }
            }
        } else {
            // find the DVD from the database and save it in the data array
            $this->data = $this->Dvd->read(null, $id);
        }

        // find dvd options from database in a list
        $formats    = $this->Dvd->Format->find('list');
        $types      = $this->Dvd->Type->find('list');
        $locations  = $this->Dvd->Location->find('list');
        $ratings    = $this->ratings;
        $this->set(compact('formats','types','locations', 'ratings'));
    }

    /**
     * admin_delete()
     * allows an admin to delete a dvd
     * url: /admin/dvds/delete/1
     */
    function admin_delete($id = null) {
        // if the id is null
        if (!$id) {
            // set flash message
            $this->Session->setFlash('Invalid id for Dvd', 'flash_bad');
            // redirect
            $this->redirect(array('action'=>'index'));
        }

        // set the id of the dvd
        $this->Dvd->id = $id;

        // try to change status from 1 to 0
        if ($this->Dvd->saveField('status', 0)) {
            // set flash message
            $this->Session->setFlash('The Dvd was successfully deleted.', 'flash_good');
        } else {
            // set flash message
            $this->Session->setFlash('The Dvd could not be deleted. Please try again.', 'flash_bad');
        }

        // redirect
        $this->redirect(array('action'=>'index'));
    }

    /**
     * upload_image()
     * private function to upload a file if it exists in the form
     */
    function _upload_image() {
        // init
        $image_ok = TRUE;

        // if a file has been added
        if($this->data['File']['image']['error'] != 4) {
            // try to upload the file
    $result = $this->upload_files('img/dvds', $this->data['File']);

            // if there are errors
            if(array_key_exists('errors', $result)) {
                // set image ok to false
                $image_ok = FALSE;
                // set the error for the view
                $this->set('errors', $result['errors']);
            } else {
                // save the url
                $this->data['Dvd']['image'] = $result['urls'][0];
            }
        }

    return $image_ok;
    }
}

?>

,视图文件

<div class="dvds form">

<?php
// if there was an error uploading the file then display errors here
if(isset($errors)) {
    echo $misc->display_errors($errors);
}
?>

<?php echo $form->create('Dvd', array('type'=>'file'));?>
    <fieldset>
        <legend>Edit a Dvd</legend>
        <?php
        // create the form inputs

        // include the id of the DVD as a form input
        // CakePHP will automatically create this as a hidden element
        echo $form->input('id');
        echo $form->input('name', array('label'=>'Name: *'));
        echo $form->input('format_id', array('label'=>'Format: *', 'type'=>'select', 'options'=>$formats));
        echo $form->input('type_id', array('label'=>'Type: *', 'class'=>'type_select'));
        echo $form->input('location_id', array('label'=>'Location: *'));

        // display image if it exists
        if(!empty($this->data['Dvd']['image'])): ?>
        <div class="input">
            <label>Current Image:</label>
<img src="/<?php echo $this->data['Dvd']['image']; ?>" width="100" />
        </div>
        <?php endif;

        echo $form->input('File.image', array('label'=>'Image:', 'type'=>'file'));
        echo $form->input('rating', array('label'=>'Rating:'));
        echo $form->input('website', array('label'=>'Website URL:'));
        echo $form->input('imdb', array('label'=>'Imdb URL:'));
        echo $form->input('discs', array('label'=>'Number of Discs:', 'class'=>'tv_hide'));
        echo $form->input('episodes', array('label'=>'Number of Episodes:', 'class'=>'tv_hide'));
        ?>
    </fieldset>
<?php echo $form->end('Save');?>
</div>

<ul class="actions">
    <li><?php echo $html->link('List DVDs', array('action'=>'index'));?></li>
    <li><?php echo $html->link('Add a DVD', array('action'=>'add'));?></li>
</ul>

2 个答案:

答案 0 :(得分:1)

   <img src="/<?php echo $this->data['Dvd']['image']; ?>" width="100" />

产生

<img src="/img/dvds/Blue_hills.jpg" width="100" />

正如您的评论所示,实际的src应为:

<img src="/dvdcatalog/img/dvds/Blue_hills.jpg" width="100" />

甚至

<img src="http://localhost/dvdcatalog/img/dvds/Blue_hills.jpg" width="100" />

使用图像帮助器,它使用正确的webroot路径输出链接等

 if(!empty($this->data['Dvd']['image'])): ?>
    <div class="input">
     <label>Current Image:</label>
     <?php 
       echo $this->Html->image($this->data['Dvd']['image'], array('width'=>100));
     ?>
    </div>
  <?php endif; 

你应该得到理想的结果。

答案 1 :(得分:0)

    public function admin_edit($id) {
    $categories = $this->JobCategory->find('list', array('conditions' => array('JobCategory.status' => 1)));
    $this->set('cat', $categories);

    if ($this->request->is('post') || $this->request->is('put')) {
        $filename = strtotime('now') . $this->request->data['Job']['add_file']['name'];

        /* copy uploaded file */
        if (move_uploaded_file($this->request->data['Job']['add_file']['tmp_name'], WWW_ROOT . "img/job_images/" . $filename)) {

            $this->request->data['Job']['add_file'] = $filename;
        } else {
            unset($this->request->data['Job']['add_file']);
        }
        if ($this->Job->save($this->request->data)) {
            $this->Session->setFlash(__d('Job', 'Job Updated'), 'default', array('class' => 'success'));
            $this->redirect(array('admin' => true, 'action' => 'index'));
        }
    }
    $this->request->data = $this->Job->findById($id);
}