uploading single file in php

To upload a single file, the following code is useful. NOTE: you just need to fix the path of $uploaddir

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<?php

if( isset($_POST['btnsubmit']) )
{
//print_r($_FILES);

$uploaddir = 'A:/AAA/www/web/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
{
    echo "File is valid, and was successfully uploaded.\n";
} 
else 
{
    echo "File upload failed!\n";
}

}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
  <input type="file" name="userfile" id="userfile"> <br>
  <input id="btnsubmit" name="btnsubmit" type="submit" value="Upload">
</form>
</body>
</html>

 

Add a Comment

Your email address will not be published. Required fields are marked *