olezko46
olezko46

Reputation: 17

Upload file without form (Laravel)

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

Upvotes: 1

Views: 1467

Answers (2)

Ahajji87
Ahajji87

Reputation: 51

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

Upvotes: 0

Valery Viktorovsky
Valery Viktorovsky

Reputation: 6726

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

Upvotes: 1

Related Questions