php how to upload file code example
Example 1: basic code for file upload in php
<!DOCTYPE html>
<html>
<head>
<title>ImageUpload</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Username</label>
<input type="text" name="username">
<br>
<label>UploadImage</label>
<input type="file" name='myfile'>
<br/>
<input type="submit" value="upload">
</form>
</body>
</html>
<?php
$user=$_POST['username'];
$image=$_FILES['myfile'];
echo "Hello $user <br/>";
echo "File Name<b>::</b> ".$image['name'];
move_uploaded_file($image['tmp_name'],"photos/".$image['name']);
?>
Example 2: php upload
if(upload("FILENAME", array("jpeg","jpg","png"), 209715, "C:/xampp/htdocs/")){
echo "Success";
}
function upload($f_name, $f_ext_allowed, $f_maxsize, $f_path){
$f_name_2 = $_FILES[$f_name]['name'];
$f_size = $_FILES[$f_name]['size'];
$f_tmp = $_FILES[$f_name]['tmp_name'];
$f_error = $_FILES[$f_name]['error'];
$f_ext = strtolower(end(explode('.',$f_name_2)));
$f_rename = $_SESSION['uid'] . "." . $f_ext;
if($f_error == 0 && in_array($f_ext, $f_ext_allowed)
&& $f_size < $f_maxsize && mb_strlen($f_name_2, "UTF-8") < 225
&& preg_match("`^[-0-9A-Z_\.]+$`i", $f_name_2)){
if(move_uploaded_file($f_tmp, $f_path . $f_name_2){
return true;
}else{
return false;
}
}else{
return false;
}
}
Example 3: how to upload image in php
<!DOCTYPE html><html><body><form action="upload.php" method="post"
enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit">
</form></body></html>