2011-10-22 53 views
0

**我为dvd库建立dvd脚本..当我想编辑dvd详细信息图像我uploded不显示,但是当我看到databse表我看到它已经进入这张表,我发现图像在webroot img文件夹(dvd)..所以如何显示图像进行编辑?如何显示图像进行编辑?

DVD光盘控制器

<?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> 
+0

'$ this-> data ['Dvd'] ['image']'包含什么?一个完整的路径?只是图片的名字? – Ross

+0

img/dvds/Blue_hills.jpg – fakhrawy

+0

当我把这个URL我看到图像http://localhost/dvdcatalog/img/dvds/Blue_hills.jpg – fakhrawy

回答

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" />

使用,它使用了正确的根目录路径输出链接等

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; 
图像帮手

你应该得到想要的结果。

+0

谢谢你,你是好男人..现在是工作 – fakhrawy

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); 
} 
+0

嗨。哇。代码长城。对代码提供一些解释以及它如何解决原始问题,一般都是很好的形式。你应该知道网站上有许多新手 - 他们可以从你的专业知识中学到一两件事 - 但只有当你提供一些解释。不要忘记,什么可能是一个明显的事情,你不会对他们:) –

相关问题