0

I need to check if a uploaded file has .csv or .txt as extension and I'm doing as follow:

$uploadfiles = $_FILES['uploadfiles'];

echo '<pre>';
var_dump($uploadfiles);
echo '</pre>';

if (is_array($uploadfiles)) {
    foreach ($uploadfiles['name'] as $key => $value) {
        if ($uploadfiles['error'][$key] == 0) {

            $filetmp = $uploadfiles['tmp_name'][$key];
            $filename = $uploadfiles['name'][$key];

            $filetype = wp_check_filetype(basename($filename), null);
            $filetitle = preg_replace('/\.[^.]+$/', '', basename($filename));
            $filename = $filetitle . '.' . $filetype['ext'];
            $upload_dir = wp_upload_dir();

            if ($uploadfiles['type'] != "text/csv" || $uploadfiles['type'] != "text/plain") {
                echo "Error, the file $filename has not a valid extension: " . $filetype["ext"];
                continue;
            }

            echo "entre";die();

            $i = 0;
            while (file_exists($upload_dir['path'] . '/' . $filename)) {
                $filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
                $i++;
            }

            $filedest = $upload_dir['path'] . '/' . $filename;

            if (!is_writeable($upload_dir['path'])) {
                $this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
                return;
            }

            if (!move_uploaded_file($filetmp, $filedest)) {
                $this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
                continue;
            }
        }
    }
}

But I'm getting this error all the time:

Error, the file Aruba Airlines tickets-coupons (through 27-Apr-2014) - Aruba Airlines tickets-coupons .csv has not a valid extension: csv

I also test with this code:

if ($filetype['type'] != "text/csv" || $filetype['type'] != "text/plain") {
    echo "Error, the file $filename has not a valid extension: " . $filetype["ext"];
    continue;
}

And result is the same. If I do this:

echo '<pre>';
var_dump($uploadfiles);
echo '</pre>';

I get this as result:

array (size=5)
  'name' => 
    array (size=1)
      0 => string 'Aruba Airlines tickets-coupons (through 27-Apr-2014) - Aruba Airlines tickets-coupons .csv' (length=90)
  'type' => 
    array (size=1)
      0 => string 'text/csv' (length=8)
  'tmp_name' => 
    array (size=1)
      0 => string '/tmp/php53tzhZ' (length=14)
  'error' => 
    array (size=1)
      0 => int 0
  'size' => 
    array (size=1)
      0 => int 14084526

So where is the error?

Checking for compared values types

So following the suggestion from a member here I did this:

echo '<pre>';
print_r($uploadfiles);
echo '</pre>';

// this is the only key since I'm uploading 
// just one file for that I check in [0]     
echo gettype($uploadfiles['type'][0]); 

// output: string

Then:

echo gettype($filetype['type']);

// output: string

Then:

if ($filetype['type'] != $fileCSV || $filetype['type'] != $fileText) {
    echo "Error, the file $filename has not a valid extension: " . $filetype["ext"];
    continue;
}

Still getting the same error, this is driving me crazy!

2

Found where I made the error, on the condition, my code was:

if ($filetype['type'] != $fileCSV || $filetype['type'] != $fileText) {
    echo "Error, the file $filename has not a valid extension: " . $filetype["ext"];
    continue;
}

And the right is this one:

if (!in_array($filetype['type'], array($fileCSV, $fileText))) {
    echo "Error, the file $filename has not a valid mimetype: " . $filetype["type"];
    continue;
}
1

This is just an idea and not something guaranteed to work, but I have in my experience in the past ran into issues with Strings going between defining as 'string' vs "string".

I notice in the array output it shows it as 'text/csv' where as you have it "text/csv". Again this might not be the issue but figure it is worth a try.

Reading through your code everything seems pretty tight to me logic wise.

You might could even set file types to variables, this way you don't have to type those out over and over since most code editors will suggest/autofill variables. You never know when you will support more types or make new references.

Try setting it to something like this $fileCSV = 'text/csv'; $fileText = 'text/plain'; Then substitute that in your conditional statement.

if ($filetype['type'] != $fileCSV || $filetype['type'] != $fileText) {
  • I'm getting crazy with this, your approach didn't work I spent all the afternoon trying to get where the error is and I can't, any other advice? – ReynierPM Jun 10 '14 at 0:53
  • Reynier, glad you sorted it out! :) – Eric Mayfield Jun 10 '14 at 23:27
0

Why don't you user Wordpress inbuilt functionality for that.

Check Filetype

Retrieve the file type from the file name.

You can optionally define the mime array, if needed.

<?php wp_check_filetype( $filename, $mimes ) ?>

Check Filetype & Extension

Attempts to determine the real file type of a file.

If unable to, the file name extension will be used to determine type. If it's determined that the extension does not match the file's real type, then the "proper_filename" value will be set with a proper filename and extension.

 <?php
      $validate = wp_check_filetype_and_ext( $file, $filename, $mimes );

      if( $validate['proper_filename'] !== false )
          $filename = $validate['proper_filename'];
 ?> 

Reference:

http://codex.wordpress.org/Function_Reference/wp_check_filetype

http://codex.wordpress.org/Function_Reference/wp_check_filetype_and_ext

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.