0
votes

I've set the value of upload_max_filesize and post_max_size to 128M in the php.ini file of XAMPP. Also, when I display phpinfo() it says that the maximum file upload size is at 128 Megabytes.

However, when I try to upload a 512 Kilobytes big file via a default file upload form, there just appears the text "Couldn't upload the file!". Strangely enough, it works with smaller files like 158 Kilobytes.

Of course, I tried out to change the max_file_uploads value. I restarted the XAMPP server multiple times.

HTML Code:

<form method="post" enctype="multipart/form-data">
    <!-- Avatar -->
    <div class="form-group">
        <label for="avatar">Avatar</label>
        <input type="file" name="avatar">                           
    </div>
</form>

PHP Handling code:

// Avatar
        if (isset($_FILES['avatar'])) {
            $fileName = $_FILES['avatar']['name'];
            $tmpName = $_FILES['avatar']['tmp_name'];
            $fileSize = $_FILES['avatar']['size'];
            $fileType = $_FILES['avatar']['type'];

            echo $fileName . " " . $fileSize . " " . $fileType . " - ";

            $newData['profile_picture'] = $this->fileModel->uploadFile($fileName, $tmpName, $fileSize, $fileType);
            unset($_FILES['avatar']);
        }

fileModel->uploadFile:

public function uploadFile($original_name, $tmpname, $size, $type)
{
    $target_dir = "files" . DIRECTORY_SEPARATOR;
    $filetype = pathinfo(basename($original_name), PATHINFO_EXTENSION);
    $target_file = $target_dir . $this->generateName() . '.' . $filetype;
    $name = explode('.' . $filetype, explode(DIRECTORY_SEPARATOR, $target_file)[1])[0];

    if (!move_uploaded_file($tmpname, $target_file)) {
        die('File couldn\'t be uploaded!');
    }

    $this->db->query('INSERT INTO files (name, original_name, type, size, path) VALUES (?, ?, ?, ?, ?)', [$name, $original_name, $type, $size, $target_file]);

    return "/file/open/" . $name;
}

What's returned when uploading an image:

KingOfDog Logo.png 162649 image/png - Couldn't upload the file!

Here's what is returned when doing var_dump($_FILES):

array(2) { ["avatar"]=> array(5) { ["name"]=> string(18) "KingOfDog Logo.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(25) "D:\xamppi\tmp\phpED5E.tmp" ["error"]=> int(0) ["size"]=> int(162649) } ["header"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } }

I'm running CodeIgniter on PHP version 5.6.28 with XAMPP v3.2.2 on a local Windows machine. Also, I'm using Bootstrap and jQuery.

1
"Couldn't upload the file!" Did you set this error? post the code that handles the uploadSpoody
@MehdiBounya No, I didn't set this error. In my whole project, PHPStorm couldn't find this string.KingOfDog
Please re edit your question with your upload code.Mr. ED
print_r($_FILES); will show you the real erroruser8011997
@wolfgang1983 Finished.KingOfDog

1 Answers

0
votes

You also must increase max_post_size value as it is supper and file upload goes via POST request.