1

I just want to upload a single pdf file using cakephp, here is my view called 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'));?> 

Here is my conroller:

 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']); } 

It gives me this error:

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] 

And also I want to rename the file to 1.pdf. The file should save in webroot/files.

1
  • I am using cakephp 2.0.6 Commented Feb 8, 2013 at 0:37

3 Answers 3

4

Replace this:

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

with this:

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

However, you really should do more validation, like using PHP's is_uploaded_file function, making sure the file really is a PDF, etc.

Sign up to request clarification or add additional context in comments.

Comments

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

1.Change this code according to directory 2.c:xampp/htdocs(its a default location for uploading in cakephp ),then put your upload file location

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

3.You can rename it before uploading

Comments

1

This line get only file name, not file.

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

You could use this.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.