0

I'm using a library to handle my uploads, this is installed via composer.

The library is called FileUpload.

As you can see from the documentation, three parts of the library are abstracted so you can create your own custom class if you need to.

"The reason why the path resolver, the validators and the file system are abstracted, is so you can write your own, fitting your own needs (and also, for unit testing). The library is shipped with a bunch of "simple" implementations which fit the basic needs. You could write a file system implementation that works with Amazon S3, for example."

This is great, but I need to override a function on this class.

<?php

namespace FileUpload;

class File {
  /**
   * Preset no errors
   * @var mixed
   */
  public $error = 0;

  /**
   * Preset unknown mime type
   * @var string
   */
  public $type  = 'application/octet-stream';

  /**
   * Is the file completely downloaded
   * @var boolean
   */
  public $completed  = false;

  /**
   * Determine file type from path (actual mime type, not extension checking)
   * @param string $path
   */
  public function setTypeFromPath($path) {
    $this->type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  }

  /**
   * Does this file have an image mime type?
   * @return boolean
   */
  public function isImage() {
    return in_array($this->type, array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'));
  }
}

The function I need to override is: setTypeFromPath(). This function is using a php function to get the actual mime type of the file.

finfo_file, which is fine for images and standard documents but when I try to upload a compressed document e.g docx, potx it doesn't pass my validation as the mime type is returned as application/zip.

My validation:

$validator = new FileUpload\Validator\Simple(1024 * 1024 * 10, array(
                'application/msword',
                'application/word',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
                'application/pdf',
                'application/x-pdf',
                ' application/acrobat',
                'applications/vnd.pdf'
            ), $_REQUEST);
CharliePrynn
  • 3,034
  • 5
  • 40
  • 68

0 Answers0