1.upload_file.php
This file is responsible for getting the uploaded image extension and randomly generated file name
Header ("Content-type:text/html;charset=utf-8");
/**
* Get file name extension
*enter description here ...
* @param unknown_type $filename
*/
function GetFileName ($filename) {
Strrchr-finds the last occurrence of a specified character in a string
Return substr (STRRCHR ($filename, '. '), 1);
}
/**
* Random generation of n-bit strings
* 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 < $num; $i + +) {
$s. = $str [rand (0, $str _len)];
}
Echo $s;
}
?>
2.index.html
3.deal_upload.php
Header ("Content-type:text/html;charset=utf-8");
Introduces the function lib that gets the name of the uploaded file that has generated the filename
Require ' upload_file.php ';
Set the directory to save the file
$upload _dir = "files/";
if (!file_exists ($upload _dir)) {
mkdir ($upload _dir);
}
Set up file types to run uploads
$type =array (' jpg ', ' png ', ' gif ', ' jpeg ');
in_array-checks if a value exists in the array
if (!in_array (Strtolower (GetFileName ($_files[' file ' [' name '])), $type)) {
Implode, union the array into a string
$text = Implode (', ', $type);
echo "";
}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-to determine if the file was uploaded via HTTP post
if (is_uploaded_file ($_files[' file ' [' Tmp_name '])) {
Move_uploaded_file-moving uploaded files 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 "
After uploading your file, upload the image preview: ";
echo "
";
echo "Continue uploading";
}else{
echo "Upload failed";
}
}
}
?>
http://www.bkjia.com/PHPjc/815137.html www.bkjia.com true http://www.bkjia.com/PHPjc/815137.html techarticle 1.upload_file.php//The file is responsible for obtaining the uploaded image extension and randomly generated filename header ("Content-type:text/html;charset=utf-8");/** * Get File extension * Enter desc ...