我的网络应用程序在 Google App Engine 和云存储上的 PHP7 上运行良好多年。本周我将更新到 PHP8,并修复了更严格类型等常见问题。剩下的唯一问题是我的文件上传功能不适用于 PHP8。
我的代码可以在本地使用 Apache/PHP8,并且如果我在 GAE 上恢复到 PHP7 版本也可以工作。我在 App Engine 日志中没有看到任何错误(或者我不知道如何找到它们)。 PHP 不会停止执行,但只是不会将上传内容保存到 Cloud Storage。这是我的代码:
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());
}
}
自 PHP7 以来,我的代码有什么变化吗? Composer存储库与PHP8兼容吗?我该如何检查?欢迎任何其他建议...时间不多了,因为 1 月 31 日之后 GAE 将不再支持 PHP7。
谢谢, -马克
感谢@John Hanley 提出检查作曲家依赖性的建议。
几天后添加错误日志,我还发现 PHP8 的更改破坏了我的部分上传过程。如果 $f 不再存在,fclose($f) 将产生致命错误。