Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

String manipulation

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

String manipulation

Postby php-punk » Tue Jul 22, 2003 5:28 am

k this is probably tho most of simple things that can be done with PHP but..

how would one go find out teh file extension of a file, as i am going to create a FTP to upload pictures to my site, and i would like only pictures and nothing else to be uploaded, can anybody help?
php-punk
New php-forum User
New php-forum User
 
Posts: 60
Joined: Sun Feb 02, 2003 2:10 am
Location: Leeds, Uk

Postby Oleg Butuzov » Tue Jul 22, 2003 6:30 am

Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby php-punk » Tue Jul 22, 2003 7:16 am

right, got that bit out of the way..

but n e ideas on why this does work::
Code: Select all
<?
//in this case this is pre- defined!!
$file ="rah.exe";
$ext = explode(".", $file);

$count = count($ext);
if (!$ext[$count] = "jpeg"||"gif"||"png"){
   echo ("sorry invalid file");
   }
else{
echo("::Upload Script::");
   }
?>


it always goes straigh to upload script[/code]
php-punk
New php-forum User
New php-forum User
 
Posts: 60
Joined: Sun Feb 02, 2003 2:10 am
Location: Leeds, Uk

Postby swirlee » Tue Jul 22, 2003 7:50 am

php-punk wrote:right, got that bit out of the way..

but n e ideas on why this does work::
Code: Select all
<?
//in this case this is pre- defined!!
$file ="rah.exe";
$ext = explode(".", $file);

$count = count($ext);
if (!$ext[$count] = "jpeg"||"gif"||"png"){
   echo ("sorry invalid file");
   }
else{
echo("::Upload Script::");
   }
?>


it always goes straigh to upload script[/code]


Several reasons.

FIrst, PHP (and, in fact, nearly all programming languages) starts counting at 0. So if you have an array with two elements, they'll have indexes 0 and 1. But count() doesn't return the highest index, it returns the number of elements, so it would return 2. And since the highest index in your array is 1, $array[2] doesn't have a value. I recommend using count($array) - 1.

Second, the operator for equals is ==, not =. Not to mention you should be using != (as in $a != $b) intead of !$a == $b. This will alleviate a lot of operator precedece issues.

Third, your comparison won't work. You cannot string together a bunch of boolean operators and expect them to work. PHP does not work like natural language. You can't just say "A is equal to B or C or D". Instead, you have to be very specific: "(A is equal to B) or (A is equal to C) or (A is equal to D)". A working version of your code (significantly adjusted for readability) would look more like this:

Code: Select all
<?
$filename ='rah.exe';
$filename_parts = explode('.', $filename);

$extension = $filename_parts[count($filename_parts) - 1];
if(   $extension == 'jpg'  ||
      $extension == 'jpeg' ||
      $extension == 'gif'  ||
      $extension == 'png'
) {
   echo '::Upload Script::';
} else {
   echo 'Sorry, invalid file.';
}
?>


I might offer another suggestion -- if you have the GD library installed, use getimagesize() to retrieve the filetype. It'll read the actual file headers rather than just the filename.

Also useful if you've a number of different filetypes other than image files (or don't have GD installed), you can just dump them into an array and use in_array() to see if it's an allowable filetype:

Code: Select all
<?
$allowed_types = array(
   'jpg',
   'jpeg',
   'gif',
   'png'
)

$extension = $filename_parts[count($filename_parts) - 1];
if(in_array($extension, $allowed_types)) {
   echo '::Upload Script::';
} else {
   echo 'Sorry, invalid file.';
}
?>
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.