1

Im trying to upload a mp3 file with the size of approximately 6MB. The problem is that I keep getting following output from print_r($file)

 Array
 (
 [name] => a.mp3
 [type] => 
 [tmp_name] => 
 [error] => 1
 [size] => 0
 )

I guess I have the error code 1, now I've read Can't upload mp3 files on my php script and set my settings to

memory_limit = 99M
max_execution_time = 300
upload_max_filesize = 20M
post_max_size = 24M

Now I cant even upload images anymore because it will throw error 0.

What did I do wrong?

Here is my code

$file = $_FILES['file'];
    $id = $_POST['id'];
    $title = $_POST['title'];
    print_r($file);

    $id=htmlspecialchars(strip_tags($id));

    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    //$fileType = $_FILES['file']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowedImage = array('jpg', 'jpeg', 'png', 'pdf');
    $allowedPodcast = array('mp3');
    $allowedVideo = array('mp4');

    if (in_array($fileActualExt, $allowedPodcast)) {

        if ($fileError === 0) {

            if ($fileSize < 1000000) {

                try {
                    $conn->beginTransaction();
                    $fileNameNew = uniqid('', true).".".$fileActualExt;
                    $fileDestination = '../api/data/object/'. $id . '/image%' .$fileNameNew;
                    move_uploaded_file($fileTmpName, $fileDestination);

                    $query = "INSERT INTO media SET type='image', url=:url, id_object=:id_object, title=:title";

                    $stmt = $conn->prepare($query);                  

                    $stmt->bindParam(":url", $fileDestination);
                    $stmt->bindParam(":id_object", $id);
                    $stmt->bindParam(":title", $title);

                    $stmt->execute();
                    $conn->commit();
                    echo json_encode(array("message" => "file inserted successfully!"));
                } catch (Exception $e) {
                    $conn->rollBack();
                    echo json_encode(array("message" => "An error occured!"));
                }


            } else {
                echo json_encode(array("your file was too big"));
            }

        } else {
            echo json_encode(array("message" => "there was an error uploading your file"));
        }

    }
Huondui
  • 383
  • 1
  • 3
  • 12
  • 2
    `[error] => 0` would mean there was _no_ error; something else must go wrong in your script then. Please go read [ask] and [mre], and edit your question accordingly. – misorude Aug 14 '19 at 13:44
  • Does `php_info` when called from browser show the changes made for the upload sizes? Also is it `[error] => 1` or `will throw error 0` – user3783243 Aug 14 '19 at 13:55
  • @php_n00b Its hard to say what you did wrong if you dont show what you did. Refer to [PHP Uploader Errors](https://www.php.net/manual/de/features.file-upload.errors.php) for detailed information about the errors. `0` means ok, `1` occurrs when you ini max filesize is to low. – Code Spirit Aug 14 '19 at 13:56
  • Error 1 says that the uploaded file exceeds the maximum size in php.ini. The zero (0) means that there was no error and the file was uploaded correctly. Your assertion that your changes caused something else to fail doesn't make sense since a zero error code is correct and desired. – Dave Aug 14 '19 at 13:56
  • I added my code, in case it helps – Huondui Aug 14 '19 at 13:57
  • @misorude is right about the second part of my question, the first part is still a big question for me – Huondui Aug 14 '19 at 13:57
  • 1
    You probably can't upload images (or anything other than `.mp3`) because `if (in_array($fileActualExt, $allowedPodcast)) { ...` is limiting to `mp3` only. – brombeer Aug 14 '19 at 14:01
  • @kerbholz I have only uploaded one of 3 if statements, i have the same for images and videos but with slightly other parameters – Huondui Aug 14 '19 at 14:03
  • ok, I might have found the problem, Im testing on xampp and I edited php.ini-development and php.ini-production because there is no php.ini, may that be the problem? – Huondui Aug 14 '19 at 14:09

0 Answers0