Error 403 on writing base64 on file with php - php

I'm trying to decode a base64 string and write it to a file:
<?php
// requires php5
define('UPLOAD_DIR', '/images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
But the server gives this error message:
403 Forbidden
You don't have permission to access testupload.php on this server
I have set all permissons for the php file and the images directory, so I don't understand why I get this error.
When I put the decoded string of image directly inside the $img variable, it works and it creates the file under the images folder... But when I try to post the decoded string it doesn't work.
How can I solve this?
NOTE :
when we try to send decoded string of image it may contain some virus or something like that from hackers ... i contact with my host providers and they say if you want we can turn of the modsecurity but its really unsafe way to do ... so what i have to do with this situation?

Related

PHP Decode Base64 Image to png and save in current directory

I know there are so many solutions for decode base64 image. But any of them didn't work for me.
I have the Base64 image data and i need to convert it to a png file and save it to the local directory.
I tried the following code which i got from here. But it didnt work for me. Please help me to get this done..
define('UPLOAD_DIR', 'images/');
$base64string = "data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBB...";
$img = $base64string;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . 'txtimg.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
When i execute the script its getting the error message Unable to save. Then i gave full permission for the users. But still no luck.
I tried to save a simple text file through file_put_contents() ; it worked.
I'm using IIS 8 on Windows Azure Server.

PHP base64_docode saves corrupt image

I am sending base64 encoded image to PHP from my android app. Sometime it stores full image (4KB) and sometime (3KB) (Same Image). when I use URL in picasso, image with 4KB size works fine but image with 3KB size does not load it shows decode error.
This is my PHP code (which sometime works)
$encodedImage = str_replace(' ','+',$_POST['encodedProfileImage']);
$data = base64_decode($encodedImage);
$file = 'Pics/'. uniqid() . '.png';
$success = file_put_contents($file, $data);
$BASE_URL = 'http://domain.com/TestApp/';
I then do SQL operation in PHP to store image path. Is there any chance that next code operation is done on half decoded image(which is corrupt).
You need to remove the part that says data:image/png;base64, at the beginning of the image data. The actual base64 data comes after that.
Use below function:-
function base64_to_png($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");
$data = explode(',', $base64_string);
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $output_file;
}
If you want to use str_replace function then may be below way work. I am not sure :)
$fname = filter_input(INPUT_POST, "name");
$encodedImage = filter_input(INPUT_POST, "image");
$encodedImage = str_replace('data:image/png;base64,', '', $encodedImage);
$encodedImage = str_replace(' ', '+', $encodedImage);
$encodedImage = base64_decode($encodedImage);
file_put_contents($fname, $encodedImage);
print "Image has been saved!";
Hope it will help you :)

Decoding base64 string gives a black rectangle image

I'm trying to get an image from canvas and save it with my php script. But when the script proceedes, I got a simple black rectangle instead of my canvas image (web-cam snapshot).
Here is my code:
$img = $base64Img;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = "photo/" . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
What is the purpose of this line:
$img = str_replace(' ', '+', $img);
Don't think it is necessary.
Otherwise all seems fine.
I usually use php explode to isolate the data:
$exploded = explode(',', $img);
$base64 = $exploded[1];
$data = base64_decode($base64);
But str_replace should also do the job.
Maybe the error is in the code that loads the image?
UPDATE
Purpose of that line is to encode the white space in the base64 data.
This is also mentioned in this comment on php.net.
Probably it would be better to use the PHP function urlencode in such cases.
$data = base64_decode($data);
This line is probably causing the issues in this, since the data is not used as a data url but saved to a file directly.

Uploading a base64 image as an image file

So I'm receiving an image in base64 encoding. I want to decode the image and upload it in a specific directory. The encoded file is an image and I'm using $file = base64_decode($base64_string); to decode it. Uploading it via file_put_contents('/uploads/images/', $file); always results in failed to open stream: No such file or directory error.
So, how do I take the decoded string, and upload it on a specific path?
Thanks.
Your path is wrong.
/uploads/images
is checking for a folder in the / directory called uploads. You should specify the full path to the folder:
/var/www/vhosts/MYSITE/uploads/images
I had the same problem and finally solved using this:
<?php
$file = base64_decode($base);
$photo = imagecreatefromstring($file);
//create a random name
$RandomStr = md5(microtime());
$name = substr($RandomStr, 0, 3);
$name .= date('Y-m-d');
$name .= '.jpg';
//assign name and upload your image
imagejpeg($photo, 'images/'.$name, 100);

upload video from iphone device to server in php

I am working on the project which includes uploading image and video of user from iphone device. I am handling the backend with PHP. For uploading images I am getting the base64encode string and I have use following code to upload the image -
$str = $data['pictureurl']; // base64encode string
$newVar = base64_decode($str);
define('UPLOAD_DIR', 'userimages/');
$img = $str;
$date = strtotime(date("m-d-y H:i:s"));
$img = str_replace('data1:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data1 = base64_decode($img);
$file = UPLOAD_DIR . strtolower($data['firstName']).strtolower($data['lastName']). "_".$userId . '.png';
$totalpath = $path.$file;
#chmod('userimages/', 0777);
$success = file_put_contents($file, $data1);
From this code, image gets uploaded to the server in specified directory properly with .png extension. May I use the same code for uploading video making the file type .flv or .mov? I have tried it, but file gets placed in the folder with 0 bytes of data. What could be the problem? or is there any another solution to upload such file ?

Categories