note 92025 added to features.file-upload
| From: | alan at projecthuddle dot com | Date: | Mon, 06 Jul 2009 09:09:46 +0000 |
| Subject: | note 92025 added to features.file-upload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157952@lists.php.net to get a copy of this message | ||
I've looked for an easy way to tell if there is a CSV extension. In the end I used:
$filename = $_FILES['userfile']['name'];
$filename = strtolower($filename) ;
$returns = strrchr($filename,".csv");
if ($returns != ".csv"){
echo"fail";
}
So this
gets the file name
converts it to lowercase
finds the first occurance of .csv and then returns that and everything after it
I then check that $returns only includes .csv
If the file name had filename.csv.inc $returns would be .csv.inc and so it would fail.
Only those with CSV extision pass this test. It'd be pretty easy to adjust the function to do
multiple file types. I haven't tried it but something like this should work:
$filename = $_FILES['userfile']['name'];
$filename = strtolower($filename) ;
$returns = strrchr($filename,".");
switch ($returns )
{
case ".txt":
echo "txt found";
break;
case ".csv":
echo "csv found";
break;
case ".png:
echo "png found";
break;
default:
echo "No valid file format found";
}
----
Server IP: 69.147.83.197
Probable Submitter: 86.14.190.32
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.php
Edit -- https://master.php.net/note/edit/92025
Del: integrated -- https://master.php.net/note/delete/92025/integrated
Del: useless -- https://master.php.net/note/delete/92025/useless
Del: bad code -- https://master.php.net/note/delete/92025/bad+code
Del: spam -- https://master.php.net/note/delete/92025/spam
Del: non-english -- https://master.php.net/note/delete/92025/non-english
Del: in docs -- https://master.php.net/note/delete/92025/in+docs
Del: other reasons-- https://master.php.net/note/delete/92025
Reject -- https://master.php.net/note/reject/92025
Search -- https://master.php.net/manage/user-notes.php