473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Function to check file extension not working

Anybody please help me! The function always return false although file extension is supported. Here my code;

[PHP]function checkExtension($extension)
{
$valid_ext = array('3gp','aac','avi','bmp','doc','flv','gif','g z','gzip','jpeg','jpg','mov','mp3','mpeg','mpg','p df','png','ppt','rar','tar','tif','txt','wav','wmv ','zip');

foreach($valid_ext as $ext)
{
if($extension==$ext)
{
return true;
break;
}
else
{
return false;
}
}
}

if(!checkExtension('doc'))
{
echo 'File not suppoted!';
}
else
{
echo 'File supported!';
}[/PHP]
Sep 29 '07 #1
2 1701
it's ok i have been got the solution by using in_array function as shown below;

[PHP]$valid_ext = array('3gp','aac','avi','bmp','doc','flv','gif','g z','gzip','jpeg','jpg','mov','mp3','mpeg','mpg','p df','png','ppt','rar','tar','tif','txt','wav','wmv ','zip');
if(!in_array('abc',$valid_ext))
{
echo 'File not supported!';
}
else
{
echo 'File supported!';
}[/PHP]
Sep 29 '07 #2
pbmods
5,821 Expert 4TB
Heya, Adam.

A more elegant and efficient solution:
Expand|Select|Wrap|Line Numbers
  1. $_validExtensions =
  2.     array
  3.     (
  4.         '3gp' => true,
  5.         'aac' => true,
  6.         .
  7.         .
  8.         .
  9.     );
  10.  
  11. return ( ! empty($_validExtensions[$ext]) );
  12.  
Searching an array by value takes O(n), whereas searching by key takes O(1)
Sep 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Philip D Heady | last post by:
Ok, here's my code. Can't get it to check file extension properly or file size...had it working before but not sure why it's buggy now. } elseif ($photo) { $ext = strtolower(substr($photo,...
9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: Biffo | last post by:
I have PHP Version 5.0.3 installed on Windows XP Pro with IIS as the webserver. All is working well, except I get a - Fatal error: Call to undefined function mssql_connect() in...
6
by: Puzzled | last post by:
This is a weird problem, but perhaps someone else has seen it before (I hope!) If I use a fully qualified include call include ( 'http://localhost/subtree/filename.php') I get an 'undefined...
1
by: drewmania001 | last post by:
i've read various info on the web including the following http://php.mirrors.ilisys.com.au/manual/en/ref.mysqli.php mySQL Version 4.1.16 PHP Version 5.1.2 with Zend Engine v2.1.0 OS Windows...
3
by: adamjblakey | last post by:
Hi, I want to convert this into a function. How would i go about doing this? $path_thumbs = "images/thumbs"; $path_big = "images/big"; //the new width of the resized image....
8
by: adamjblakey | last post by:
Hi, I am trying to build a function and from a script but it does not seem to be working. Can anyone see anything wrong with this? Also i need it to convert the large image to a specific size...
1
by: Chris Rebert | last post by:
On Sat, Sep 27, 2008 at 3:42 PM, Michael Crute <mcrute@gmail.comwrote: Looking at the docs for the mimetypes module, it just guesses based on the filename (and extension), not the actual contents...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.