Skip to content

Instantly share code, notes, and snippets.

@antxd
Last active July 20, 2023 19:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save antxd/9fa72983d5197c166e441276e23bf786 to your computer and use it in GitHub Desktop.
Save antxd/9fa72983d5197c166e441276e23bf786 to your computer and use it in GitHub Desktop.
Simple example of case use imgbb API
<?php
function save_record_image($image,$name = null){
$API_KEY = 'YOUR_CLIENT_API_KEY';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgbb.com/1/upload?key='.$API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
$extension = pathinfo($image['name'],PATHINFO_EXTENSION);
$file_name = ($name)? $name.'.'.$extension : $image['name'] ;
$data = array('image' => base64_encode(file_get_contents($image['tmp_name'])), 'name' => $file_name);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
return 'Error:' . curl_error($ch);
}else{
return json_decode($result, true);
}
curl_close($ch);
}
if (!empty($_FILES['record_image'])) {
$return = save_record_image($_FILES['record_image'],'test');
echo $return['data']['url'];
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="record_image" accept="image/*">
<button type="submit">Guardar</button>
</form>
@Sosobou17
Copy link

it does not work

@Zona-Fantasma
Copy link

It worked for what I needed, just a note is that in php 7 you no longer need to put "curl_setopt ($ ch, CURLOPT_SAFE_UPLOAD, false);" in the code

@fadhil-riyanto
Copy link

wow, its working...thanks

@ommiyoo
Copy link

ommiyoo commented Dec 26, 2020

It is working .... Thanks

@nalamapu
Copy link

How to rename the uploaded file with original filename and mentioned suffix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment