1. upload_file.php
// Obtain the extension of the uploaded image and the randomly generated file name.
Header ("content-type: text/html; charset = UTF-8 ");
/**
* Get the file extension
* Enter description here...
*@ ParamUnknown_type $ filename
*/
Function getFileName ($ filename ){
// Strrchr-find the last occurrence of a specified character in the string
Return substr (strrchr ($ filename, '.'), 1 );
}
/**
* Random generation of n-bit strings
* Enterdescription here...
*@ ParamUnknown_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 ");
// Introduce the Function lib for obtaining the name of the uploaded file that has generated the file name
Require 'upload _ file. php ';
// Set the directory to save the file
$ Upload_dir = "files /";
If (! File_exists ($ upload_dir )){
Mkdir ($ upload_dir );
}
// Set the file type for running upload
$ Type = array ('jpg ', 'png', 'gif', 'jpeg ');
// In_array-check whether a value exists in the array
If (! In_array (strtolower (getFileName ($ _ FILES ['file'] ['name']), $ type )){
// Implode, which combines the array into a string
$ Text = implode (',', $ type );
Echo "<script> alert('file names are only allowed when text='{}}location='index.html '; </script> ";
} Else {
// Obtain the file name
$ Filename = explode ('.', $ _ FILES ['file'] ['name']);
$ Filename [0] = rand_str (10 );
$ Name = implode ('.', $ filename );
Bytes;
// Is_uploaded_file-determines whether the file is uploaded through http post.
If (is_uploaded_file ($ _ FILES ['file'] ['tmp _ name']) {
// Move_uploaded_file-move the uploaded file to the new location
If (move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], http://blog.csdn.net/u012332735/article/details/javasuploadfile )){
$ File_path = getcwd (). '\'. http://blog.csdn.net/u012332735/article/details/$uploadfile; echo"
After uploading your file, upload the image to preview it: ";
Echo"
";
Echo "Continue upload ";
} Else {
Echo "Upload Failed ";
}
}
}
?>