0

I try to upload a file with PHP and I use the function "move_uploaded_file()" for it. Everything works well, until I try to upload a zipped CSV file. The file itself is 50MB, but once it is zipped, it's just 3MB. I can upload files until 5MB to the server, so in the case of the zip file, I thought this would work, but it doesn't.

* Uploading a zipped CSV file of 100kb does work,
* I did delete 1/3 of the lines from the CSV file, the CSV file is 28MB, and this I could upload (zipped it was 1.6MB),
* The zipped file of 3MB doesn't appear in the folder on the server. The zipped files of 100KB and 1.6MB do appear there,
* I didn't get an error message, but it does echo "Error uploading!".

Did someone face the same problems, or does someone knows a solution? Let me know! This is basicly my code;
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_POST["submit"])) {
    $file = $_FILES["file"];
    $filename = $file["name"];
    $target_dir = "data/";
    $target_file = $target_dir . basename($filename);
    $file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    if (move_uploaded_file($file["tmp_name"], $target_file)) {
        echo "Done uploading!";
    } else {
        echo "Error uploading!";
    }
}
?>

<form action="" method="post" enctype="multipart/form-data">
    <div class="custom-file mb-3">
        <input type="file" class="custom-file-input" id="file" name="file" required accept=".csv,.zip" />
    </div>
    <p class="text-right">
        <button class="btn btn-primary" name="submit" type="submit">Upload!</button>
    </p>
</form>


Let me know if you need something. All the help and ideas are appericiated. Thanks!


Edit;
I did got an error in $_FILES['file']['error'];
I got error "1", which reffers to: 'The uploaded file exceeds the upload_max_filesize directive in php.ini'
https://www.php.net/manual/en/features.file-upload.errors.php

I did "echo phpinfo();", and found out that upload_max_filesize is only 2MB instead of the 5MB that I thought I configurated.
Antonio
  • 133
  • 1
  • 11
  • _"I thought this would work, but it doesn't"_ What part doesn't work? You're explicitly deleting the uploaded target file, what are you expecting to happen? – Alex Howansky Nov 03 '21 at 22:08
  • What error messages are you getting? How do you know it isn't working? – ryantxr Nov 03 '21 at 22:09
  • 1
    Check any PHP logs in case this is a memory issue or make sure you are reporting errors – apokryfos Nov 03 '21 at 22:12
  • The zipped file of 3mb doesn't appear in the folder on the server. The zipped file of 100kb does appear there. I didn't get an error message, it does stops after a while. – Antonio Nov 03 '21 at 22:12
  • @apokryfos I'll do that tomorrow morning, I'll let you know the results of that – Antonio Nov 03 '21 at 22:13
  • How do you know it doesn't appear when you're _immediately deleting it_? Some very rudimentary debugging will locate your issue. What is `move_uploaded_file()` returning? What is `$file_type` getting set to? What is `ZipArchive::open()` returning? What is `unlink()` returning? – Alex Howansky Nov 03 '21 at 22:27
  • 1
    debug whats in $_FILES, if `error` is populated correlate it with the errors shown in the docs https://www.php.net/manual/en/features.file-upload.errors.php if you not changed your servers config its probably post_max_size/upload_max_filesize settings – Lawrence Cherone Nov 03 '21 at 23:42
  • You need more info. Check any PHP log file, not all PHP errors are shown in screen. Put debugging messages, for example echo "Moved"; when move_uploaded_file(), and echo "Not moved" when else. – José Carlos PHP Nov 04 '21 at 00:07
  • @AlexHowansky excuse me, I've changed the script in my question to the complete basics. The script does echo "Error uploading!", although it doesn't show any error – Antonio Nov 04 '21 at 09:02
  • wrap your code in try catch statement and in catch check if there is an exception being thown – Waqas Mustafa Nov 04 '21 at 09:27
  • 1
    @LawrenceCherone you're right, I did got an error in $_FILES['file']['error']; "1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini'." I did "echo phpinfo();", and found out that upload_max_filesize is only 2MB instead of the 5MB that I thought I configurated. – Antonio Nov 04 '21 at 09:27

0 Answers0