1.upload_file.php
//This file is responsible for obtaining the extension of the uploaded image and randomly generating the file name
header("content-type:text/html;charset=utf-8");
/**
* Get file extension
*Enter description here ...
* @param unknown_type $filename
*/
function getFileName($filename){
//strrchr—Find the last occurrence of the specified character in the string
return substr(strrchr($filename,'.'), 1);
}
/**
* Randomly generate n-digit string
* Enterdescription here ...
* @param unknown_type $num
*/
function rand_str($num){
$str = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKOLP1234567890";
$str_len = strlen($str)-1;
//echo$str_len;
$s='';
for ($i = 0; $i
$s.=$str[rand(0,$str_len)];
}
echo $s;
}
?>
2.index.html
3.deal_upload.php
header("content-type:text/html;charset=utf-8");
//Introduce the function lib
to get the name of the uploaded file and generate the file namerequire 'upload_file.php';
//Set the directory where the file is to be saved
$upload_dir = "files/";
if(!file_exists($upload_dir)){
mkdir($upload_dir);
}
//Set the file type to be uploaded
$type =array('jpg','png','gif','jpeg');
//in_array — Check if a value exists in the array
if(!in_array(strtolower(getFileName($_FILES['file']['name'])), $type)){
//implode, combine arrays into a string
$text = implode(',', $type);
echo "<script>alert('The file type is only allowed to be {$text}');window.location='index.html';</script>";
}else{
//Get file name
$filename = explode('.', $_FILES['file']['name']);
$filename[0] =rand_str(10);
$name =implode('.', $filename);
http://blog.csdn.net/u012332735/article/details/$uploadfile=$upload_dir.$name;
//is_uploaded_file — Determine whether the file was uploaded via HTTP POST
if(is_uploaded_file($_FILES['file']['tmp_name'])){
//move_uploaded_file — Move the uploaded file to a new location
if(move_uploaded_file($_FILES['file']['tmp_name'],http://blog.csdn.net/u012332735/article/details/$uploadfile)){
$file_path = getcwd().'\'.http://blog.csdn.net/u012332735/article/details/$uploadfile; echo "
echo "
";
echo "Continue uploading";
}else{
echo "Upload failed";
}
}
}
?>