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!