PHP file upload code example
image upload
Upload file:
//Types that can be uploaded $uptypes=array('image/jpg','image/jpeg','image/png',' image/pjpeg','image/gif', 'image/bmp','image/x-png'); ?>
The file types allowed to be uploaded are: =implode(',',$ uptypes)?>
$max_file_szie=2*pow(2,20); //The uploaded file is less than 2MB $ destination_folder='uploadimg/'; //Upload file saving path if($_SERVER['REQUEST_METHOD']=='POST'){ if(!is_uploaded_file($_FILES['upfile']['tmp_name'])) { echo 'The picture does not exist! '; exit; } if($max_file_szie<$_FILES['upfile']['size']){ echo 'The file is too big! '; exit; } if(!in_array($_FILES['upfile']['type'],$uptypes)){ echo 'The file type does not match! '.$_FILES['upfile']['type']; exit; } if(!file_exists($destination_folder)){ mkdir($destination_folder); } $filename=$_FILES['upfile' ]['tmp_name']; $image_size=getimagesize($filename); $pinfo=pathinfo($_FILES['upfile']['name']); //File path information $ftype=$pinfo[' extension']; //Old file extension $destination = $destination_folder.time().".".$ftype; //New file name if(file_exists($destination)&&$voerwrie !=true){ echo 'The file with the same name already exists! '; exit; } //Move the uploaded file from the temporary folder to the specified directory if(!move_uploaded_file($filename,$destination)){ echo 'An error occurred while moving the file! '; exit; } $pinfo=pathinfo($destination); $fname=$pinfo[basename]; echo "Uploaded successfully < br>File name:
".$destination_folder.$fname." ";
echo 'Width:'.$image_size[0];
echo 'Height:'.$image_size[1];
echo ' Size:'.$_FILES['upfile']['size']."bytes";
}
?>
Copy Code