0
votes

I try using cloudinary for upload my image and video for my personal blog but it's a fail. I watch this video tutorial https://www.youtube.com/watch?v=oZCQLjfq97o. the error says :

Notice: Undefined index: file in >C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php on line 8

Notice: Undefined index: file in >C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php on line 9

Fatal error: Uncaught Cloudinary\Error: Missing required parameter - file in C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php:558 Stack trace: #0 C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php(407): Cloudinary\Uploader::call_api('upload', Array, Array, NULL) #1 C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php(100): Cloudinary\Uploader::call_cacheable_api('upload', Array, Array, NULL) #2 C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php(11): Cloudinary\Uploader::upload(NULL, Array) #3 {main} thrown in C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php on line 558

This is my code for upload :

require 'vendor/autoload.php';
require 'config.php';

if (isset($_POST['Simpan'])) {
    $nama = $_POST['nama'];
    $slug = $_POST['slug'];
    $gambar = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];

    \Cloudinary\Uploader::upload($file_tmp, array('public_id' => $slug));
}

and this is the code for form :

<form method="POST">
    <input type="text" name="nama">
    <input type="text" name="slug">
    <?php echo cl_image_upload_tag('image_id');?>
    <input type="submit" name="Simpan" value="Simpan">
</form>
1

1 Answers

0
votes

The cl_image_upload_tag is used to perform direct uploading from the browser to Cloudinary. When you submit the form you there is no file parameter sent to your backend and thus the Cloudinary::Uploader::upload call fails with the error Missing required parameter - file.

If you would like to upload the file from your backend then you need to replace the cl_image_upload_tag with a regular file input field element. Once a file is selected and the form submitted your backend can use the same code to upload the file to Cloudinary.

On the other hand, if would like to use cl_image_upload_tag then please see the direct uploading section of the Cloudinary documentation that describes setting this up. https://cloudinary.com/documentation/php_image_and_video_upload#direct_upload_file_tag