1

i am getting an error in uploading image into folder. there is an error occured during uploading that image. my Controller Code is (cakeplus is my root folder ex: htpp://localhost/cakeplus) :

$directory = "http://".$_SERVER['HTTP_HOST'].'/cakeplus/pics'; if(!is_dir($directory)) { mkdir($directory,0777); } $files_array = $this->data['Photo']['path']; //pr($files_array); die; if(isset($files_array['name']) && $files_array['name']!='') { $filetype = $files_array['type']; $filesize = $files_array['size']; $filename = $files_array['name']; $filetmpname = $files_array['tmp_name']; $file_type = explode('.',$filename); $ext_name = array_reverse($file_type); $final_file_title = $ext_name[1]; $file_name = Inflector::slug( str_replace( ".".$ext_name[0], "" , $filename ). '_' .time() ,'-' ); $newFileName = $file_name.'.'.$ext_name[0]; move_uploaded_file($filetmpname, $directory.'/'.$newFileName); $requirementuploadData['Photo']['path'] = $file_name; $this->Photo->create(); $this->Photo->save($requirementuploadData,false); } 

Error(s)(Warnings) :

Warning (2): move_uploaded_file(http://localhost/cakeplus/pics/wallpaper-1433586197.png): failed to open stream: HTTP wrapper does not support writeable connections [APP\Controller\PhotosController.php, line 31] Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA80D.tmp' to 'http://localhost/cakeplus/pics/wallpaper-1433586197.png' [APP\Controller\Photos 
1
  • Like the error says, you can't write to an HTTP stream. You need to write the file to the file system, or use some other method to move it elsewhere if that's what you need to do Commented Jun 6, 2015 at 10:35

2 Answers 2

1

Look into the CakePHP Upload plugin - it will abstract away much of the work that goes into dealing with file and image uploads.

The error you are seeing is because you cannot use move_uploaded_file() to transfer from a file path (C:\xampp\tmp\phpA80D.tmp) to an HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png).

If you don't want to use the Upload plugin, and would prefer to keep working with what you already have, I would start by changing the $directory path. Something like this might be more appropriate:

$directory = WWW_ROOT . 'pics'; 

This will contain the path to your ./yourapp/webroot/pics directory, which is also the location of http://yourapp.com/pics.

Check out the documentation for more predefined paths.

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

Comments

0

may be folder dont have permission to write an image. you should to use cakephp upload component.

$this->Upload->upload($file,$destination,$name); 

1 Comment

dont need to use "http://".$_SERVER['HTTP_HOST'] in $directory just use folder path.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.