At first we have to do this following HTML form.
<form action=” ” method=”post” enctype=”multipart/form-data”>
<input type=”file” name=”myupload” id=”myupload”>
<input type=”submit” name=”submit” value=”fileupload”>
</form>
In this above HTML code we have mentioned the parameter enctype=”multipart/form-data” because this parameter is necessary for file uploading.If you do not write this parameter you can not be able to upload the file.
Here is the PHP code to upload the file in a directory:
<?php
$tmp_file_name = $_FILES[‘myupload’] [‘tmp_name’];
$file_name = $_FILES[‘myupload’] [‘name’];
echo $tmp_file_name.'<br>’; /* For debugging you can use this code */
echo $file_name.'<br>’; /* For debugging you can use this code */
$path= “fileuploaddirectory”;
$Myupload =$path.”/”.$file_name;
if(move_uploaded_file($tmp_file_name, $Myupload))
{
echo “file uploaded successfully”;
}
?>