Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recieve an image (file) with PHP that was submitted over a POST request

I have a file: success.jpg

I would like to send this file over an HTTP POST request and have it land in a public directory on my server.

I have a simple HTML form and PHP processor that work if I'm uploading from the browser: php.net/manual/en/features.file-upload.post-method.php

I'm trying to drop the use of a form altogether and just pass data over POST to a URL (e.g. myimageserver.com/public/upload.php).

It seems that I can use the PHP function move_uploaded_file and it even talks about using POST here: http://php.net/manual/en/function.move-uploaded-file.php but it doesn't provide the code which can receive and store a file that has been uploaded with POST.

Has anyone ever done something similar?

like image 404
sparecycle Avatar asked Oct 31 '14 16:10

sparecycle


1 Answers

If you want to upload using a mobile app for example, you have to send via POST the base64 content of the image with the mimetype or the file extension of it, and then use something like this:

  • Send the content base64 encoded and urlescaped.
  • Receive the content and do base64 decode and then urldecode.

Then in PHP just do:

<?php
$base64decodedString = base64_decode(urldecode($_POST['yourInputString']));
$fileName = $_POST['fileNameString']; 
file_put_contents($fileName, $base64decodedString);

This will generate a file with the content

like image 77
Germanaz0 Avatar answered Oct 12 '22 19:10

Germanaz0