Search code examples
phpgoogle-app-enginegoogle-cloud-storagephp-8.2

After upgrading to PHP8.2 on GAE, upload no longer work to Cloud Storage


My web app has been working fine for many years on PHP7 on Google App Engine and Cloud Storage. This week I'm updating to PHP8 and have fixed the usual issues with stricter types, etc. The only issue left is that my file upload function does not work with PHP8.

My code works locally with Apache/PHP8 and works if I revert back to a PHP7 version on GAE. I don't see any errors (or I don't know how to find them) in the App Engine logs. PHP does not stop executing, but just doesn't save the uploads to Cloud Storage. Here is my code:

session_start();

// Use the composer autoloader to load dependencies.
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
try {
    $storage = new StorageClient();
}catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}


// Cloud Storage Settings
$_SESSION['project_id'] = "xxapp";
$_SESSION['bucket_name'] = "xxapp.appspot.com";
$_SESSION['devStorage'] = "https://storage.googleapis.com/xxapp.appspot.com/uploads/";

$success = upload_to_GCP_Storage($storage,
    $_FILES["cert_".$_POST['updateVERI_ID']]["name"],
    $_FILES["cert_".$_POST['updateVERI_ID']]["tmp_name"],
    ''
);


function upload_to_GCP_Storage($storage, $objectName, $source, $option) {

    $type = strtolower(substr(strrchr($objectName,"."),1));
    if($type == 'jpeg') $type = 'jpg';

    try {
        $dir = sys_get_temp_dir();

        switch(strtolower($type)) {
            case 'jpg': break;
            case 'png': break;
            case 'pdf': break;               
            case 'doc': break;  
            case 'docx': break;  
            case 'xls': break;  
            case 'xlsx': break;  
            default : return "Unsupported file type!";
        }
        $newFileName = md5(time().$objectName).".".$type;

        // Move uploaded file to temporary directory
        if (!move_uploaded_file($source, dir.'/'.$newFileName)) {
            handleError("Failed to move uploaded file");
        }

        $bucket = $storage->bucket($_SESSION["bucket_name"]);

        $f = fopen($dir.'/'.$newFileName, 'r');
            $object = $bucket->upload($f, [
                'name' => 'uploads/'.$newFileName
            ]);
        $object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']); // makes public readable
            
        fclose($f); // this removes the tmp file

        return $newFileName;

    } catch (Exception $e) {
        handleError($e->getMessage());
    }

}

Has something changed with how my code should be since PHP7? Is the composer storage library compatible with PHP8? How do I check? Any other suggestions are welcome ... time is running out since PHP7 won't be supported on GAE after Jan 31st.

Thanks, -Mark


Solution

  • Thank you @John Hanley for your suggestion to check composer dependencies.

    After several days and adding in error logging, I also found a PHP8 change that broke part of my upload procedure. fclose($f) will create a fatal error if $f no longer exists.