Jump to content

Using ereg() in file upload script


jaArch

Recommended Posts

Hi:

 

Is it possible to use the ereg() function in a file upload script so that the ereg() can recognize whether or not the file type is a .jpg, .gif, .png?

 

I already figured out how to do it without using the ereg()  function - but I'm just interested to know if it is possible using ereg()..if so, could you provide me with an example? I'm also a bit confused as to how to use ereg() to recognize file types.

 

Here;s what I've done:

 

function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

if ($image)
{

$filename = stripslashes($_FILES['image']['name']);

$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{

echo '<h1>Unknown extension!</h1>';
$errors=1;
}

 

Thanks!

Link to comment
Share on other sites

1)  Don't bump, it's against the rules.

 

2)  ereg is deprecated, preg is better.

 

3)  preg can be used to see if the fileNAME ends in a specific three-letter combination, but that doesn't mean anything.  All imgur posts are .jpg regardless of their actual filetype.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.