In THINKPHP5: There is a chapter to upload files:
https://www.kancloud.cn/manual/thinkphp5/155159
If the file upload this chapter, do not understand, first look at the following, and then look at this paragraph.
Here we customize two rules.
1.datea rules, Month/time and seconds. (3-bit random number)
/201706/164030789
2.YMMD5 rules, Year/month/MD5
Modify the Framework file: thinkphp/library/think/file.php.
Increase of two case:
Case ' Datea ':
Case ' YMMD5 ':
That's all you can do.
/** * get save file name * @param string|bool $savename saved filenames default auto generate * @return string */protected function buildsavename ($ Savename) { if (true === $savename) { // automatically generate file name if ($this->rule Instanceof \closure) { $ Savename = call_user_func_array ($this->rule, [$this]); } else { switch ($this->rule) { case ' Date ': $savename = date (' Ymd ') . ds . md5 (Microtime (true)); break; case ' Datea ': $savename = date (' Ym ') . ds . date (' his ') . Rand (100,999)); break; case ' Ymmd5 ': $savename = date (' Y ') . ds . Date (' m ') . ds. $This->hash (' MD5 '); break; default: if (In_array ($this->rule, hash_algos ())) { $hash = $this->hash ($this->rule); $savename = substr ($hash, 0, 2) . ds . substr ($hash, 2); } elseif (is_callable ($this->rule)) { $ Savename = call_user_func ($this->rule); } else { $savename = date (' Ymd ') . ds . md5 (Microtime (true)); } } } } elseif (" === $savename) { $savename = $this->getinfo (' name '); } if (! Strpos ($savename, '. ')) { $savename .= '. ' . pathinfo ($this->getinfo (' name '), pathinfo_extension); } return $savename;}
If you do not understand the file upload chapter, look at the following first.
Suppose the form code is as follows:
<form action= "/index/index/upload" enctype= "Multipart/form-data" method= "POST" ><input type= "file" Name= " Image "/> <br> <input type=" Submit "value=" Upload "/> </form>
Then add the following code to the controller:
Public function upload () { // get form upload file For example, uploaded 001.jpg $file = request ()->file (' image '); // move to the Framework app root directory/public/uploads/ $info = $ File->move (root_path . ' public ' . DS . ' uploads '); if ($info) { // After successful upload get upload information // output jpg echo $info->getextension (); // Output 20160820/ 42a79759f284b767dfcb2a0197904287.jpg echo $info Getsavename (); // Output 42a79759f284b767dfcb2a0197904287.jpg echo $info->getfilename (); }else{ // upload failed get error message echo $file->geterror (); }}
move
If the method succeeds, it returns an \think\File
object that you can follow up on the uploaded file.
Multiple file uploads
If you are using a multi-file upload form, for example:
<form action= "/index/index/upload" enctype= "Multipart/form-data" method= "POST" ><input type= "file" Name= " image[] "/> <br> <input type=" file "Name=" image[] "/> <br> <input type=" file "Name=" image[] "/> ; <br> <input type= "Submit" value= "Upload"/> </form>
The controller code can be changed to:
Public function upload () { // get form upload file $files = request ()->file (' image '); foreach ($files as $file) { // move to the Framework app root directory/ public/uploads/ $info = $file->move (root_ path . ' public ' . DS . ' uploads '); if ($info) { // after successful upload Get upload information // output jpg echo $info->getextension (); // Output 42a79759f284b767dfcb2a0197904287.jpg echo $info->getfilename (); }else{ // upload failed get error message echo $ File->geterror (); } }}
Upload Rules
By default, a file with the current date as a subdirectory, encoded as a file name in microseconds, is generated under the upload directory md5
, for example the file name generated above may be:
/home/www/upload/20160510/42a79759f284b767dfcb2a0197904287.jpg
We can specify the naming rules for uploading files, using the rule
method, for example:
Get form upload file such as upload 001.jpg$file = Request ()->file (' image '),//move to the server's upload directory and use MD5 rules $file->rule (' MD5 ')->move (' /home/www/upload/');
The resulting file name is similar to the following:
/home/www/upload/72/ef580909368d824e899f77c7c98388.jpg
Several upload naming conventions are provided by default, including:
rules |
Description |
Date |
Generated based on date and number of microseconds |
Md5 |
Using md5_file hash generation for files |
Sha1 |
Using Sha1_file hash generation for files |
Where the MD5 and SHA1 rules automatically take the first two characters of the hash value as subdirectories, followed by the hash value as the file name.
If you need to use a custom naming convention, you can pass in a rule
method to a function or method, for example:
Get form upload file such as upload 001.jpg$file = Request ()->file (' image '),//move to the server's upload directory and use uniqid rules $file->rule (' uniqid ') Move ('/home/www/upload/');
The resulting file name is similar to the following:
/home/www/upload/573d3b6d7abe2.jpg
If you want to keep the original file name, you can use:
Get the form upload file such as upload 001.jpg$file = Request ()->file (' image '),//move to the server's upload directory and use the original filename $file->move ('/home/www/upload/ ‘,‘‘);
By default, files with the same name are overwritten by the server's upload directory and can be used if you do not want to overwrite them:
Get form upload file such as upload 001.jpg$file = Request ()->file (' image '),//move to the server's upload directory and set not to overwrite $file->move ('/home/www/upload/' , True,false);
---------- recruit the future great God -----------------------
If you have altruistic heart, willing to help others, willing to share
If you encounter PHP problem, Baidu and asked the other groups still did not get answers
Welcome to join, PHP technical question and answer group, QQ Group: 292626152
Educational developments Help others, you will also get promotion!
In order to cherish everyone's valuable time, please do not chat.
May we help each other and grow together!
Add message code, PHP,AJAX,THINKPHP,YII ...
Custom naming rules for uploading files in THINKPHP5