gpt4 book ai didi

PHP file_put_contents : failed to open stream: Protocol error

转载 作者:行者123 更新时间:2023-12-04 22:09:42 26 4
gpt4 key购买 nike

使用 PHP 副本或 file_put_contents 保存图像有时会返回以下错误:无法打开流:协议(protocol)错误。

这是我尝试过的工作流程:

// A. using file_get_contents and file_put_contents
$image = file_get_contents('https://maps.googleapis.com/maps/api/place/photo?key=' . $apiKey . '&photoreference=' . $mainPhoto['photo_reference'] . '&maxheight=300'

file_put_contents('googleimage/' . $mainPhoto['photo_reference'] . '.jpg', $image) // here is where the exception is thrown

// B. using copy
copy('https://maps.googleapis.com/maps/api/place/photo?key=' . $apiKey . '&photoreference=' . $mainPhoto['photo_reference'] . '&maxheight=300', 'googleimages/' . $mainPhoto['photo_reference'] . '.jpg');

这只有时会发生,仅适用于某些图片。

最佳答案

当建议的文件名不符合文件名规范时,可能会发生协议(protocol)错误。例如,它包含保留字符或太长。

此行发生错误

file_put_contents('googleimage/' . $mainPhoto['photo_reference'] . '.jpg', $image)

在评论中,您举例说明了 $mainPhoto['photo_reference'] 可以包含什么(以及什么不起作用)。
诚然,我没想到会找到保留字符并且没​​有,所以我尝试缩短它。请参阅下面的比较(我从其末尾删除了字符,直到它起作用)。首先是原始字符串,然后是缩短的字符串。
CqQBlwAAAJMqc533H5kDibo444KqEkGjgnCWRKvmadBld3Aw_wlGom502_V3FJeg76SuGnRU77oriCREzQDZ21WtOhqT76l9ysb8aj5xWeKq7jUUTkwBZ5oWyQB7joGNca6SENMTeBY_DxGt2zrxCxouVFysg1rWaFbRA0wSMk1TGyGeY_zDFdP_I1qVp_aj5QlLFNNTpMYTNjf_YXzyTosjcAS8t5ESEMJIOAxBL1iQaOP1UH9eWxwaFH7WRElugeqGGG2srPd_wOAKvtLO
CqQBlwAAAJMqc533H5kDibo444KqEkGjgnCWRKvmadBld3Aw_wlGom502_V3FJeg76SuGnRU77oriCREzQDZ21WtOhqT76l9ysb8aj5xWeKq7jUUTkwBZ5oWyQB7joGNca6SENMTeBY_DxGt2zrxCxouVFysg1rWaFbRA0wSMk1TGyGeY_zDFdP_I1qVp_aj5QlLFNNTpMYTNjf_YXzyTosjcAS8t5ESEMJIOAxBL1iQaOP1UH9eWxwaFH7WREl

(您可以尝试一下,也可以随机缩短字符串。但是,删除我以这种方式创建的文件并不容易。)

This only happens sometimes, only for some pictures.



我怀疑这取决于您收到的谷歌引用字符串。长度可能变化很大,以至于它确实有所作为。

请参阅 Microsoft 的 Naming Files, Paths, and Namespaces从 2018 年 5 月 31 日开始:

例如,保留字符(来自 Naming Conventions)
    < (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

来自维基百科的文章 Filename , 部分 Length Restrictions :

Some filesystems restrict the length of filenames.



维基百科文章 Comparison of Filesystems包含一个有用的表 Limits .

关于PHP file_put_contents : failed to open stream: Protocol error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28650418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com