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.

4

3 回答 3

4

替换这个:

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

有了这个:

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

但是,您确实应该进行更多验证,例如使用PHP 的is_uploaded_file函数,确保文件确实是 PDF 等。

于 2013-02-08T02:37:15.670 回答
1
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.上传前可以重命名

于 2013-09-04T12:35:15.173 回答
1

此行仅获取文件名,而不是文件。

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

你可以用这个。

$file = $_FILES ['Document'] ['submittedfile'];
于 2015-02-17T11:11:41.940 回答