1
votes

I'm trying to write REST API to laravel 5.1. How to upload a file (image) on server without form and views?

I tried to get out of the body of the request and convert it to a resource, but also get a line image.

$image = Request::getContent();
$i = imagecreatefromstring($image);
imagejpeg($i);
2

2 Answers

1
votes

You could use CURL with curl_file_create:

$file = '/foo/bar/img.jpg';
$filename = basename($file);
$data = [
    'uploaded_file' => curl_file_create($file, 'image/jpeg', $filename),
];
$ch = curl_init();   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

or just encode file with MIME base64 (base64_encode function) and include it as API param:

$file = '/foo/bar/img.jpg';
$imagedata = file_get_contents($file);
$base64 = base64_encode($imagedata);
0
votes

Input::file('nameOfParameterGet') You can get an array of files to.