Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何在Cakephp中获取图像的完整URL?_Image_Url_Cakephp_Hyperlink_Path - Fatal编程技术网

Image 如何在Cakephp中获取图像的完整URL?

Image 如何在Cakephp中获取图像的完整URL?,image,url,cakephp,hyperlink,path,Image,Url,Cakephp,Hyperlink,Path,假设我在assets文件夹中存储了一个“image.jpg”,路径是“www.example.com/public”,我只需要图像的字符串路径。我怎么得到这个?我不想要html标签,只想要路径字符串 $this->Html->url('/img/image.jpg') // returns /cake/app/img/image.jpg 可以选择将true传递给示例一以获取完整的基本URL。(mysite.com/cake/app/img/image.jpg) 我认为你需要自己创作

假设我在assets文件夹中存储了一个“image.jpg”,路径是“www.example.com/public”,我只需要图像的字符串路径。我怎么得到这个?我不想要html标签,只想要路径字符串

$this->Html->url('/img/image.jpg') // returns /cake/app/img/image.jpg
可以选择将
true
传递给示例一以获取完整的基本URL。(
mysite.com/cake/app/img/image.jpg


我认为你需要自己创作这条路。如果您知道文件名及其所在的目录,是什么阻止您执行以下操作:

$imageUrl = sprintf("%s/%s/%s", FULL_BASE_URL, "public", $imageName);
//  this produces http://www.example.com/public/image.png

检查cakephp 2.0+
FULL\u BASE\u URL提供的该常量


要了解所有内容,请查看的源代码,查看此帮助程序如何创建正确的URL;稍加修改,这就是它的实现方式:

$imageUrl = $this->assetUrl('image.jpg', array(
    // note: only required if you need the
    // URL -including- http://example.com
    'fullBase'   => true,
    'pathPrefix' => IMAGES_URL
));
Helper::assetUrl()的源代码可以在这里找到:

您可以使用:

$this->webroot.'img/abc.jpg'

$this->webroot将为您提供应用程序的当前路径。

在尝试各种CakePHP全局常量(ROOT、webroot\u DIR、WWW\u ROOT、CSS等)但没有结果后,一般解决方案似乎可以在$this->webroot属性中找到,该属性返回应用程序webroot的路径。因此,对于上述各种情况,我们的布局、视图或元素中可能包含:

一个简单的图像标签:

<img src="<?php echo $this->webroot; ?>img/foo.gif" .../ > 

在CakePHP中,图像通常存储在webroot/img文件夹中

这将对你有帮助

     HtmlHelper::image(string $path, array $options = array())
参数:

     $path (string) – Path to the image.

     $options (array) – An array of html attributes.
希望你觉得这个有用。

我会用

$this->Html->url('/', true).'img/image.jpg';
$imageUrl=$this->Html->assetUrl('image.jpg',数组( //注意:仅当您需要 //URL-包括-http://example.com “fullBase”=>true, “路径前缀”=>图像\u URL ));
即使在CakePHP版本3中使用主题,也可以使用url帮助程序:

$this->Url->build('/img/image.jpg');
在CakePHP 3(版本3.2.4)中,您可以使用url/image helper,但路径不是必需的:

$this->Url->image('image.jpg');
或其他资产:

// Outputs /js/app.js
$this->Url->script('my.js');

// Outputs /css/app.css
$this->Url->css('my.css');

对于CakePHP 3.5,我认为最好的选择是:

$imgUrl = Cake\Routing\Router('/', true) . 'img/' . $imageName;
如果您的应用程序在插件范围内,
$this->webroot
不适合

希望它能帮助别人。

示例:

在cake 3+中,您可以使用:$this->request->webroot


返回:/ninavendas/rhhealth\u vendas/

fullBase
选项-返回带有域名的完整URL

$this->Url->image('image.jpg', ['fullBase'=>true]);
CakePHP3

使用蛋糕\路由\路由器

<img src="<?php echo Router::url('/', true); ?>/img/name.jpg" />
/img/name.jpg/>

您可以使用此代码

<?php $image = $this->Html->image('../../site/webroot/img/folder_name/'.$content->image_path.''); ?>



你想说image.jpg文件存储在你给出的路径中吗?对不起,你是什么意思?请详细说明你的问题。或者说清楚图像的
字符串路径是什么意思
如果我在资产文件夹中有一个图像,我如何获得该图像的URL?嘿,我正在尝试Html->URL,但我得到的是“在非对象上调用成员函数url()时出现“错误”。我已经包含了“public$helpers=array('Html','Form');“在我的控制器中…@JohnathanAu您使用的是什么版本的CakePHP?这是一个CakePHP帮助函数,仅在视图中可用,而在控制器中不可用。这是您出现该错误的最可能原因。您没有在控制器中指定。使用
WWW_ROOT.'path/to/file.jpg'
elsewhere添加到@Ross的答案中,
WWW\u ROOT
是服务器的本地路径。
$this->Html->url(…)
将生成一个特定于站点服务所在域的URL。哦,在构建本地路径时,最好使用
DS
常量,而不是使用前/后斜杠,因为它依赖于平台。使用
DS
意味着您可以在任何环境中部署。对我有用。I值得注意的是,有些全局变量在2.4版之前已经被弃用了:注意:这个解决方案不是插件安全的。在CakePHP 1.2.2中也可以使用!谢谢你的提示!注意CakePHP:弃用了因为版本2.4:这个常量被弃用了,你应该改用Router::fullbaseUrl()。除了我必须使用:
'pathPrefix'=>Configure::read之外,其他都可以使用('App.imageBaseUrl')
因为imageBaseUrl是在Cake 3.0 alos中的core.phpWorks中定义的。这是Cakephp的最佳和最可靠的答案。应该这样标记。问题是拥有完整的url,而不是是否有必要。例如,如果您需要电子邮件中的图像,这不起作用。这就是为什么选择“fullbase”“存在。
// Outputs /js/app.js
$this->Url->script('my.js');

// Outputs /css/app.css
$this->Url->css('my.css');
$imgUrl = Cake\Routing\Router('/', true) . 'img/' . $imageName;
$this->Url->image('image.jpg', ['fullBase'=>true]);
<img src="<?php echo Router::url('/', true); ?>/img/name.jpg" />
<?php $image = $this->Html->image('../../site/webroot/img/folder_name/'.$content->image_path.''); ?>