Time-out error uploading file using Dropbox API - php

I try to upload a file to DropBox using their API and PHP.
Thats the code:
require_once "dropbox/lib/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("app_info.json");
$csrfTokenStore = new dbx\ArrayEntryStore($_SESSION, 'dropbox-auth-csrf-token');
$webAuth = new dbx\WebAuth($appInfo, "NoteBoxApp/0.01", "http://localhost/notes", $csrfTokenStore, null);
$title=$_POST["titulo"].".txt";
$nota=$_POST["conteudo"];
$accessToken=$_SESSION["token"];
$clientIdentifier=$_SESSION["userId"];
$client= new dbx\Client($accessToken, $clientIdentifier);
$file = fopen($title, "w") or die("Unable to open file!");
fwrite($file, $nota);
$stat = fstat($file);
$size = (int) $stat['size'];
$dropboxPath="/Aplicativos/Notes01";
try{
$metadata = $client->uploadFile($dropboxPath, dbx\WriteMode::add(), $file, $size);
}
catch(Exception $e) {
echo "Exceção: ", $e->getMessage(), "\n";
}
fclose($file);
I always get the exception
Error executing HTTP request: Operation too slow. Less than 1024
bytes/sec transferred the last 10 seconds
I cant see why! I using XAMPP on localhost!...
Thanks in advance for the help!

I found the solution:
I can't upload a file open in "write" mode!
I have to close the file first then open it again in "read" mode and then it works.

Are you sure the file you are trying to upload is not empty? It seems that you aren't sending any data at all.

Related

How to increase the performance of a file upload using native sftp functions and fwrite in PHP

Hi I am using the following code to upload a huge file (500MB) to a sftp server.
<?php
$connection = ssh2_connect($this->host, $this->port, null);
$sftp = ssh2_sftp($connection);
$connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
$stream = fopen('ssh2.sftp://' . $connection_string, 'w');
$source = fopen($localFilepath, 'r');
if (!$stream) {
throw new Exception('Could not create file: ' . $connection_string);
}
while (!feof($source)) {
// Chunk size 32 MB
if (fwrite($stream, fread($source, 33554432)) === false) {
throw new Exception('Could not send data: ' . $connection_string);
}
}
fclose($source);
fclose($stream);
But the upload is very slow. The code is running on Google Cloud Run. The upload speed is around 8 MiB/s.
I also tried to use lftp via shell_exec but this lead to even more issues due to Cloud Run.
The uplink can't be the problem as I can send files via CURL post without any issues.
Anyone able to help here?
Many thanks and best,
intxcc
The issue is that even though 32MB are read and then written to the sftp stream, fwrite will chunk at a different size. I think just a few KB.
For filesystems (which is the usual case with fwrite) this is fine, but not with high latency due to fwriting to a remote server.
So the solution is to increase the chunk size of the sftp stream with
stream_set_chunk_size($stream, 1024 * 1024);
So the final working code is:
<?php
$connection = ssh2_connect($this->host, $this->port, null);
$sftp = ssh2_sftp($connection);
$connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
$stream = fopen('ssh2.sftp://' . $connection_string, 'w');
$source = fopen($localFilepath, 'r');
// Stream chunk size 1 MB
stream_set_chunk_size($stream, 1024 * 1024);
if (!$stream) {
throw new Exception('Could not create file: ' . $connection_string);
}
while (!feof($source)) {
// Chunk size 32 MB
if (fwrite($stream, fread($source, 33554432)) === false) {
throw new Exception('Could not send data: ' . $connection_string);
}
}
fclose($source);
fclose($stream);
Hope this helps the next person that is getting gray hair trying to figure that out ;)

Cannot upload wav file on an Azure blob container using PHP

