0
votes

When uploading an image to Google Cloud Storage using PHP createUploadUrl and option Content=type image/jpeg, I get an error logged as

"Invalid options supplied: Content-Type..."

This is the code:

$options = [ 'gs_bucket_name' => 'mybucket', 'Content-Type' => 'image/jpeg' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload.php', $options);

The image is uploaded, but appears as binary/octet-stream files (GCS default). I have also tried mimeType and image/jpg. What am I doing wrong?

2

2 Answers

2
votes

Pass the Content-Type as part of the context to move_uploaded_file(), not as part of the options to createUploadURL.

0
votes

Once you upload a file(s) don't use move_uploaded_file() like most documentation shows, because it looses the type information during the move.

Even worse it ignores the stream_context_create() if you try to set it before move and sets type to default - binary/octet-stream.

Simply use rename() instead of move_uploaded_file(). The arguments in the brackets are the same.

If it helps here is my example.

Simple one:

foreach($_FILES['userfile']['name'] as $idx => $name) 
{     
    $original = $root_path . '/' . $name;;
    rename($_FILES['userfile']['tmp_name'][$idx], $original); 
}

Here is a more complicated one that forces the type (in case you want to):

foreach($_FILES['userfile']['name'] as $idx => $name) 
{     
    $original = $root_path . '/' . $name;
    $options = ['gs' => ['Content-Type' => $_FILES['userfile']['type'][$idx]]];
    stream_context_set_default($options);
    rename($_FILES['userfile']['tmp_name'][$idx], $original); 
}