ok, Copy paste this code, I've taken your funciton and made some modification. read the commetns they will help explain what is happening.
Just make sure the the directory you are uploading to Exists and has write priviledges
EDIT:
For got to say why the code before didnt work
That was just an example, it didnt do anything other then compare strings to a regular expression.
Basically, programming comes down to... if you dont tell it to do something, it wont do anything. That may seem overly simple, but as you advance you'll find that programming is less to do with code and more to do in how you think of things
<?PHP
// an array of allowed image extensions
$Allowed[]=".gif";
$Allowed[]=".jpeg";
$Allowed[]=".jpg";
$Allowed[]=".bmp";
//// defining a function
function CheckImage($name, $allowedarray){
//// get the last period in the name, so a name like my.image.gif woudl work
//// striipos is a case insensitive functiont that returns the last occurance of a needle in a string
$pos=strripos($name, ".");
/// get the characters that come after the last period
// substr returns the characters in a string given it's parameters
/// so i'm starting at $pos and going to the end of the name
/// strlen returns the amount of charachters in a string
$ext=substr($name, $pos, strlen($name));
/// now we are goign to search the array, and check if it matches one of ours
$find = array_search($ext, $allowedarray);
/// if it was found $find will contain the key that holds the extension, so we return the array and key
/// so we return that value, otherwise return false;
if ($find !== false) return $allowedarray[$find];
else return false;
}
/// this function will check to see if a file exists and if so, add a random number to the beging and return a full target path
function randname($target){
/// check to see if the file exists
if (is_file($target))
{
/// if so cicle through a rand until there is no file with the name;
/// first i'm going to seperate the directories from the name;
$name=basename($target);
//// if I erase the filename from the target I get the directories
$dirs=str_replace($name, "", $target);
//// now I get a 4 digit number;
$rand=rand(1000, 9999);
while(is_file($dirs.$rand."_".$name))
{
$rand=rand(1000, 9999);
}
/// now I have a new name, so I return the new name
return $dirs.$rand."_".$name;
}
/// if the file isnt already set, return the same path
else return $target;
}
/// check to see if any file uploaded
if ((isset($_FILES['uploaded'])) && (is_file($_FILES['uploaded']['tmp_name'])))
{
$target = "upload/";
$name=basename($_FILES['uploaded']['name']);
$target = $target . $name ;
$ok=1;
$uploaded_size=$_FILES['uploaded']['size'];
//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
//// HERE I'm calling the function created, the function will return false if no match is found
/// else it will return the type of extension it matched
/// the function call for 2 variables, 1 being the name beign examined,
//// the other the Array (an array is a variable that holds more then one set of information)
$type=CheckImage($name, $Allowed);
if ($type == false) {
echo "Your file must be either a gif, jpg, jpeg, or bmp<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded<br>";
}
//If everything is ok we try to upload it
else
{
/// seetting up a rand number just in case
//// I always am careful not to replace files already on the server
//// so I do an loop to give it a single random number
/// if you choose to do this uncomment the next line, meaning delete the /// in front of it
//// $target=randname($target);
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".
basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
}
?>