Home >Backend Development >PHP Tutorial >The legendary...File upload_PHP tutorial
Image upload in file upload
First, set the format, size (MAX_size), and attributes (type) of the image.
Then use logic to judge.
Finally, create a
form and upload it.Source code:
file.php
print_r($_FILES);
define('MAX_SIZE',100000);
$arr_filetype = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif','application/octet-stream');
if ($_FILES["file"]["error"]>0){
echo "Failed to upload file!";
}
else {
if(!in_array($_FILES["file"]["type"],$arr_filetype)){
echo "<script>alert('The file format is incorrect! Please re-upload!');history.back();</script>";
exit;
}
/*switch($_FILES["file"]["type"]){
case 'image/jpeg':break;
case 'image/pjpeg':break;
case 'image/gif':break;
case 'image/png':break;
case 'image/x-png':break;
default:echo "<script>alert('The file format is incorrect! Please re-upload!');history.back();</script>";
} */
else{
if($_FILES["file"]["size"]>100000){
echo "<script>alert('The file is too large! Please re-upload!');history.back();</script>";
exit;
}
}
if(move_uploaded_file($_FILES["file"]["tmp_name"],"d:/wamp/tmp/".time().$_FILES["file"]["name"])){
echo "File uploaded successfully!";
}
}
file.html