使用cakephp上传文件

时间:2013-02-08 00:37:10

标签: php cakephp cakephp-2.0

我只想使用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 个答案:

答案 0 :(得分:4)

替换它:

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

用这个:

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

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

答案 1 :(得分: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.您可以在上传

之前重命名

答案 2 :(得分:1)

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

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

你可以使用它。

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