PDA

View Full Version : function to return file extension characters


songboy
10-06-2009, 11:52 PM
Could someone tell me why this isn't working. I was expecting it would echo jpg but actually the echo delivers nothing.

function getFileExtension($str)
{
$i = strrpos($str,”.”); // $i will be the numeric position of the dot in the string eg position 4//
if (!$i)
{
return;
}
$l = strlen($str) - $i; // $l will always equal 3 (the length of the string eg 7 - $i (4) = 3//
$ext = substr($str,$i+1,$l);//the file extension (eg go to position 4)
($i+1) and target 3 characters starting with position 5)//
return $ext; //So, $ext = the characters representing the file extension.//
}
@$fileName = $_FILES['writer_photo_file']['name'];
@$tmpName = $_FILES['writer_photo_file']['tmp_name'];
@$fileSize = $_FILES['writer_photo_file']['size'];
@$fileType = $_FILES['writer_photo_file']['type'];
$upload_destin = 'c:\wamp\www\Local Root\another\writer_photo_image'."//".$fileName;
if(isset($_POST['Submit']))
{
if($_FILES['writer_photo_file']['tmp_name'] =="none")
{
$photo_message="No upload - try a different photo.";
include("add_the_songs.php");
exit();
}
}
if(!ereg("image",$_FILES['writer_photo_file']['type']))
{
$message_not_photo ="File is not a picture. Please try another file.";
include("add_the_songs.php");
exit();
}
$fileName = stripslashes($_FILES['writer_photo_file']['name']);// Take out the slashes before using the function .//
@$extension = strtolower(getFileExtension($fileName)); // Make sure the extension is lower case.
echo($extension);

I'm not getting any error message.
Thanks for any ideas.
Songboy

bee80
10-07-2009, 07:40 AM
much simpler to make an array of allowed file formats :


$allowed = array ('image/pjpeg','image/jpeg','image/jpg','image/JPG','image/X-JPG','image/x-jpg','image/jpeg','image/X-PNG','image/PNG','image/png','image/x-png');


and then check if file type is in array:


$fileType = $_FILES['writer_photo_file']['type'];

if(in_array($fileType,$allowed)) {

//do this if an image extension

}else{

//if its not a image extension do this
$message_not_photo ="File is not a picture. Please try another file.";
include("add_the_songs.php");
exit();

}



in your function your returning $ext not $extension

return $ext; //So, $ext = the characters representing the file extension.//

your not echoing this also where are you setting the $str variable?

is this all on 1 line because you have 2 lots of comment slashes on the 1st line so it would comment out the @$extension part if its on 1 line:

$fileName = stripslashes($_FILES['writer_photo_file']['name']);// Take out the slashes before using the function .//
@$extension = strtolower(getFileExtension($fileName)); // Make sure the extension is lower case.

bee80
10-07-2009, 04:23 PM
also ive never seen a variable echo'd like this , maybe im wrong but it doesnt look right:

echo($extension);


ive seen variables echo'd with curly braces(inside a string) and no braces, i personally use the later:

echo {$extension};
echo $extension;