我使用CakePHP-Upload插件,现在需要使用没有表单的上传,如下例所示: 没有表单的编程文件检索
我的所有上传都存储在关联的模型中,称为附件。因此,当我保存文章时,同时将图像保存在Attachmen模型中。
插件文档建议如下:
<?php
$this->Article->set(array('file' => $image_path)); // ?????
$this->Article->save();
?>
我在服务器上有更大的图像集合,以前通过Joomla CMS上传,现在我将数据迁移到基于CakePHP框架构建的自定义CMS。我需要拍摄所有这些照片并通过此插件再次上传。
如何正确使用此插件来满足我的需求,我尝试将路径设置为本地图片,但上传不起作用???
编辑
$image_path = WWW_ROOT . 'img' . DS . 'attachment' . DS . '59'.DS.'hpim4799.jpg';
debug($image_path);
'C:xampphtdocsportalwebrootimgattachment59hpim4799.jpg'
不要保存记录,也不要创建新图像。请注意,通过HTML表单上传效果很好,我使用Windows 7和xampp服务器。
编辑 2/解决方案
图片路径必须是有效的网址,例如"http://localhost/portal/img/attachment/59/hpim4799.jpg"
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
谢谢。
你能告诉我变量$image_url
的内容是什么吗?双面文件表示您可以通过表单添加文件,也可以以编程方式添加文件。如果这样做,请使用$image_url
作为图像的路径。如果你有一个像Attachment
这样的关联模型,你应该使用:
<?php
$this->Article->set(array('Attachment.file' => $image_path)); // path + file to file
$this->Article->save();
?>
经过数小时的搜索解决方案和对行为代码的详细检查,我终于找到了它。
由于行为使用 php FILTER_VALIDATE_URL过滤器,因此 path 必须是有效的 URL。下面是一个示例:
<?php
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
// return http://localhost/portal/img/attachment/59/hpim4799.jpg
$this->Article->Attachment->set(array('file' => $image_path));
$this->Article->Attachment->save();
?>