How can i Download file to local directly from Dropbox API - php

I m using https://github.com/kunalvarma05/dropbox-php-sdk this library in my laravel project .From this code this "example.txt" file downloaded in my project directory. But i want to download this "example.txt" file with my browser into browser download folder.
$file = $dropbox->download("/example.txt");
$contents = $file->getContents();
$metadata = $file->getMetadata();
file_put_contents(__DIR__ . "/example.txt", $contents);

A little use with Google and found it for you.
$file = $dropbox->download("/my-logo.png");
//File Contents
$contents = $file->getContents();
//Save file contents to disk
file_put_contents(__DIR__ . "/logo.png", $contents);
//Downloaded File Metadata
$metadata = $file->getMetadata();
//Name
$metadata->getName();
link to examples and github:
https://github.com/kunalvarma05/dropbox-php-sdk/wiki/Upload-and-Download-Files

Related

how to download users twitter images from API with php

i am trying to download twitter users images that sign up in my website such that i can save them in my file to insert into my database later on for use in my website.
this is my TWITTER API:
$user_pic = $user_info->profile_image_url_https;
i tried this and it works but i want it to download the image to my base file which is profilePictures/:
$twitterimage = file_get_contents($user_pic);
$fp = fopen('path_to_filename.png', 'w');
fwrite($fp, $twitterimage);
fclose($fp);
now it is being downloading but how can i download the twitter images and save them in my base file "profilePictures/"?
i found the solution, all i had to do is this
$user_pic= str_replace('_normal','',$user_pic);
$Imgurl = $user_pic;
$target_path = "profilePictures/";
$filename = basename($Imgurl);
$saveLoc = $target_path . $filename;
file_put_contents($saveLoc, file_get_contents($Imgurl));
and then save the $saveLoc variable into my database

Store blob as a file in S3 with Laravel

I am using a file upload plugin in Vue to upload some image and pdf files through an API and it has an option to create a blob field when uploading the files.
The request sent to Laravel is as follows. I can access the blob from the browser by copying and pasting the URL on the browser.
On the Server side code, I am trying to save the file with the following code but the saved file is some corrupted 64byte file rather than the actual image. How would we store the blob as a normal file in the filesystem?
if ($request->has('files')) {
$files = $request->get('files');
$urls = [];
foreach ($files as $file) {
$filename = 'files/' . $file['name'];
// Upload File to s3
Storage::disk('s3')->put($filename, $file['blob']);
Storage::disk('s3')->setVisibility($filename, 'public');
$url = Storage::disk('s3')->url($filename);
$urls[] = $url;
}
return response()->json(['urls' => $urls]);
}
Try by replacing this:
Storage::disk('s3')->put($filename, $file['blob']);
with this:
Storage::disk('s3')->put($filename, base64_decode($file['blob']));

How to read a PDF file from web and store it in PHP?

I have PDF files on some server
$path = 'http://someserver/somepath/someFile.pdf'
I would like to save these files in my Server
$file_name = 'someFile.pdf';
$file_handler = fopen('/' . DS . $file_name, 'w');
$PDF_CONTENTS = file_get_contents($path);
file_put_contents($file_handler, $PDF_CONTENTS);
fclose($file_handler);
It is creating someFile.pdf but no contents.
No need of using fopen.
Try
$storeLocation = '/pdf/mypdf.pdf';
$PDF_CONTENTS = file_get_contents($path);
file_put_contents($storeLocation, $PDF_CONTENTS);

Recovery Symfony2 session

I am a beginner in PHP and am working on a web application developed in Symfony 2, in this application I built a flash module that records the sound of the microphone and save the mp3 file to the connected account folder.
(each user must be connected for it to make recording)
here is my code which is used to save the mp3 file to a folder (recording / user1)
<?php
$songname = $_GET["name"];
$filename = $songname. " - " . date("dmY"). ".mp3";
$file = "recording/" . $filename;
$songname $_GET["name"];
$putdata = fopen("php://input", "r");
$fp = fopen($file, "w");
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
fclose($fp);
fclose($putdata);
echo $filename;
}
?>
My question: If possible, please retrieve the session or the name and user name connected and send the mp3 file directly to each user's folder (folders are created in advance in the recording folder).
thank you

Download a thubnails of an image using Dropbox API for php

I made a script where I download the images from a dropbox folder to my computer using PHP.
What I try to do now is to download thumbnail of the images instead of the whole image.
For this I use the: GetThumbNail method from the Dropbox API.
Here is part of the code:
// download the files
$f = fopen($img_name, "w+b");
$fileMetadata = $dbxClient->getThumbnail($path, 'jpeg','xl');
fclose($f);
When I run this the images I get are 0 size and they have no content. Any ideas what I am missing?
Thanks
D.
EDITED
$f = fopen($img_name, 'w+b');
$thumbnailData = $dbxClient->getThumbnail($path, 'jpeg', 'xl');
fwrite($f, $thumbnailData);
fclose($f);
You're opening and closing $f without ever writing anything into it.
getThumbnail returns an array with two elements: the metadata for the file and the thumbnail data.
So I think you'll want something like this:
$f = fopen($img_name, 'w+b');
list($fileMetadata, $thumbnailData) = $dbxClient->getThumbnail($path, 'jpeg', 'xl');
fwrite($f, $thumbnailData);
fclose($f);

Categories