| Server IP : 172.67.187.206 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/news-system/ |
Upload File : |
<?php
// 1. เรียกใช้งาน Google Client Library
require_once 'vendor/autoload.php';
// 2. รับข้อมูลจากฟอร์ม
$news_title = $_POST['news_title'];
$file_tmp = $_FILES['news_image']['tmp_name'];
$file_name = $_FILES['news_image']['name'];
// 3. ตั้งค่าการเชื่อมต่อ Google Drive API
$client = new Google_Client();
// *** ต้องใส่ đường dẫn ไปยังไฟล์ JSON ของ Service Account ที่ดาวน์โหลดมา ***
$client->setAuthConfig('credentials.json');
$client->addScope(Google_Service_Drive::DRIVE);
$service = new Google_Service_Drive($client);
// 4. เตรียมไฟล์สำหรับอัปโหลด
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $file_name,
// *** ใส่ ID ของโฟลเดอร์ใน Google Drive ที่ต้องการเก็บไฟล์ ***
'parents' => array('YOUR_FOLDER_ID')
));
$content = file_get_contents($file_tmp);
$file = $service->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => $_FILES['news_image']['type'],
'uploadType' => 'multipart',
'fields' => 'id'));
// 5. ได้รับ File ID กลับมา
$file_id = $file->id;
// *** ตั้งค่าการแชร์ไฟล์ให้ทุกคนที่มีลิงก์ดูได้ ***
$permission = new Google_Service_Drive_Permission(array(
'type' => 'anyone',
'role' => 'reader'
));
$service->permissions->create($file_id, $permission);
// 6. บันทึกข้อมูลลงฐานข้อมูล MySQL
// (โค้ดส่วนเชื่อมต่อและบันทึกข้อมูลของคุณ)
// mysql_query("INSERT INTO news (title, image_file_id) VALUES ('$news_title', '$file_id')");
echo "อัปโหลดไฟล์และบันทึกข้อมูลสำเร็จ! File ID: " . $file_id;
?>