CakePHP不显示上传的图片

时间:2014-03-08 01:48:28

标签: image cakephp

我真的变得疯狂了!

我用Meiouploader和PHPThumb制作了一个画廊。一切都很好。

我上传的图片也保存在文件夹img / uploads / images和我的数据库中。

但是在显示图像的领域我只看到alt文本。不是图像。

但是当检查HTML代码时,我看到了正确的图像路径。但是我没有看到它。

有什么不对?

请帮忙!

好的,这是我的所有代码: 我认为路径是正确的,因为在浏览器的源代码中我可以看到图像 - 标签。这是我的Image-Model代码:

class Image extends AppModel {
var $name = 'Image';
var $validate = array(
    'gallery_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    //'img_file' => array(
        //'notempty' => array(
            //'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        //),
    //),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed

var $belongsTo = array(
    'Gallery' => array(
        'className' => 'Gallery',
        'foreignKey' => 'gallery_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

var $actsAs = array(
    'MeioUpload' => array(
        'img_file' => array(
            'dir' => 'img{DS}uploads{DS}images',
            'create_directory' => false,
            'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/png'),
            'allowed_ext' => array('.jpg', '.jpeg', '.png'),
            'zoomCrop' => true,
            'thumbnails' => true ,
            'thumbnailQuality' => 75, 
            'thumbnailDir' => 'thumb',
            'removeOriginal' => true,
            'thumbsizes' => array(
                'normal' => array('width' => 400, 'height' => 300),
            ),
            'default' => 'default.jpg'          
        )
    )
);

}

这是我的图像控制器代码:

class ImagesController extends AppController {

var $name = 'Images';

function index() {
    $this->Image->recursive = 0;
    $this->set('images', $this->paginate());
}

function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid image', true));
        $this->redirect(array('action' => 'index'));
    }
    $this->set('image', $this->Image->read(null, $id));
}

function add() {
    if (!empty($this->data)) {
        $this->Image->create();
        if ($this->Image->save($this->data)) {
            $this->Session->setFlash(__('The image has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The image could not be saved. Please, try again.', true));
        }
    }
    $galleries = $this->Image->Gallery->find('list');
    $this->set(compact('galleries'));
}

function edit($id = null) {
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid image', true));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        if ($this->Image->save($this->data)) {
            $this->Session->setFlash(__('The image has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The image could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->data = $this->Image->read(null, $id);
    }
    $galleries = $this->Image->Gallery->find('list');
    $this->set(compact('galleries'));
}

function delete($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid id for image', true));
        $this->redirect(array('action'=>'index'));
    }
    if ($this->Image->delete($id)) {
        $this->Session->setFlash(__('Image deleted', true));
        $this->redirect(array('action'=>'index'));
    }
    $this->Session->setFlash(__('Image was not deleted', true));
    $this->redirect(array('action' => 'index'));
}
}

这是我的index.ctp代码 - 查看:

<div class="images index">
<h2><?php __('Images');?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
        <th><?php echo $this->Paginator->sort('id');?></th>
        <th><?php echo $this->Paginator->sort('gallery_id');?></th>
        <th><?php echo $this->Paginator->sort('name');?></th>
        <th><?php echo $this->Paginator->sort('img_file');?></th>
        <th class="actions"><?php __('Actions');?></th>
</tr>
<?php
$i = 0;
foreach ($images as $image):
    $class = null;
    if ($i++ % 2 == 0) {
        $class = ' class="altrow"';
    }
?>
<tr<?php echo $class;?>>
    <td><?php echo $image['Image']['id']; ?>&nbsp;</td>
    <td>
        <?php echo $this->Html->link($image['Gallery']['name'], array('controller' => 'galleries', 'action' => 'view', $image['Gallery']['id'])); ?>
    </td>
    <td><?php echo $image['Image']['name']; ?>&nbsp;</td>
    <!--<td><?php echo $image['Image']['img_file']; ?>&nbsp;</td>-->

    <td><?php  echo $html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'], array('alt' => 'Gallery Image', 'width' => '400')); ?></td>

    <td class="actions">
        <?php echo $this->Html->link(__('View', true), array('action' => 'view', $image['Image']['id'])); ?>
        <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $image['Image']['id'])); ?>
        <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $image['Image']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $image['Image']['id'])); ?>
    </td>
</tr>
<?php endforeach; ?>
    </table>
        <p>
        <?php
            echo $this->Paginator->counter(array(
            'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
            ));
        ?>  
        </p>

    <div class="paging">
        <?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
        <?php echo $this->Paginator->numbers();?>
        <?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
    </div>
</div>
<div class="actions">
    <h3><?php __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('New Image', true), array('action' => 'add')); ?></li>
        <li><?php echo $this->Html->link(__('List Galleries', true), array('controller' => 'galleries', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Gallery', true), array('controller' => 'galleries', 'action' => 'add')); ?> </li>
    </ul>
</div>

这是我的add.ctp代码 - 查看:

<div class="images form">
<?php // echo $this->Form->create('Image');?>
<?php echo $form->create('Image',array('type' => 'file')); ?>
<fieldset>
    <legend><?php __('Add Image'); ?></legend>
<?php
    echo $this->Form->input('gallery_id');
    echo $this->Form->input('name');
    //echo $this->Form->input('img_file');
    echo $form->input('img_file', array('type' => 'file'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>

    <li><?php echo $this->Html->link(__('List Images', true), array('action' => 'index'));?></li>
    <li><?php echo $this->Html->link(__('List Galleries', true), array('controller' => 'galleries', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Gallery', true), array('controller' => 'galleries', 'action' => 'add')); ?> </li>
</ul>
</div>

我在Jason Whydro的教程中做了所有的事情,但是效果并不好。它不会显示此字段中的图片,只显示其中的alt文本和宽度。

当我在浏览器中点击源代码中的其中一个图像链接时,他说我:没有对象。在服务器上找不到URL !!

我希望你能看到什么出错了。我没有看到它。用户权限是什么意思?如果这是问题,我该如何解决呢?我使用Windows 8。

...问候

1 个答案:

答案 0 :(得分:0)

如果您的路径正确,则意味着当您将其放在浏览器的网址栏上时,它会显示出来。

如果没有,可能是许可问题。尝试检查您的http用户的文件权限。