I need to upload audio files in wav format to an Azure container using the Azure SDK for PHP but the content of the wav does not upload. Indeed, I only have a 0 bytes .wav file in my container so i'm not able to use it.
I have tested several codes but this is the best one I have. I am not an expert in PHP but I force him to use this language to integrate it into a CRM.
When I load a text file it also uploads empty so the problem doesn't come from the way I read the file.
Thanks a lot for your help.
<?php
require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
$connectionString = "DefaultEndpointsProtocol=https;AccountName=".getenv('ACCOUNT_NAME').";AccountKey=".getenv('ACCOUNT_KEY');
// Create blob client.
$blobClient = BlobRestProxy::createBlobService($connectionString);
$fileToUpload = "audio.wav";
if (!isset($_GET["Cleanup"])) {
$containerName = "cs-blob-input";
try {
// Getting local file so that we can upload it to Azure
$myfile = fopen($fileToUpload, "w") or die("Unable to open file!");
fclose($myfile);
# Upload file as a block blob
echo "Uploading BlockBlob: ".PHP_EOL;
$content = fopen($fileToUpload, "r");
//Upload blob
$blobClient->createBlockBlob($containerName, $fileToUpload, $content);
// List blobs.
$listBlobsOptions = new ListBlobsOptions();
echo "These are the blobs present in the container: ".PHP_EOL;
do{
$result = $blobClient->listBlobs($containerName, $listBlobsOptions);
foreach ($result->getBlobs() as $blob)
{
echo $blob->getName().PHP_EOL;
}
$listBlobsOptions->setContinuationToken($result->getContinuationToken());
} while($result->getContinuationToken());
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
catch(InvalidArgumentTypeException $e){
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/library/azure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
}
else
{
try{
// Delete container.
echo "Deleting Container".PHP_EOL;
echo $_GET["containerName"].PHP_EOL;
echo "<br />";
$blobClient->deleteContainer($_GET["containerName"]);
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
}
?>
The problem is here:
// Getting local file so that we can upload it to Azure
$myfile = fopen($fileToUpload, "w") or die("Unable to open file!");
fclose($myfile);
What is happening is, you open a file "audio.wav" for writing.
Based on official docs (See the mode parameter 'w') your file will be created if it not exists. Then, if it exists, it will be truncated to zero size. Then you close the file.
After that you do:
$content = fopen($fileToUpload, "r");
Which reads an empty file. So the uploaded content is => 0 in size

php file download sometimes not working (gzopen, gzwrite, gzclose)

<?php
set_time_limit(0);
$myfile = gzopen($constpath . '.gz', 'w');
if (!$myfile){
throw new \UnexpectedValueException('could not open datafeed.gz file');
}
$mystream = gzopen($constURL, 'r');
if (!$mystream){
throw new \UnexpectedValueException('could not open gzip remote file');
}
echo '1<br>';
while (!gzeof($mystream)){
$data = gzread($mystream, 8096);
gzwrite($myfile, $data);
}
echo '4<br>';
gzclose($mystream);
gzclose($myfile);
echo '5<br>';
echo 'down done';
//begin ungzip
$fp = fopen($constpath . '.csv', 'w');
$gz = gzopen($constpath . '.gz', 'r');
if (!$gz){
throw new \UnexpectedValueException(
'could not open gzip file'
);
}
if (!$fp){
gzclose($gz);
throw new \UnexpectedValueException(
'could not open destination file'
);
}
while (!gzeof($gz)) {
fwrite($fp, gzread($gz, 8096));
}
gzclose($gz);
fclose($fp);
echo 'ungzip done';
?>
Hi,
guys so above is my code, it intermittently allows me to download the gz file then unzip it, however it does not seem to be doing this in any sort of pattern or anything else, is there anything i need to know about how to use these functions like maybe is there a limit on the URL length (it's currently about 2.5K characters) but unfortunately not something i can change. what would people recommend on how to debug if there're bugs? or what i can do?
thanks!
EDIT: something i've noticed is that it is taking an absolute age to create a 25kB file, and previous to that it is 0kB, it then stops at 25kB
when i open the file in nano i get
^_�^H^#^#^#^#^#^#^C^#^#^#��^C^#^#^#^#^#^#^#^#^#
but when i unzip and open in windows there is nothing there?

PHP: fopen error handling

I do fetch a file with
$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);
and then the method gives it back as image. But when fopen() fails, because the file did not exists, it throws an error:
[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...
This is coming back as json, obviously.
The Question is now: How can i catch the error and prevent the method from throwing this error directly to the client?
You should first test the existence of a file by file_exists().
try
{
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( !file_exists($fileName) ) {
throw new Exception('File not found.');
}
$fp = fopen($fileName, "rb");
if ( !$fp ) {
throw new Exception('File open failed.');
}
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
} catch ( Exception $e ) {
// send error message if you can
}
or simple solution without exceptions:
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ) {
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
}
else
{
// send error message if you can
}
You can use the file_exists() function before calling fopen().
if(file_exists('uploads/Team/img/'.$team_id.'.png')
{
$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);
}
[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...
the error is clear: you've put the wrong directory, you can try what you whant but it'll not work. you can make it work with this:
take your file and put it in the same folder of your php file
(you'll be able to move it after don't worry, it's about your error)
or on a folder "higher" of your script (just not outside of your www
folder)
change the fopen to ('./$team_id.'png',"rb");
rerun your script file
don't forget this : you can't access a file that is'nt in your "www" folder
(he doesn't found your file because he give you her name: the name come from the $team_id variable)
Generically - This is probably the best way to do file-io in php (as mentioned by #Cendak here)
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ){
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
}else{
// send an error message if you can
}
But it does not work with PHP 7.3, these modifications do,
if(file_exists($filename) && ($fp = fopen($filename,"r") !== false)){
$fp = fopen($filename,"r");
$filedata = fread($fp,filesize($filename));
fclose($fp);
}else{
$filedata = "default-string";
}

cloudfiles remote file problem

I am trying to load a file to my Cloud Files Container from a remote file.
Below is the example code a Cloud Files Support person gave me:
<?php
require('cloud/cloudfiles.php');
$res = fopen("http://images.piccsy.com/cache/images/66653-d71e1b-320-469.jpg", "rb");
$temp = tmpfile();
$size = 0.0;
while (!feof($res))
{
$bytes = fread($res, 1024);
fwrite($temp, $bytes);
$size += (float) strlen($bytes);
}
fclose($res);
fseek($temp, 0);
//
$auth = new CF_Authentication('user','token ');
//Calling the Authenticate method returns a valid storage token and allows you to connect to the CloudFiles Platform.
$auth->authenticate();
$conn = new CF_Connection($auth);
$container = $conn->create_container("example");
$object = $container->create_object("example.jpg");
$object->content_type = "image/jpeg";
$object->write($temp, $size);
fclose($temp);
?>
The problem I am getting is the below error:
Fatal error: Call to a member function create_container() on a non-object in /home2/sharingi/public_html/daily_dose/remote_test.php on line 24
Not sure exactly what I am not noticing here.
It seems $conn did not successfully instantiate an object. That is the problem, but the solution is not clear cut.
Is CF_Connection defined? Does error_reporting(E_ALL) give you more helpful info? What does var_dump($conn instanceof CF_Connection) output?
This should point you in the right direction.

Categories