1

我在我的项目中使用 cakephp 2.4.1。我有两个表 casts 和 castimage。现在 castimage 有两列 cast id 和 cast_image_path。现在我必须分别上传图像到演员 ID 并根据演员 ID 将其存储在不同的文件夹中?那么我将如何做到这一点以及如何将图像路径存储在数据库中?

4

3 回答 3

0

这个插件会做你想做的事:https ://github.com/josgonzalez/cakephp-upload

于 2013-10-20T20:31:04.323 回答
0

您在视图文件中添加

视图.ctp

<?php if (!empty($this->data['Contact']['filepath'])): ?>
<div class="input">
    <label>Uploaded File</label>
    <?php
    echo $this->Form->input('filepath', array('type'=>'hidden'));
    echo $this->Html->link(basename($this->data['Contact']['filepath']), $this->data['Contact']['filepath']);
    ?>
</div>
<?php else: ?>
<?php echo $this->Form->input('filename',array(
    'type' => 'file'
)); ?>

在您的控制器中

    public function add(){
    if ($this->request->is('post')) {
        // create
        $this->Castimage->create();

        // attempt to save
        if ($this->Castimage->save($this->request->data)) {
            $this->Session->setFlash('image has been successfully saved!');
            $this->redirect(array('action' => 'index'));

        // form validation failed
        } else {
            // check if file has been uploaded, if so get the file path
            if (!empty($this->Castimage->data['Castimage']['filepath']) && is_string($this->Castimage->data['Castimage']['filepath'])) {
                $this->request->data['Castimage']['filepath'] = $this->Castimage->data['Castimage']['filepath'];
            }
        }
    }
}
于 2013-10-21T05:37:16.427 回答
0
$cast_id = $this->request->data['Image']['cast_id];

如果没有创建目录,则新建一个

if(!is_dir($cast_id) {
$oldumask = umask(0);
mkdir($cast_id, 0777);
umask($oldumask);
}

$uniq = mt_rand();
    move_uploaded_file($this->requset->data['Image']['tmp_name'],WWW_ROOT.DS.$cast_id.DS.$uniq.'.'$this->request->data['Image']['name']);

我希望它能给你一些想法

于 2013-10-28T13:59:47.643 回答