【问题标题】:Not uploading image on Cakephp 3不在 Cakephp 3 上上传图片
【发布时间】:2016-08-05 14:53:23
【问题描述】:

我是 CakePHP 的新手,我正在使用这个框架创建一个应用程序,现在我遇到了一点问题,不是上传图像,而是图像名称保存到数据库。在我的代码下方:

public function save()
{
    $shop = TableRegistry::get('shop');
    $query = $shop->query();
    //

    $product = $this->request->data('prductname');
    $domain = $this->request->data('domainname');
    $phone = $this->request->data('phone');

    if (!empty($this->request->data)) 
    {
       $file = $this->request->data['file'];
    }

        $filename = $file;
        $file_tmp_name = $filename;
        $dir = WWW_ROOT.'img'.DS.'uploads';
        $allowed = array('png','jpg','jpeg' );
        if (!in_array(substr(strrchr($filename,'.'),1),$allowed)) 
        {
            throw new InternalErrorException("Error Processing Request", 1);
        }
        elseif(is_uploaded_file($file_tmp_name))
        {
            $filename = Text::uuid().'-'.$filename;
            $fileDB = TableRegistry::get('shop');
            $entity = $fileDB->newEntity();
            $entity->filename = $filename;
            $fileDB->save($entity);
            move_uploaded_file($file_tmp_name, $dir.DS.$filename);
        }

    $query->insert([

        'prductname','domainname','phone','file'

    ])->values([

        'prductname'=>$product,
        'domainname'=>$domain,
        'phone'=>$phone,
        'file' => $file

    ])->execute();

    if ($query) 
    {
        $this->Flash->success('This information has been saved.');
        $this->redirect(['controller'=>'shop','action'=>'index']);
    }
}

表格:

<?php 
    echo $this->Form->create(null,['url'=>['controller'=>'Shop', 'action'=>'save']],['enctype' => 'multipart/form-data']); 
?>

    <div class="form-group">
        <label for="product">Product Name</label>
        <input type="text" class="form-control" name="prductname" placeholder="Name">
    </div>

    <div class="form-group">
        <label for="domain">Domain Name</label>
        <input type="text" name="domainname" class="form-control" placeholder="Domain Name">
    </div>

    <div class="form-group">
        <label for="Phone">Phone</label>
        <input type="text" name="phone" class="form-control" placeholder="Phone">
    </div>

     <div class="form-group">
        <label for="InputFile">File input</label>
        <input type="file" name="file">
    </div>

    <button type="submit" class="btn btn-default">Save</button>
<?php
    echo $this->Form->end();
?>

未显示任何错误,图像名称保存到数据库但未将图像替换到目标文件夹。

谢谢

【问题讨论】:

  • 能否提供move_uploaded_file();函数的代码?

标签: php cakephp cakephp-3.0


【解决方案1】:

我认为$file_tmp_name 可能与move_uploaded_file 的预期不符。 move_uploaded_file 期望 $tmp_name 是 PHP 分配给每个上传文件的唯一字符串。我不确定$this-&gt;request-&gt;data['file'] 究竟会有什么结构,但$_FILES 数组有数组键,例如'name''size''tmp_name''type'。您需要使用move_uploaded_file 中的'tmp_name'。这可能存在于$this-&gt;request-&gt;data 中,如果是这样,最好使用该数组中的'tmp_name'

http://php.net/manual/en/function.move-uploaded-file.php

http://php.net/manual/en/features.file-upload.php

【讨论】:

    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多