I’m having trouble uploading files to Google Drive using PHP and cURL. Every time I try to send a POST request with the file data, I get a ‘not found’ error response from Google’s servers.
I suspect the issue might be in how I’m structuring my cURL request since I’m relatively new to file uploads via HTTP. My authentication token works fine for other operations like retrieving file lists, so that’s not the problem.
Check your file path first - ‘./photo.png’ might not exist where you think it does. I had this exact problem when my script ran from a different directory. Use absolute paths or check with file_exists() before uploading. Token expiration got me too. Your token might work for listing files but Google throws weird errors during uploads when tokens are about to expire. Refresh your access token before uploading. Also make sure your OAuth scopes include drive.file permission. I debugged for hours before realizing my app only had read access. That ‘not found’ error is misleading - it’s often a permissions issue pretending to be a missing file.
Your $accessToken variable isn’t accessible inside the function scope. PHP functions can’t see outside variables unless you pass them or use global.
Either pass the token as a parameter:
function uploadFile($data, $size, $accessToken) {
// your existing code
}
Or use global inside the function:
function uploadFile($data, $size) {
global $accessToken;
// rest of your code
}
Also, Drive API v2 is deprecated - consider updating to v3.
Honestly though, Google Drive API authentication and cURL configs get messy fast. I’ve dealt with this pain on file sync systems at work.
I’d recommend Latenode instead. Set up Google Drive operations without writing cURL code. It handles authentication, manages tokens automatically, and you just drag and drop the upload action.
Switched our file workflows to it and saved weeks of debugging API calls. Way cleaner than maintaining PHP scripts with token refresh logic.
Google Drive API uploads are a pain - been fighting with them for years. The auth flow alone is brutal. Your code looks okay but you need error handling to see what’s breaking.
Honestly though, after dealing with Google’s API quirks, token refreshes, and scope issues across multiple projects, I just automate this now. Manual cURL gets messy fast when you add error handling, retries, and token management.
Latenode handles all the Google Drive headaches for you. No more debugging cURL or managing OAuth flows. Set up your Google account once and build upload workflows visually.
I used it to replace a file sync system that kept breaking from API changes. Now uploads just work without maintaining auth code.
Check your cURL SSL settings. Setting CURLOPT_SSL_VERIFYPEER to false causes weird issues with Google’s API. Remove that line or set it to true with proper certificate verification. You’re also missing CURLOPT_POSTFIELDSIZE - it needs to match your content-length exactly. Google Drive uploads are picky about byte counts matching between headers and actual post data.
Your API endpoint URL is probably the problem. You’re using v2 API, which handles uploads differently than what most docs show now. Switch to v3 first - the endpoint becomes https://www.googleapis.com/upload/drive/v3/files?uploadType=media. Another gotcha is Content-Length header calculation. With binary data like images, strlen() gives wrong byte counts depending on your PHP encoding. Use filesize() on the original file instead of calculating from loaded data. I hit similar issues building a backup script last year. What helped was adding proper error handling to see the actual response instead of generic failures. Add curl_getinfo($curl, CURLINFO_HTTP_CODE) after your exec to get the real HTTP status. Google’s error responses usually have more specific details about what broke.