I have a form that allows a user to fill in several aspects and then choose a file to upload.
When the form is submitted, I want write some code that saves the file to a dropbox account and gets access to a direct download link and places this in a database I am hosting.
If anyone has done this, is there a specific section of the API to look at? Or any examples?
I can't seem to find this in the API.
Thanks.
From what I see in the API it is possible to do this. You need to download the Dropbox Core API. Inside the zip file, you will find an example folder with example code for authentication, upload, download, direct-link and so on. Just see the direct-link.php and change it to your needs. Here is a tested working example of uploading a file and generating a direct link for download:
<?php
require_once "dropbox-php-sdk-1.1.2/lib/Dropbox/autoload.php";
use \Dropbox as dbx;
$dropbox_config = array(
'key' => 'your_key',
'secret' => 'your_secret'
);
$appInfo = dbx\AppInfo::loadFromJson($dropbox_config);
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
$authorizeUrl = $webAuth->start();
echo "1. Go to: " . $authorizeUrl . "<br>";
echo "2. Click \"Allow\" (you might have to log in first).<br>";
echo "3. Copy the authorization code and insert it into $authCode.<br>";
$authCode = trim('DjsR-iGv4PAAAAAAAAAAAbn9snrWyk9Sqrr2vsdAOm0');
list($accessToken, $dropboxUserId) = $webAuth->finish($authCode);
echo "Access Token: " . $accessToken . "<br>";
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
// Uploading the file
$f = fopen("working-draft.txt", "rb");
$result = $dbxClient->uploadFile("/working-draft.txt", dbx\WriteMode::add(), $f);
fclose($f);
print_r($result);
// Get file info
$file = $dbxClient->getMetadata('/working-draft.txt');
// sending the direct link:
$dropboxPath = $file['path'];
$pathError = dbx\Path::findError($dropboxPath);
if ($pathError !== null) {
fwrite(STDERR, "Invalid <dropbox-path>: $pathError\n");
die;
}
// The $link is an array!
$link = $dbxClient->createTemporaryDirectLink($dropboxPath);
// adding ?dl=1 to the link will force the file to be downloaded by the client.
$dw_link = $link[0]."?dl=1";
echo "Download link: ".$dw_link."<br>";
?>
I made this really fast just to get it working. Eventually you may need to tweak it a bit so it will suite your needs.
There is section in the Core API manual, see this link.
So you can use the upload part like this:
$f = file_get_contents('data.txt');
$result = $dbxClient->uploadFile("/data.txt", dbx\WriteMode::add(), $f);
echo 'file uploaded';
Related
I'm trying to get a video from my google drive account and publish it on my website.
The idea is to authorize the access to the file using a service account, so the video will be "public" accessible without the user using his google credentials.
Right now for the images I download them and the show it from my server, but due to storage space I would prefer not doing the same for videos
Here's my code:
$client = getGoogleClient(); // Specify the CLIENT_ID of the app that accesses the backend
$service = new Google_Service_Drive($client);
switch ($type) {
case 1: //video
$startPos=strrpos($url['URL'], "file/d")+7;
if($startPos>7)
{
$endPos=strrpos($url['URL'],"/");
$url=substr($url['URL'],$startPos,$endPos-$startPos); //its the file id
}
// Get files from our request
$file = $service->files->get($url,array("fields"=>"webContentLink"));
$customData=$file->webContentLink;
$customclass="hasVideo";
break;
case 3: //img
if(is_null($img))
{
//we have to donwload the file and store it temporaly
//find img id
$startPos=strrpos($url['URL'], "file/d")+7;
if($startPos>7)
{
$endPos=strrpos($url['URL'],"/");
$url=substr($url['URL'],$startPos,$endPos-$startPos);
$content = $service->files->get($url, array("alt" => "media"));
// Open file handle for output.
$filePath="./cachedFiles/".uniqid().".jpg";
$outHandle = fopen($filePath, "w+");
// Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.
while (!$content->getBody()->eof())
fwrite($outHandle, $content->getBody()->read(1024));
// Close output file handle.
fclose($outHandle);
$connection->runQuery("UPDATE File_Elemento SET cachedFile='".$filePath."', lastCached='".date('Y-m-d H:m:s')."' WHERE ID=".$ID);
}
else
$type=0;
}
else
$filePath=$img;
require_once('./ImageCache/ImageCache.php');
$imagecache = new ImageCache\ImageCache();
$imagecache->cached_image_directory = dirname(__FILE__) . '/cachedImg';
$filePath = $imagecache->cache( $filePath );
break;
default:
break;
}
echo '<a onclick="showDetail(this,\''.$customData.'\')" class="grid-item '.($subject ? $subject : "Generico").' '.($customclass!="" ? $customclass : "").'"><div class="card newsCard">'.($type==3 ? '<img class="lazy-load imgPrev" data-src="'.$filePath.'">' : "").'<h3>'.$school.'</h3><h1>'.$name.'</h1>';
echo '<div class="prev">'.$subject.'</div><span class="goin mainColor">Visualizza</span></div></a>';
right now I tried to get the webContentLink and then put the url I get as source for a video tag, but I get a 403 error, so still I didn't authorize the access using the service account
Any help would be appreciated
Embedding the webContentLink to your website won't make this publicly available. The webContentLink is as restricted as the file itself: it can only be accessed by users with which the file has been shared.
So you should do one of these:
Make this video public (via Permissions: create, or through the UI itself) (role: reader and type: anyone).
Download it serve it from your server, as with your images.
Related:
Generating a downloadable link for a private file uploaded in the Google drive
I'm trying to follow this link: https://learn.microsoft.com/en-us/rest/api/storageservices/copy-file
with examples from this repo: https://github.com/Azure/azure-storage-php/blob/master/samples/FileSamples.php#L235
The file is indeed copied to the azure server but the content aren't readable, to say the least, it takes a size but it's empty. This is only a text file as well, and what I plan to achieve after fixing this is to copy excel files generated via PHP to an azure file storage server.
Also, we are using file.core not blob.core
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\File\FileRestProxy;
use MicrosoftAzure\Storage\Common\Models\Range;
$accountName = "test";
$accountKey = "test";
$shareName = 'test';
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$accountName;AccountKey=$accountKey";
$fileClient = FileRestProxy::createFileService($connectionString);
$dstfileName = 'demo-4.txt';
$srcfileName = 'demo-4.txt';
$sourcePath = sprintf(
'%s%s/%s',
(string)$fileClient->getPsrPrimaryUri(),
$shareName,
$srcfileName
);
try {
// Create destination file.
$fileClient->createFile($shareName, $dstfileName, 1024);
// Copy file.
return $fileClient->copyFile($shareName, $dstfileName, $sourcePath);
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . PHP_EOL;
}
Update using file_get_contents
$srcfileName = 'demo-4.txt';
$content = file_get_contents('demo-4.txt');
$range = new Range(0, filesize('demo-4.txt') - 1);
$sourcePath = sprintf(
'%s%s/%s',
(string)$fileClient->getPsrPrimaryUri(),
$shareName,
$srcfileName
);
try {
// Create source file.
$fileClient->createFile($shareName, $srcfileName, 1024);
$fileClient->putFileRange($shareName, $srcfileName, $content, $range);
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . PHP_EOL;
}
This is able to create the file with the content from the source file, but the problem is that the range is incorrect since I don't know how to correctly get that value.
The created file is presented by the image attached, it has multiple nulls in it because I'm guessing my range exceeds the actual length of the source file contents.
createFile method simply creates an empty file of size specified in the method call. It essentially maps to Create File REST API operation.
You should use createFileFromContent convenience method to create a file with content. It basically first creates an empty file and then writes the contents to that file.
Other option would be to call putFileRange method to write the contents to the file after you have created it using createFile method.
I want to run a single webpage to display files, which are stored in an Azure File Storage. It must be Azure File storage because these files came from a Docker container, which is mounted, to that file storage.
I have a Azure Container Instance witch stores PDF files in an Azure File Storage. Now I run a WebApp (PHP) that shall read all the PDFs.
I’ve installed the Microsoft Azure Storage File PHP SDK but have no clue how to use it. Even the sample.php did not help me coming forward. It would be very helpful if someone could help me a bit a simple sniped.
I see you want to directly display a PDF file from Azure File Storage in a web page. Generally, the best practice is to generate the url with sas token of a file in Azure File Storage.
So I followed the GitHub repo Azure/azure-storage-php to install Azure File Storage SDK for PHP in my sample project, here is my sample code and its dependencies.
The file structure of my sample project is as the figure below.
The content of my composer.json file is as below.
{
"require": {
"microsoft/azure-storage-file": "*"
}
}
My sample PHP file demo.php is as below, that's inspired by the function generateFileDownloadLinkWithSAS of the offical sample azure-storage-php/samples/FileSamples.php.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\File\FileSharedAccessSignatureHelper;
$accountName = "<your account name>";
$accountKey = "<your account key>";
$shareName = "<your share name>";
$fileName = "<your pdf file name>";
$now = date(DATE_ISO8601);
$date = date_create($now);
date_add($date, date_interval_create_from_date_string("1 hour"));
$expiry = str_replace("+0000", "Z", date_format($date, DATE_ISO8601));
$helper = new FileSharedAccessSignatureHelper($accountName, $accountKey);
$sas = $helper->generateFileServiceSharedAccessSignatureToken(
Resources::RESOURCE_TYPE_FILE,
"$shareName/$fileName",
'r', // Read
$expiry // A valid ISO 8601 format expiry time, such as '2020-01-01T08:30:00Z'
);
$fileUrlWithSAS = "https://$accountName.file.core.windows.net/$shareName/$fileName?$sas";
echo "<h1>Demo to display PDF from Azure File Storage</h1>";
echo "<iframe src='$fileUrlWithSAS' width='800' height='500' allowfullscreen webkitallowfullscreen></iframe>";
?>
The result of my web page is as the figures below in Chrome and Firefox.
The result in Chrome:
The result in Firefox:
Update: The code to list files in a file share, as below.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\File\FileRestProxy;
$accountName = "<your account name>";
$accountKey = "<your account key>";
$shareName = "<your share name>";
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$accountName;AccountKey=$accountKey";
$fileClient = FileRestProxy::createFileService($connectionString);
$list = $fileClient->listDirectoriesAndFiles($shareName);
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
foreach($list->getFiles() as &$file) {
$fileName = $file->getName();
if(endsWith($fileName, ".pdf")) {
echo $fileName."\n";
}
};
?>
i am facing issue regarding to upload file in dropbox via php.Actually i am using uploadFile function.This function supposed to return the meta data of newly uploaded file after uploading file.But in my case it returns nothing and browser throws 504 gateway timing error.But file uploads successfully.I am using this code.
echo 'Sending file to DropBox';
$appInfo = dbx\AppInfo::loadFromJsonFile(ABSPATH."wp-content/plugins/wp-cloud-safe/lib/app-info.json");
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
$filename=ABSPATH.'clients.webkitmedia.com_dfd_16th-February-2016-10:43.zip';
$dbxClient = new dbx\Client($this->dropboxGeneratedAccessToken, "PHP-Example/1.0");
//print_r($dbxClient);
$f = fopen($filename,'rb');
//$filesize=filesize($filename);
$reult= $dbxClient->uploadFile('/test/testing.zip', dbx\WriteMode::add(),$f);
print_r($reult);
die();
fclose($f);
Please suggest any idea?
I replace these lines in upper code and issue get resolved.
$f = file_get_contents($filename);
$reult= $dbxClient->uploadFileFromString('/test/testing.zip', dbx\WriteMode::add(),$f);
I am trying to convert words to speech ..
Untill now I have tried this:
<?php
$text = "Hello this is a test for voice api of google";
// Name of the MP3 file generated using the MD5 hash
$file = md5($text);
// Save the MP3 file in this folder with the .mp3 extension
$file = "audio/" . $file .".mp3";
if($file) {
echo "created";
} else {
echo "not created";
}
// If the MP3 file exists, do not create a new request
if (!file_exists($file)) {
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?q=' . $text);
echo "hello";
file_put_contents($file, $mp3);
} else {
echo "hii";
}
?>
In my html file :
<audio controls="controls" autoplay="autoplay">
<source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
I am getting created hello and an audio player in output. But no file is played and neither it is created in the folder?
There is a problem with the url you try to access. It is broken ! You should have tried first.
The new URL, that I found on the FF console is :
http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&total=1&idx=0&textlen=5&prev=input
For the single word Hello. And you see that you have to specify the language, and the length of your text in textlen, even though it did work for all the sentences I tried without changing this var.
Another problem is that you have to urlencode() your text, or you will have a bug with accents and punctuation.
So the line to download the MP3 becomes :
// Language of the sentence
$lang = "fr";
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
So the complete code looks like :
<?php
$text = "Bonjour, comment allez vous ?";
// Yes French is a beautiful language.
$lang = "fr";
// MP3 filename generated using MD5 hash
// Added things to prevent bug if you want same sentence in two different languages
$file = md5($lang."?".urlencode($text));
// Save MP3 file in folder with .mp3 extension
$file = "audio/" . $file . ".mp3";
// Check folder exists, if not create it, else verify CHMOD
if (!is_dir("audio/"))
mkdir("audio/");
else
if (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
chmod("audio/", 0777);
// If MP3 file exists do not create new request
if (!file_exists($file))
{
// Download content
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
file_put_contents($file, $mp3);
}
?>
I found it:
https://translate.google.com.vn/translate_tts?ie=UTF-8&client=tw-ob&q=ANYTHING_TEXT&tl=YOUR_LANGUAGE_CODE
Important: client=tw-ob
YOUR_LANGUAGE_CODE can be en,us,uk,vi etc.
An improved version:
// ~~~ Credits to kube ~~~
$text = "Hello this is a test for voice api of google";
$text = urlencode($text);
$lang = urldecode("en");
$file = "audio/" . md5($text) .".mp3";
if (!file_exists($file) || filesize($file) == 0) {
$mp3 = file_get_contents('http://translate.google.com/translate_tts?ie=UTF-8&q='.$text.'&tl='.$lang.'&total=2&idx=0&textlen='.strlen($text).'&prev=input');
if(file_put_contents($file, $mp3)){
echo "Saved<br>";
}else{
echo "Wasn't able to save it !<br>";
}
} else {
echo "Already exist<br>";
}
You cannot use this service for free.
Is there any free quota?
No, the Google Translate API is only available as a paid service. Please see Pricing and Support for more details. However we do offer the Google Website Translator gadget, which will translate your website without charge.
Check translate API FAQ
More info about this unofficial way of use you can find on Techcrunch
You can also use the simple code below. Just echo the code to get the result. In this code, there is no need to save a file or getting permission problems.
echo "<iframe hidden src='http://translate.google.com/translate_tts?ie=UTF-8&q=Welcome%20back%20".$jvm['firstname']."&tl=en&total=2&idx=0&textlen=5&prev=input'></iframe>";
Your file is not creating because you forgot to create it , use below code for creating the file.
$file = "audio/".$file.".mp3";
$ourFileHandle = fopen($file, 'w') or die("can't open file");