http://www.w3schools.com/php/php_file_upload.asp
this is my working example.
<?
if($_POST['submit'])
{
if($_FILES['file_up']['name'] == "")//File has a value
{ header("Location:".$config_basedir."loadimage.php?e=1");exit; }
elseif($_FILES['file_up']['size'] == 0)// size is legitimate
{ header("Location:".$config_basedir."loadimage.php?e=2");exit;}
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$filename = stripslashes($_FILES[file_up]['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
header("Location:".$config_basedir."loadimage.php?e=4");exit;
}
//Set maximum file size.
if ($_FILES[file_up]>15000)
{
header("Location:".$config_basedir."loadimage.php?e=3");exit;
}
# Renaming the upload file.
//This line assigns a random number to a variable.
$ran_name = rand () ;
//This takes the random number you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran_name = $ran_name.".";
//This combines the directory, the random file name, and the extension
$newfile_name = $ran_name.$extension;
// the path with the file name where the file will be stored, upload is the directory name.
$add="c://xampp/htdocs/iTraining/image/$newfile_name";
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add))
{
$updsql = "UPDATE customers SET image='".$newfile_name."' WHERE id=".$_SESSION['SESS_USERID'].";";
$updres = mysql_query($updsql);
if(!$updres)
{ die(" Could not query the database ORDERS 3 : <br/>". mysql_error() ); }
header("Location:".$config_basedir."index.php");exit;
}
else
{ header("Location:".$config_basedir."loadimage.php?e=5");}
}
else
{
switch($_GET['e'])
{
case "1":
$error= "<strong> Fields are empty </strong> <br/>";
echo "<br/><div align='center'> ";
show_error($error);
echo "</div><br/>";
break;
case "2":
$error= "<strong> File Size is not Valid </strong> <br/>";
echo "<br/><div align='center'> ";
show_error($error);
echo "</div><br/>";
break;
case "3":
$error= "<strong> Your uploaded file size is more than 150KB so please reduce the file size and then upload. <br/>
Visit the help page to know how to reduce the file size.<BR> </strong> <br/>";
echo "<br/><div align='center'> ";
show_error($error);
echo "</div><br/>";
break;
case "4":
$error= "<strong> Unknown extension! </strong> <br/>";
echo "<br/><div align='center'> ";
show_error($error);
echo "</div><br/>";
break;
case "5":
$error= "<strong> Failed to upload file Contact Site admin to fix the problem </strong> <br/>";
echo "<br/><div align='center'> ";
show_error($error);
echo "</div><br/>";
break;
}
?>
<FORM ENCTYPE="multipart/form-data" ACTION="loadimage.php" METHOD=POST>
<h4>Upload the image:
<INPUT NAME="file_up" TYPE="file">
<INPUT TYPE="submit" name="submit" VALUE="Send File"></h4>
</FORM>
<? } ?>