how to upload file from app using cloudinary - php

I have app made using app inventor.
From there users selects image from phone.
The app sends the file via POSTFILE method to a PHP file.
The PHP file normally gets the file contents using:
$data = php_compat_file_get_contents('php://input')
But my host is on GoDaddy so I cant upload using this method.
So I want to use cloudinary but I get it to work. Is is due to same GoDaddy shared server restrictions?
Heres the cloudinary Upload code in the PHP file:
\Cloudinary::config(array(
"cloud_name" => "rafsystems",
"api_key" => "94993346XXXXXX",
"api_secret" => "bIgBADFROG-aU1GFLfHEzeQjWs"
));
$result = \Cloudinary\Uploader::upload("php://input", array("public_id" => $file1));
So what options do I have. I need to sort this out asap for myself and a client
Thanks

Please note that the api_secret must not be revealed to the public. Please generate a new pair of api_key and api_secret from your account's settings page.
We warmly recommend to upload the photo directly to Cloudinary from the browser (client-side upload), either signed or unsigned. Then, send the info, as returned by the upload response, to your PHP server?

Related

Copying files from Google Drive Server to Own server directly

I'm trying to download files from a Google Drive link from Google server to my web server, to avoid the 100 max size in PHP POST.
<?php
$link = $_POST["linkToUpload"];
$upload = file_put_contents("uploads/test".rand().".txt", fopen($link, 'r'));
header("Location: index.php");
?>
Inserting a normal link like http://example.com/text.txt it works fine. The problem comes linking google drive https://drive.google.com/uc?authuser=0&id=000&export=download. This is a direct link from Google Drive, but it doesn't work. So I tried to insert the link that I obtained downloading the file locally https://doc-08-bo-docs.googleusercontent.com/docs/securesc/000/000/000/000/000/000?e=download and it's still not working. Do you think Google is trying to avoid the server-to-server copies? Or there is another method to do it?
If you want to fetch files with your own application you should use the API (Application Programming Interface) to get these.
Have a look at the file download documentation for Google Drive
Example download snippet in PHP:
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$response = $driveService->files->get($fileId, array(
'alt' => 'media'));
$content = $response->getBody()->getContents();

how to return a 204 response code in firebase hosting (node.js)

