Search code examples
phpmysqlcodeigniterblobmp3

Input mp3 file returns 0


When I try to input an mp3 file the $_FILES returns null.

Controller:

$audio = $_FILES['muziek']['tmp_name'];

if ($audio != ''){
     $audioContent = file_get_contents($audio);
     $this->load->model('muziek_model');
     $this->muziek_model->insertMuziek($audioContent);
}
var_dump($audio);
var_dump($audioContent);

View:

<?php echo form_open_multipart('Muziek/uploadMuziek');?>
<table>
    <tr>
        <td><?php echo form_label('MP3 Bestand:', 'muziek'); ?></td>
        <td><input type="file" name="muziek" id="muziek" accept=".mp3" style="margin-left: 10px; padding-bottom: 35px; color: black" class="btn btn-login form-control login-formcontrol bg-white"/></td>
    </tr>
    <tr>
        <td></td>
        <td>
            <?php echo form_submit('knop', 'Uploaden', 'class = "btn btn-login login-formcontrol"'); ?>
        </td>
    </tr>
</table>
<?php echo form_close();?>

When I select an mp3 file audio should not be null


Solution

  • File uploads can fail for a variety of reasons. https://www.php.net/manual/en/features.file-upload.errors.php

    Your's is failing because it is too big:

    Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

    If you have access to php.ini you can change this variable to make it accept larger files.

    Even if you do this with success, I would suggest checking for upload errors if(!isset($_FILES['muziek']['tmp_name']) || $_FILES['muziek']['error'] !== 0) { echo 'error'; } before trying to perform actions on a file that may or may not exist.

    I personally use: https://github.com/verot/class.upload.php

    but Codeigniter has an in-built upload library.