在本地主机上的 CakePHP 中上传图像
Image Upload in CakePHP on localhost
我在将图像上传到文件夹时遇到错误。上传该图像时发生错误。
我的控制器代码是(cakeplus 是我的根文件夹,例如: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);
}
错误(警告):
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
可能是文件夹没有写入图像的权限。
你应该使用cakephp上传组件。
$this->Upload->upload($file,$destination,$name);
研究 CakePHP Upload plugin - 它会抽象出处理文件和图像上传的大部分工作。
您看到的错误是因为您无法使用 move_uploaded_file()
从文件路径 (C:\xampp\tmp\phpA80D.tmp
) 传输到 HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png
)。
如果您不想使用上传插件,并且希望继续使用您已有的插件,我将从更改 $directory
路径开始。这样的东西可能更合适:
$directory = WWW_ROOT . 'pics';
这将包含您的 ./yourapp/webroot/pics
目录的路径,这也是 http://yourapp.com/pics
.
的位置
查看文档了解更多信息 predefined paths。
我在将图像上传到文件夹时遇到错误。上传该图像时发生错误。 我的控制器代码是(cakeplus 是我的根文件夹,例如: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);
}
错误(警告):
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
可能是文件夹没有写入图像的权限。 你应该使用cakephp上传组件。
$this->Upload->upload($file,$destination,$name);
研究 CakePHP Upload plugin - 它会抽象出处理文件和图像上传的大部分工作。
您看到的错误是因为您无法使用 move_uploaded_file()
从文件路径 (C:\xampp\tmp\phpA80D.tmp
) 传输到 HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png
)。
如果您不想使用上传插件,并且希望继续使用您已有的插件,我将从更改 $directory
路径开始。这样的东西可能更合适:
$directory = WWW_ROOT . 'pics';
这将包含您的 ./yourapp/webroot/pics
目录的路径,这也是 http://yourapp.com/pics
.
查看文档了解更多信息 predefined paths。