Pimcore:PHP API资产CRUD无法打开流错误

时间:2018-03-19 16:52:38

标签: php pimcore

我正在尝试使用以下文档将图像保存为资产。

我需要明确第3行的内容。

我的图片在web / var / assets文件夹中。 (这是保留图像的正确位置吗?或任何1来解释代码)

我收到的错误如下:

  

警告:file_get_contents(myimage1.png):无法打开流:没有这样的文件或目录

https://pimcore.com/docs/5.x/Development_Documentation/Assets/Working_with_PHP_API.html

//creating and saving new asset
$newAsset = new Pimcore\Model\Asset();
$newAsset->setFilename("myAsset.png");
$data = file_get_contents('/web/var/assets/products/2_a8.JPG');
$newAsset->setData($data);
$newAsset->setParent(Pimcore\Model\Asset::getByPath("/"));

1 个答案:

答案 0 :(得分:1)

如果您的图片位于web / var / assets文件夹中,则可能不在myimage1.png

PHP抛出警告,因为它无法找到(并读取)图像,路径错误。 如果你的myimage1.png在web / var / assets中,你的代码可能看起来像这样:

$projectRoot = getenv('PIMCORE_PROJECT_ROOT'); $data = file_get_contents($projectRoot ."/web/var/assets/myimage1.png"); $newAsset->setData($data);

如果file_get_contents失败,则路径可能错误。 Pimcore现阶段没有参与。在PHP手册中查找有关该函数的更多信息:http://php.net/manual/de/function.file-get-contents.php