首页 文章

使用cakephp上传文件

提问于
浏览
1

我只想使用cakephp上传一个pdf文件,这是我的视图 pdfadd.ctp

<?php echo $this->Form->create('pdfadd1', array('enctype' => 'multipart/form-data'));?>
    <fieldset>
    <?php
        echo $this->Form->file('Document.submittedfile');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit'));?>

这是我的控制者:

public function pdfadd(){
     if ($this->request->is('post') || $this->request->is('put')) {
         //die();
         $file = $this->request->data['Document']['submittedfile'];
         //$this->pdfadd1->save($this->request->data);
         move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
     }

它给了我这个错误:

Warning (2): move_uploaded_file(D:/Program Files D/xampp/htdocs/app/webroot/files/Functions Package for email (1).pdf): failed to open stream: No such file or directory [APP\Controller\PagesController.php, line 29]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Program Files D\xampp\tmp\php862.tmp' to 'D:/Program Files D/xampp/htdocs/app/webroot/files/Functions Package for email (1).pdf' [APP\Controller\PagesController.php, line 29]

而且我想将文件重命名为 1.pdf . 该文件应保存在 webroot/files 中 .

3 回答

  • 1

    替换这个:

    $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']
    

    有了这个:

    WWW_ROOT . 'files' . DS . '1.pdf'
    

    但是,你真的应该做更多的验证,比如使用PHP's is_uploaded_file function,确保文件确实是PDF等 .

  • 4
    move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'],      $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
    

    1.根据目录2.c:xampp / htdocs(它是在cakephp上传的默认位置)更改此代码,然后将您的上传文件位置

    move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . 'cakephp/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
    

    3.您可以在上传前重命名

  • 1

    此行只获取文件名,而不是文件 .

    $file = $this->request->data['Document']['submittedfile'];
    

    你可以用它 .

    $file = $_FILES ['Document'] ['submittedfile'];
    

相关问题