Jogex's avatar
Level 2

Uploading .zip file wont validate hasfile(), or contain any real data

Hey!

This leaves me very much confused... Im working on a function for uploading .zip files, but the request file always seems to be "empty". All attributes like size, owner, path, filename etc are just "false".

This is the result of $request->file('fileName');

UploadedFile {#629 ▼
  -test: false
  -originalName: "test.zip"
  -mimeType: "application/octet-stream"
  -size: 0
  -error: 1
  #hashName: null
  path: ""
  filename: ""
  basename: ""
  pathname: ""
  extension: ""
  realPath: "C:\Users\Me\Documents\GitHub\ThisRepo\public"
  aTime: 1970-01-01 00:00:00
  mTime: 1970-01-01 00:00:00
  cTime: 1970-01-01 00:00:00
  inode: false
  size: false
  perms: 00
  owner: false
  group: false
  type: false
  writable: false
  readable: false
  executable: false
  file: false
  dir: false
  link: false
}

Selecting an image or any other file (like a .pdf or .psd) works just fine.

Is this intended, or what am I doing wrong here?

View:

<form action="{{ url("upload") }}" method="POST" enctype="multipart/form-data">
  @csrf
  <input type="file" name="fileName" class="form-control-file">
  <button type="submit">Upload</button>
</form>

Controller:

public function addPlugin (Request $request)
{

    if ($request->hasfile('fileName')) {
      // never triggeres
    }
    $file = $request->file('fileName');
    // $file doenst contain the expected data.
}

Any help would be greatly appreciated!

0 likes
3 replies
pardeepkumar's avatar

I think try this

$validator = Validator::make($request->all(), [
            'resume'   => 'mimes:doc,pdf,docx,zip'
        ]);

or also use Flysystem


use League\Flysystem\Filesystem;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;

$filesystem = new Filesystem(new ZipArchiveAdapter(getrealpath($value)));





foreach ($filesystem->listContents() as $object) {
    if (!in_array($object['mimetype'], ['mimetype1', 'mimetype2'])) {
        return false;
    }

}
return true




More Information

http://flysystem.thephpleague.com/docs/adapter/zip-archive/

or check full article

https://www.cloudways.com/blog/laravel-multiple-files-images-upload/

lostdreamer_nl's avatar
Level 53

Your UploadedFile has error: 1

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

Upload a smaller file or increase both post_max_size & upload_max_filesize in your php.ini (you can also do this via htaccess)

php_value post_max_size 20M
php_value upload_max_filesize 20M
1 like
Jogex's avatar
Level 2

Thank you! Rookie mistake :D

Please or to participate in this conversation.