0

I am currently developing a website with AngularJS for the frontend, and a restful web service using Laravel framework (PHP5) as backend ; I am currently face to one problem, file uploading. I would like to have a very simple file uploaded, with a file input and just some copy into one of the server folder.

For now, I am using a directive in AngularJS that work great, and send my file using base64 url ; I am able to get this string inside my PHP backend, but unable to transform it to a file. For example, I would like to be able to get the filename, ext, ... and copy it to the server..

Any idea how to convert a base64 to a "file object" ? (Whether the solution is designed for Laravel et simple PHP doesn't bother me).

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Jérémy Dutheil
  • 6,099
  • 7
  • 37
  • 52

1 Answers1

0

It's pretty easy just decode the base 64 String on your server and write into a file.

Example if the image is located in forms image data and data were send as POST:

file_put_contents('path/to/images/image.jpg', base_64_decode($_POST['image']));

I'm not firm with laravel but I think there are better ways inside the Framework than accessing form data PHP's $_POST Array directly.

To find of the filetype of the image have a look at finfo_buffer and see how to use for your case here

Community
  • 1
  • 1
Bernhard
  • 4,855
  • 5
  • 39
  • 70
  • In fact my String is beginning like that `data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4SPrRXhpZgAATU0AKgAAAAgACgEPAAIAAAAGAAAAhg` (comes from the HTML5 Fileread API), so I think I have to do some extra work to make it work.. I tryed removing the "data:image/jpeg;base64" part but it won't do anything, I can save the file but I am unable to open it (`Error interpreting JPEG image file (Not a JPEG file: starts with 0x2f 0x39)`) – Jérémy Dutheil Dec 12 '13 at 07:28
  • It's definitly working. You need to remove the data:image/jpeg;base64, from string I also did it in previous projects and wrote into a file – Bernhard Dec 13 '13 at 19:14