Jump to content

File extension..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Whitey

Whitey

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
Okay so have a script that edits files.
But i want to restrict it so it only allows it to edit .inc?
I know i have to use a if statement but i dont know how to come about it.

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Untested:
$filename = "config.inc";
$parts = explode(".", $filename);
$size = count($parts);

if($parts[$size-1] == "inc") {
//edit the file
}

Note: Unless you have modified your php.ini file to not display .inc files, its a good idea to use the php extension to prevent them from being displayed in the browser; config.inc.php is a better idea that just config.inc

#3
Whitey

Whitey

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I got it to work with that.. Thanks

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
No problem. :)

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Here are two other functions that will return the proper file extension:
function file_extension($filename)
{
     $path_info = pathinfo($filename);
      return $path_info['extension'];
}

and

function file_extension($filename)
{
    return end(explode(".", $filename));
}

Notice the end() surrounding the explode()?

Quote

end — Set the internal pointer of an array to its last element