move_uploaded_file returns TRUE but no file in GCS

2020-05-03 09:54发布

Very new to this and have been trying to get it to work. I have a bucket called app. I have followed the instructions to give my app permissions to the GCS (https://developers.google.com/appengine/docs/python/googlestorage/index#Prerequisites). It seems to be setup correctly.

I followed How do I upload images to the Google Cloud Storage from PHP form? which has worked for others. I haven't really diverted from it.

I get move_uploaded_file returning TRUE, but no file in GCS.

The output is "MoveResult did succeed". Anyone see anything I am doing wrong. Am I missing some setup code or something?

Here is the code:

if(!is_uploaded_file($_FILES['file']['tmp_name'])) { 
     $profile_pic_path='default';
}
else { //file exists
    $gs_name = $_FILES["file"]["tmp_name"]; 

    $fileType = $_FILES["file"]["type"]; 
    $fileSize = $_FILES["file"]["size"]; 
    $fileErrorMsg = $_FILES["file"]["error"]; 
    $fileExt = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    // change name if you want
    $fileName = 'profile_pic.jpg';

    // put to cloud storage
    $image = file_get_contents($gs_name);
    $options = [ "gs" => [ "Content-Type" => "image/jpeg"]];
    $ctx = stream_context_create($options);
    //file_put_contents("gs://app/".$fileName, $gs_name, 0, $ctx);
    if(!move_uploaded_file($gs_name, 'gs://app/'.$fileName)){
        echo json_encode(array('success' => 0,'error_message' => "MoveResult did not succeed"));
        exit;
    }
    else{
        //Get image key in or path or what not. THIS IS THE NEXT STEP
        //$profile_pic_path=<something>;
        echo json_encode(array('success' => 1,'info_message' => "MoveResult did succeed"));
        exit;
    }

}

0条回答
登录 后发表回答