Jump to content

[SOLVED] getting part of a string


cha0sriderx

Recommended Posts

<?php
$image="new.gif";
if (exif_imagetype($image) == IMAGETYPE_GIF) {
    echo 'The picture is a gif';
}
elseif(exif_imagetype($image) == IMAGETYPE_JPEG) {
    echo 'The picture is a jpeg';
}
elseif(exif_imagetype($image) == IMAGETYPE_PNG) {
    echo 'The picture is a png';
}
else
{
echo "Image type unknown";
}

?>

 

This is just an example.

You can use case statements to be more appropriate.

 

If all you want is the extension of the file:

<?php
  $file = "some.file.txt";
  $tArr = explode(".", $file);
  $ext = $tArr[count($tArr) - 1];

  $new_name = rand(5, 10) . $ext;
  echo $file . "<br>" . $new_name . "<br>";
?>

 

Bear in mind however, that the extension of the file has no bearing on the type of file.  You need to use another mechanism to validate that a file is the type you are expecting, such as an image, plain text, etc.

i have an array that checks and makes sure the file is a valid image file.

 

What do you mean?  Are you extracting the extension and just making sure it exists in Array( "gif", "jpg", "jpeg", etc. )?

 

That's exactly what I said you shouldn't be doing.

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.