php - 上传 Excel 文件 Google Drive API
问题描述
我正在使用 Google Drive API 上传 Excel 文件。我正确地完成了 API 连接。创建文件夹并正确上传excel文件。我可以在我的谷歌驱动器中看到它们。但是,excel文件是空的。我在这里缺少什么?
我的代码如下:
$path = "uploads/school_info.xlsx"; //file that I am uploading, inside a folder of my website.
$folder_id = create_folder("School_".$_POST['submit_uid'], "****folder-id****");
$file_name = "School 1";
$success = insert_file_to_drive( $path, $file_name, $folder_id);
// This will insert file into drive and returns boolean values.
function insert_file_to_drive( $file_path, $file_name, $parent_file_id = null ) {
$service = new Google_Service_Drive( $GLOBALS['client'] );
$file = new Google_Service_Drive_DriveFile();
$file->setName( $file_name );
if( !empty( $parent_file_id ) ){
$file->setParents( [ $parent_file_id ] );
}
$result = $service->files->create(
$file,
array(
'data' => file_get_contents($file_path),
//'mimeType' => 'application/octet-stream',
'mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', //works but still empty excel
)
);
$is_success = false;
if( isset( $result['name'] ) && !empty( $result['name'] ) ){
$is_success = true;
}
return $is_success;
}
解决方案
好的,我发现了问题所在。正如我在上面的代码中所写,我有一个相对路径
$path = "uploads/school_info.xlsx";
当我将其更改为这样的完整路径时:
$path = "https://www.example.com/uploads/school_info.xlsx";
一切都按预期工作
谢谢大家的时间
推荐阅读
- java-8 - 溪流
与双流 - rust - 如何“强制”结构实现相同的特征?
- ios - swift 4 约束动画无法正常工作
- javascript - JS 阻止 XSS 但允许 HTML URL
- r - 识别列中的模式,并将它们添加到数据框中的列
- django - 如何在 django 中重命名翻译字段
- ansible - 如何忽略ansible中的警告
- reactjs - 反应Hightlight.js“类型错误:#
不是函数” - plugins - 如何为 VMware 产品开发插件?
- android - 资源 res = mPackageManager.getResourcesForApplication("com.packagename"); 不工作 6.0 及以下