如何在cakephp2.5+中上载图像并将其存储在webroot文件夹中
我的控制器如何在cakephp2.5+中上载图像并将其存储在webroot文件夹中,cakephp,image-uploading,cakephp-2.3,Cakephp,Image Uploading,Cakephp 2.3,我的控制器 public function add() { $this->helpers = array('TinyMCE.TinyMCE'); $this->layout = 'adminpanel'; if ($this->request->is('post')) { $this->Tour->create(); if ($this->Tour->save($this->reque
public function add() {
$this->helpers = array('TinyMCE.TinyMCE');
$this->layout = 'adminpanel';
if ($this->request->is('post')) {
$this->Tour->create();
if ($this->Tour->save($this->request->data)) {
$this->Session->setFlash(__('Your possstt has been saved.'));
return $this->redirect(array('controller'=>'admin','action' => 'tour'));
}
$this->Session->setFlash(__('Unable to add your schedule.'));
}
我的视图文件
echo $this->Form->create('Tour');
echo $this->Form->input('vartitle',array('class' => 'form-control','label' => 'Tour Title'));
// etc.................
echo $this->Form->input('varbigimg', array('type' => 'file'));
echo $this->Form->end('Save Post',array('class' => 'form-control'));
我已经检查了模型,因为图像名称存储在数据库中,但我想使用该图像。我想将该图像保存在任何文件夹中,请帮助我。我正在使用新版本,谢谢
所以,告诉我如何将图像存储在webroot文件夹中,并将名称保存在数据库字段varbigimgfield中。我还想在页面上显示它。所以请解决我的问题谢谢
我是cakephp新手,谢谢你,伙计,我能让你简单一点。 首先,您需要使用$this->data['Tour']['varbigimg']访问控制器中的文件; 好的,这是你的档案。 好的,现在您需要将这个文件发送到webroot,您可以使用cakephp的file类 您可以使用WWW_ROOT约束将文件移动和复制到webroot,如果需要发送到webroot img use IMAGES常量,此常量将为您提供到webroot文件夹的正确路径。 使用、文件夹/文件和cakephp常量,很容易满足您的需求。 如果你需要更多关于这方面的信息,请问我,我为你创建一个真实的例子。
Tnx您需要将上载的文件移动到文件夹中。获取$this->request->data的文件信息并移动到webroot中的文件夹 有很多插件。例如:使用以下命令
echo $this->Form->create('Tour',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));
允许处理图像/视频数据
内部控制器
if ($this->request->is('post')) {
if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
{
$fileNameFull = $this->request->data['Tour']['varbigimg']['name'];
$oldFile = '../../app/webroot/img/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Tour']['varbigimg']['tmp_name'],
$oldFile
);
$this->Tour->create();
$this->request->data['Tour']['varbigimg'] = $fileNameFull;
//这里您需要为thum指定相同的值
$this->request->data['Tour']['varthumimg'] = $fileNameFull;
////这里您需要为thumbfield指定相同的值
if ($this->Tour->save($this->request->data)) {
$this->Session->setFlash(__('Your possstt has been saved.'));
return $this->redirect(array('controller'=>'admin','action' => 'tour'));
}
}
}
$this->Session->setFlash(__('Unable to add your schedule.'));
//更新
//在此明确指定代码中的输入类型
echo $this->Form->input('vartitle',array('type'=>'text','class' => 'form-control','label' => 'Tour Title'));
这个问题读起来好像你希望有人为你写代码。试着用一个关于你遇到的问题的特定问题,以及错误的细节,或者你不理解的项目,等等来构建它。到目前为止你试过什么?什么有效,什么无效……等等。在线上有很多关于这个主题的资源,所以这里不是一个进行一般操作的地方。我在这个主题上尝试了太多,没有得到任何解决方案,所以我把它贴在这里谢谢回复。但是当我粘贴这段代码时,我遇到了}错误语法错误,意外的“{”ifis_上传的文件$this->请求->数据['Tour']['varbigimg']['tmp_name']{所以这个括号错误plz我正在尝试,但不能。得到它。谢谢你的建议。plz最后解决1thanks@newsurt,现在检查,其中一个丢失了anks,我再次发现sql错误列未找到:“字段列表”数组到字符串转换中的1054未知列“Array”[CORE/Cake/Model/Datasource/DboSource.php,第1009行]在这个$this->request->data['Tour']['varbigimg']=$fileNameFull;,使用'print_$this->request->data['Tour']['varbigimg'];`,得到什么值,更新答案。我的图像存储在我的文件夹中,但我的数据没有保存在数据库中,它说数组到字符串对话。SQLSTATE[42S22]:未找到列:1054“字段列表”34中的未知列“数组”,2014-09-17 17:16:36.png的屏幕截图,数组1我没有提到的是我传递了2张图像。在此表单中