uploading image file in dropbox using dropbox api in php - php

How can I upload image files to Dropbox with the Jimscode PHP Dropbox component for CodeIgniter? It uses the add() method for uploading files. It successfully uploads all other files types (e.g. pdf) to Dropbox. But when I upload image files, it uploads empty files to Dropbox. That is of 0 bytes and unable to preview that image in Dropbox. My code:
public function add($dbpath, $filepath, array $params = array(), $root=self::DEFAULT_ROOT)
{
$dbpath = str_replace(' ', '%20', $dbpath);
$filename = rawurlencode($filepath);
$parstr = empty($params) ? '' : '&'.http_build_query($params);
$uri = "/files_put/{$root}/{$dbpath}?file={$filename}{$parstr}";
//$uri = reduce_double_slashes("/files/{$root}/{$dbpath}?file={$filename}{$parstr}");
$specialhost = 'api-content.dropbox.com';
return $this->_post_request($uri, array('file'=>'#'.$filepath), $specialhost);
}
Is there an API method I can use directly with curl and PHP?

I'm not sure what version you are using but the reduce_double_slashes line isn't commented out normally. That wouldn't cause some file types to upload and not others though. Is the failure with a single image or does it fail to upload with every image.
You can also set the DEBUG const in the library to true and have it write debug information out to your error log. That might help you figure out where the issue is happening.

Related

Sending image or video file as response in CakePHP 4 is not working

I have tried to send image and video file as response with the code as per CakePHP 4 documentation: https://book.cakephp.org/4/en/controllers/request-response.html#sending-files
Below is my function to access file from url.
public function noteFileAccess() {
$filename = $this->request->getParam('filename');
$id = $this->request->getParam('id');
$path = Configure::read('FilePath.NoteFiles');
$file = new File($path . $id . DS . $filename);
if(!$file->exists()) {
throw new ForbiddenException;
}
$this->response = $this->response->withFile($file->path);
//Also tried to set mime type like below commented line
//$this->response = $this->response->withType($file->mime())->withFile($file->path);
return $this->response;
}
By using above code, it is sending pdf, zip, csv files properly. But images & videos files are not working. Its showing white square in chrome browser. And if downloaded it is showing format not supported in windows system.
Only different I see in downloaded image file vs original image file is image dimensions and some other things like in screenshot below
Edit: File Content Difference added below
I have checked with file path & mime type of the image file is good.
Do you think I am missing anything here?

When uploading images in Amazon S3 it tells me that the file is damaged

Currently I want to upload the images that the client chooses to the Amazon S3 service and in fact it seems that it already does so because it uploads a file and tells me that it weighs 92kb the same as the file I am choosing but when I want to open it with the path that is It gives me the following error:
The image <image path> cannot be displayed because it contains errors.
Code Laravel
public function SaveImage($request){
if ($request->hasFile('inputFile')) {
$image = $request->file('inputFile');
$fecha= date("m-d-y");
$name = $fecha."_".$request->txtNombre.'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('UploadProfile');
$image->move($destinationPath, $name);
$filePath=$destinationPath."\\".$name;
$initPath="Perfil\\".$name;
Storage::disk('s3')->put($initPath, $filePath);
return $urlImage= Storage::cloud()->url($initPath);
}
}
the strangest thing is that it does not mark me errors, everything runs correctly but if it shows me that the files were uploaded in the panels of the buckets, besides if I upload directly through the buckets and open the path if it opens right way.
The image path would be as follows:
https://katanasystem.s3-us-west-1.amazonaws.com/Profile/01-05-20_david.jpg

$_FILES to a resource - Laravel 5 and Dropbox

Well, I've uploaded an app to Heroku, and I've discovered that I can't upload files to it. Then I started to use Dropbox as storage option, and I've done a few tests, of send and retrieve link, and all worked fine.
Now, the problem is to use the uploadFile() method on DropboxAdapter. He accepts an resource as the file, and I did'nt work well. I've done a few tests, and still no way. Here is what I am doing, if anyone could me point a solution, or a direction to this problem, please. :)
Here is my actual code for the update user (Update the user image, and get the link to the file).
$input = $_FILES['picture'];
$inputName = $input['name'];
$image = imagecreatefromstring(file_get_contents($_FILES['picture']['tmp_name']));
Storage::disk('dropbox')->putStream('/avatars/' . $inputName, $image);
// $data = Storage::disk('dropbox')->getLink('/avatars/' . $inputName);
return dd($image);
In some tests, using fopen() into a file on the disk, and doing the same process, I've noticed this:
This is when I've used fopen() on a file stored on the public folder
http://i.imgur.com/07ZiZD5.png
And this, when i've die(var_dump()) the $image that I've tried to create. (Which is a suggestion from this two links: PHP temporary file upload not valid Image resource, Dropbox uploading within script.
http://i.imgur.com/pSv6l1k.png
Any Idea?
Try a simple fopen on the uploaded file:
$image = fopen($_FILES['picture']['tmp_name'], 'r');
https://www.php.net/manual/en/function.fopen.php
You don't need an image stream but just a filestream, which fopen provides.

Symfony2: Amazon S3 retrieve image and mofidy using Imagine

I'm using amazon s3 to store my images. For now I've got 3 different folders - temporary one to not accepted pictures, one for accepted and one for watermarked one.
When photo is being accepted it is copied to accepted and watermarked folders and deleted form temporary one.
The problem is with watermarked folder because I need to add watermark. During work in progress I've mocked method that is adding watermark like this:
protected function addWatermark($product)
{
$filterName = 'watermark';
$this->imagineFilterManager->getFilter($filterName)
->apply($this->imagine->open($product->getFilePath()))
->save($path, array('format' => "png"));
}
and it was pretty fine. But the problem is - mock was working on uploaded file, and FilePath was temporary file path to image.
Now I need to retrieve image from amazon s3, pass it to imagineFilterManager and save back at amazon s3 location. The problem is with retrieving photo.
I tried like this:
read method:
public function read($path)
{
return $this->fileSystem->read($path);
}
protected function addWatermark($product)
{
var_dump('addWatermark');
$filterName = 'watermark';
$path = $this->getShopPicturePath($product);
$file = $this->staticContentAdapter->read($path);
$file = imagecreatefromstring($file);
$this->imagineFilterManager->getFilter($filterName)
->apply($this->imagine->open($file))
->save($path, array('format' => "png"));
var_dump('filtered');exit;
}
but after this $this-imagine-open() returns
File Resource id #141 doesn't exist
The strange thing form me is that it is was possible for me to save image file on s3 using only path:
->save($path, array('format' => "png"));
but it is not possible to open file using only path:
->apply($this->imagine->open($file->getFilePath()))
It throws exceptions
File watermarked/asd_asd.png doesn't exist.

Uploading image with url address in Codeigniter

CodeIgniter upload library only supports uploading image file from PC.
I am trying to make a code that a user can upload image with image url address.
http://example.com/photo/jake2910.jpg
I simply send this image address to server and upload the photo.
Is there a way to do that? (trust me I tried different codes already for few days.)
There is no need in CI's upload library at all. You can simply download the file with this code:
$url = 'http://example.com/photo/jake2910.jpg';
/* Extract the filename */
$filename = substr($url, strrpos($url, '/') + 1);
/* Save file wherever you want */
file_put_contents('upload/'.$filename, file_get_contents($url));
But be very cautious about what files you are trying to save. Remember that even images can be stuffed with vulnerable code.
Try this
copy('http://example.com/photo/jake2910.jpg', '/mylocation/jake2910.jpg');
for more details you can check this similar post
Copy Image from Remote Server Over HTTP

Categories