Need some help. I am trying to access a file on firebase hosting with an android application, the application is downloading a .txt file and for verification reasons, before starting the download, it should check if the server is reachable, it needs a 204 response code (no content).
I can do it in PHP like so: (https://www.yourserver.com/return204.php):
<? php http_response_code(204); ?>
I found this link, but its looks very complicated: Firebase HTTP Functions, compared to the PHP solution.
How can i do it in Firebase with Firebase Cloudfunctions or maybe simpler. Thanks
Use this...
res.status(204).end();
Here is the full function...
exports.MyFunction = functions.https.onRequest((req, res) => {
res.status(204).end();
});

Transfer File on different server

I have a plan and there is a must :
server 1 = web server, to save source like php and etc
server 2 = to save file/document that uploaded by client
client access web server in Server 1 and then upload or download document/image but i want the document or image saved in server 2.
so web source and file/image is separate.
but i don't know what is the ideal concept from transfer file from server 1 to server 2. i try using FTP and Curl.
and please consider how to client Download file from server 2.
what is the ideal concept to transfer file like upload or download for scenario above ?
You can accomplish this with PHP FTP.
Make sure you have correct permissions to write files on the servers.
Download File using cURL :
set_time_limit(0); // unlimited max execution time for big files
$options = array(
CURLOPT_FILE => '/path/download/file/server1.img',
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => 'http://server2.com/path/download/file/server2.img',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
For this you can use any CDN hosting providers like Rackspace or any other.Actually I have used Rackspace were we uploaded files, kept storage separate from the code.They provide you API to upload,delete,retrieve files even you can create directories. They provide you API key and secret key which is used to make API call.While implementing this make common functions which can be used throughout the project to do this operations.

How to include Google App Engine for PHP in my scripts / autoloading?

I have a website on an Ubuntu webserver (not an app and not hosted at App Engine) and I want to use google cloud storage for the upload/download of large files. I am trying to upload a file directly to the Google Cloud Storage which isn't working (maybe because I made some basic errors).
I have installed the Google Cloud SDK and downloaded and unzipped Google App Engine. If I now include CloudStorageTools.php I get an the error:
Class 'google\appengine\CreateUploadURLRequest' not found"
My script looks like this:
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'test' ];
$upload_url = CloudStorageTools::createUploadUrl( '/test.php' , $options );
If you want to use the functionality of Google App Engine (gae), you will need to host on gae, which will likely have a larger impact on your app architecture (it uses a custom google compiled php version with limited libraries and no local file handling, so all that functionality needs to be into blob store or gcs - Google Cloud Storage).
With a PHP app running on ubuntu, your best bet is to use the google-api-php-client to connect to the storage JSON api.
Unfortunately the documentation is not very good for php. You can check my answer in How to rename or move a file in Google Cloud Storage (PHP API) to see how to GET / COPY / DELETE an object.
To upload I would suggest to retrieve a pre signed Upload URL like so:
//get google client and auth token for request
$gc = \Google::getClient();
if($gc->isAccessTokenExpired())
$gc->getAuth()->refreshTokenWithAssertion();
$googleAccessToken = json_decode($gc->getAccessToken(), true)['access_token'];
//compose url and headers for upload url request
$initUploadURL = "https://www.googleapis.com/upload/storage/v1/b/"
.$bucket."/o?uploadType=resumable&name="
.urlencode($file_dest);
//Compose headers
$initUploadHeaders = [
"Authorization" =>"Bearer ".$googleAccessToken,
"X-Upload-Content-Type" => $mimetype,
"X-Upload-Content-Length" => $filesize,
"Content-Length" => 0,
"Origin" => env('APP_ADDRESS')
];
//send request to retrieve upload url
$req = $gc->getIo()->makeRequest(new \Google_Http_Request($initUploadURL, 'POST', $initUploadHeaders));
// pre signed upload url that allows client side upload
$presigned_upload_URL = $req->getResponseHeader('location');
With that URL sent to your client side, you can use it to PUT the file directly onto your bucket with an upload script that generates an appropriate PUT request. Here an example in AngularJS with ng-file-upload:
file.upload = Upload.http({
url: uploadurl.url,
skipAuthorization: true,
method: 'PUT',
filename: file.name,
headers: {
"Content-Type": file.type !== '' ? file.type : 'application/octet-stream'
},
data: file
});
Good luck - gcs is a tough one if you don't want to go google all the way with app engine!
The Google API PHP Client allows you to connect to any Google API, including the Cloud Storage API. Here's an example, and here's a getting-started guide.

How to upload files directly to S3 using PHP and with progress bar

There are some similar questions but none have a good answers in how to upload files directly to S3 using PHP with progress bar. Is it even possible adding a progress bar without using Flash?
NOTE: I am referring to uploading from client browser directly to S3.
I've done this in our project. You can't upload directly to S3 using AJAX because of standard cross domain security policies; instead, you need to use either a regular form POST or Flash. You'll need to send the security policy and signature in a relatively complex process, as explained in the S3 docs.
YES, it is possible to do this in PHP SDK v3.
$client = new S3Client(/* config */);
$result = $client->putObject([
'Bucket' => 'bucket-name',
'Key' => 'bucket-name/file.ext',
'SourceFile' => 'local-file.ext',
'ContentType' => 'application/pdf',
'#http' => [
'progress' => function ($downloadTotalSize, $downloadSizeSoFar, $uploadTotalSize, $uploadSizeSoFar) {
// handle your progress bar percentage
printf(
"%s of %s downloaded, %s of %s uploaded.\n",
$downloadSizeSoFar,
$downloadTotalSize,
$uploadSizeSoFar,
$uploadTotalSize
);
}
]
]);
This is explained in the AWS docs - S3 Config section. It works by exposing GuzzleHttp's progress property-callable, as explained in this SO answer.
Technically speaking, with PHP you cannot go from client --> S3. Your solution, if you want to use PHP would either have to be designed as follows:
Client -> Web Server (PHP) -> Amazon S3
Client with PHP server embedded -> Amazon S3
The AWS PHP SDK: http://aws.amazon.com/sdkforphp/ is very well written and contains a specific example on how to send a file from a Client --> Server --> S3
With respects to the progress bar, there are many options available. A quick search of stackoverflow.com shows a question answered identical to this one:
Upload File Directly to S3 with Progress Bar
'pass through' php upload to amazon's s3?
It is possible to directly upload, but progress bar is impossible:
http://undesigned.org.za/2007/10/22/amazon-s3-php-class/
see example_form in the downloads,
direct upload from browser to S3

Categories