upload video from iphone device to server in php - 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 ?

Related

How can I convert base64 string to image and upload the file in storage and save the path in the database

I am building an SPA. I am getting base64 string from my vue component in my laravel controller. How can I convert base64 string to a file. Then upload the site into my laravel local storage and then save the path in the database table ?
I have googled almost every link and tried too many things. I even tried image intervention but the only thing I was able to achieve was to upload the file into the laravel storage but wasn't able to store the right path in the database table.
Here is my controller code
$team = new Team;
$team->hood_id = $user->hood->id;
$team->title = $request->title;
$team->max_no_of_players = $request->max_no_of_players;
$team->description = $request->description;
$team->about_us = $request->about_us;
$team->email = $request->email;
$team->contact_no = $request->contact_no;
$team->meetup_place = $request->meetup_place;
/**
* Image base64 converting code starts from here
*/
$image = $request->image;
preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension
$image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part
$image = str_replace(' ', '+', $image);
$imageName = 'image_' . time() . '.' . $image_extension[1]; //generating unique file name;
$team->image = Storage::disk('public')->put($imageName, base64_decode($image));
I should be able to use Storage::url($image); when fetching the teams from database table. But right now I am not getting the correct path, Any help would be highly appreciated
In this way, you can fetch the file from the Base64 URL, I used this way.
$pos = strpos($fileBase64Url, ';');
$filetype = explode('/', substr($fileBase64Url, 0, $pos))[1]; //get file type
$contents = file_get_contents($fileBase64Url); //get the content from the URL
$unique_name = 'filename.' . $filetype; //file name
Storage::put('/public/folderName/' . $unique_name, $contents); //save to the directory
//$unique_name (Save this name to the Database)
And this is the path of this image file
http://127.0.0.1:8000/storage/folderName/filename.jpg (remove public form url)

Error 403 on writing base64 on file with 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?

Save base64 encoded image as JPG instead of PNG

I am saving a base64 encoded image using this simple PHP script...
define('UPLOAD_DIR', 'upload/');
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
file_put_contents($file, $data);
This works great but the image is always a PNG. I have tried changing the extension to JPG in the hopes that it would work but it doesn't.
Do I need to save it as a PNG first then convert it to a JPG?
Yes, you must convert the image into JPG at first.

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 :)

Categories