Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_put_contents: failed to open stream: Protocol error

Saving an image using PHP copy or file_put_contents returns sometimes the following error: failed to open stream: Protocol error.

This is the workflow I tried:

// 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');

This only happens sometimes, only for some pictures.

like image 335
Tudor Ravoiu Avatar asked Feb 21 '15 19:02

Tudor Ravoiu


1 Answers

The Protocol Error can occur when the proposed filename doesn't meet the specifications for filenames. For example, it includes reserved characters or is too long.

The error occurred in this line

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

and in a comment you gave an example of what $mainPhoto['photo_reference'] can contain (and what doesn't work). Admittedly, I didn't expect to find reserved characters and didn't, so I tried to shorten it. See the comparison below (I removed characters from its end until it worked). Original string first, then shortened string.

CqQBlwAAAJMqc533H5kDibo444KqEkGjgnCWRKvmadBld3Aw_wlGom502_V3FJeg76SuGnRU77oriCREzQDZ21WtOhqT76l9ysb8aj5xWeKq7jUUTkwBZ5oWyQB7joGNca6SENMTeBY_DxGt2zrxCxouVFysg1rWaFbRA0wSMk1TGyGeY_zDFdP_I1qVp_aj5QlLFNNTpMYTNjf_YXzyTosjcAS8t5ESEMJIOAxBL1iQaOP1UH9eWxwaFH7WRElugeqGGG2srPd_wOAKvtLO
CqQBlwAAAJMqc533H5kDibo444KqEkGjgnCWRKvmadBld3Aw_wlGom502_V3FJeg76SuGnRU77oriCREzQDZ21WtOhqT76l9ysb8aj5xWeKq7jUUTkwBZ5oWyQB7joGNca6SENMTeBY_DxGt2zrxCxouVFysg1rWaFbRA0wSMk1TGyGeY_zDFdP_I1qVp_aj5QlLFNNTpMYTNjf_YXzyTosjcAS8t5ESEMJIOAxBL1iQaOP1UH9eWxwaFH7WREl

(You can try it out, also randomly shortening the string. However, removing the file I created this way is not that easy.)

This only happens sometimes, only for some pictures.

I suspect that this depends on the google reference string you receive. The length may vary so much that it does make a difference.


See Microsoft's Naming Files, Paths, and Namespaces from 2018.05.31:

For example, reserved characters (from Naming Conventions)

    < (less than)
    > (greater than)
    : (colon)
    " (double quote)
    / (forward slash)
    \ (backslash)
    | (vertical bar or pipe)
    ? (question mark)
    * (asterisk)

From Wikipedia's article Filename, section Length Restrictions:

Some filesystems restrict the length of filenames.

The Wikipedia article Comparison of Filesystems contains a useful table Limits.

like image 179
Jeanne Dark Avatar answered Sep 30 '22 11:09

Jeanne Dark