HelpNeeder
HelpNeeder

Reputation: 6480

Keeps throwing 'invalid file' exception, where lays the issue?

I'm having an issue with this snippet where I try to validate a file that I'm trying to upload. The code come from this page, but it keeps throwing Invalid file format. exception when I'm checking the mime types. I need to upload only PDF files. Every PDF file I tried, failed.

What could be the issue here?

    private function File($f) { // example from http://php.net/manual/en/features.file-upload.php
        try {
            if(!isset($f['file']['error']) || is_array($f['file']['error'])) {
                throw new RuntimeException('Invalid parameters.');
            }

            switch ($f['file']['error']) {
                case UPLOAD_ERR_OK:
                    break;
                case UPLOAD_ERR_NO_FILE:
                    throw new RuntimeException('No file sent.');
                case UPLOAD_ERR_INI_SIZE:
                case UPLOAD_ERR_FORM_SIZE:
                    throw new RuntimeException('Exceeded filesize limit.');
                default:
                    throw new RuntimeException('Unknown errors.');
            }

            if($f['file']['size'] > 10000000) {
                throw new RuntimeException('Exceeded filesize limit.');
            }

            $finfo = new finfo(FILEINFO_MIME_TYPE);

            $extentions = array(
                'pdf' => 'application/pdf',
                'pdf' => 'x-pdf',
                'pdf' => 'application/vnd.cups-pdf',
                'pdf' => 'application/vnd.sealedmedia.softseal-pdf'
            );

            $ext = array_search($finfo->file($f['file']['tmp_name']), $extentions);

            if(false === $ext) {
                throw new RuntimeException('Invalid file format.');
            }

            if(!move_uploaded_file($f['file']['tmp_name'], sprintf('./uploads/%s.%s', sha1_file($f['file']['tmp_name']), $ext))) {
                throw new RuntimeException('Failed to move uploaded file.');
            }

            echo 'File is uploaded successfully.';
        } 
        catch (RuntimeException $e) {
            echo $e->getMessage();
        }
    }

Upvotes: 0

Views: 777

Answers (2)

Crackertastic
Crackertastic

Reputation: 4913

You are overwriting your array keys in $extentions with new values instead of adding new key/value pairs.

The following code isn't doing what you think:

$extentions = array(
    'pdf' => 'application/pdf',
    'pdf' => 'x-pdf',
    'pdf' => 'application/vnd.cups-pdf',
    'pdf' => 'application/vnd.sealedmedia.softseal-pdf'
);

A var_dump($extentions); will produce:

array(1) {
  ["pdf"]=>
  string(40) "application/vnd.sealedmedia.softseal-pdf"
}

You need to add the additional MIME types instead of writing over them.

Upvotes: 1

cwallenpoole
cwallenpoole

Reputation: 81988

One problem is this: I believe your keys and values are swapped. In the following, there will be an array called $extensions with one key pdf associated with one value application/vnd.sealedmedia.softseal-pdf

 $extentions = array(
    'pdf' => 'application/pdf',
    'pdf' => 'x-pdf',
    'pdf' => 'application/vnd.cups-pdf',
    'pdf' => 'application/vnd.sealedmedia.softseal-pdf'
 );

My suspicion is that you are actually trying to do this:

$extentions = array(
    'application/pdf' => 'pdf',
    'x-pdf' => 'pdf',
    'application/vnd.cups-pdf' => 'pdf',
    'application/vnd.sealedmedia.softseal-pdf' => 'pdf'
 );

$ext = @$extensions[$finfo->file($f['file']['tmp_name'])];

Upvotes: 1

Related Questions