Felayman -- uploads images from PHP to the server. 1. upload_file.php this file is responsible for obtaining the extension of the uploaded image and the randomly generated file name header (content-type: texthtml; charsetutf-8); *** get file extension * Enterdesc 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 "scriptpolicalert('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 ";
}
}
}
?>
Http://www.bkjia.com/PHPjc/815137.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/815137.htmlTechArticle1.upload_file.php // The 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 the file extension * Enter desc...