Home >Backend Development >PHP Tutorial >PHP Beginner - PHP File Upload Tutorial_PHP Tutorial
html form upload code
The following is the php file upload function code
function uploadfile($type,$name ,$ext,$size,$error,$tmp_name,$targetname,$upload_dir)
{
$max_size = 2000000;
$file_mimes = array('image/pjpeg','image/jpeg' ,'image/jpg','image/gif','image/png');
$file_exts = array('.jpg','.gif','.png','.jpg','. gif','.png');
$file_path = $upload_dir.$targetname;
if(!is_dir($upload_dir))
{
if(!mkdir($upload_dir))
die("The file upload directory does not exist and the file upload directory cannot be created");
if(!chmod($upload_dir,0755))
die("The permissions of the file upload directory cannot be set to readable" Writable");
}
if($size>$max_size)
die("The uploaded file size exceeds the specified size");
if($size == 0)
die("Please select the file to upload");
if(!in_array($type,$file_mimes) || !in_array($ext,$file_exts))
die("Please upload files that meet the requirements Type");
if(!move_uploaded_file($tmp_name, $file_path))
die("Failed to copy file, please upload again");
switch($error)
{
case 0:
return ;
case 1:
die("The uploaded file exceeds the value limited by the upload_max_filesize option in php.ini");
case 2:
die("Upload The size of the file exceeds the value specified by the max_file_size option in the html form");
case 3:
die("Only part of the file was uploaded");
case 4:
die("No file Uploaded");
}
}