Thinkphp3.1 how to store images and documents uploaded from multiple files in different folders?

Source: Internet
Author: User
Thinkphp3.1 how to store images and documents uploaded from multiple files in different folders? Thinkphp3.1 how to save the uploaded images and documents separately?


Reply to discussion (solution)

Determine the upload directory selection based on the file suffix

Determine the upload directory selection based on the file suffix


$ Upload-> savePath = './Public/image/home/news /';

If (! $ Upload-> upload () {// upload error message
$ This-> error ($ upload-> getErrorMsg ());
} Else {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}
Can I set only one Upload Directory? how can I set two for the first time in the thinkphp Upload class.

Thinkphp3.1 does not provide the corresponding solution, so you need to design it yourself
You can inherit the UploadFile class and override the save method.
You can also use your own Upload class to complete

If you can obtain the file suffix $ type

If ($ type = '.jpg '){
$ Upload-> savePath = './Public/image/home/new1 /';
If (! $ Upload-> upload () {// upload error message
$ This-> error ($ upload-> getErrorMsg ());
} Else {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}
} Else if ($ type = '.txt '){
$ Upload-> savePath = './Public/image/home/new2 /';
If (! $ Upload-> upload () {// upload error message
$ This-> error ($ upload-> getErrorMsg ());
} Else {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}
}

What if I upload an image, a document, and two files?

If you can obtain the file suffix $ type

If ($ type = '.jpg '){
$ Upload-> savePath = './Public/image/home/new1 /';
If (! $ Upload-> upload () {// upload error message
$ This-> error ($ upload-> getErrorMsg ());
} Else {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}
} Else if ($ type = '.txt '){
$ Upload-> savePath = './Public/image/home/new2 /';
If (! $ Upload-> upload () {// upload error message
$ This-> error ($ upload-> getErrorMsg ());
} Else {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}
}

If multiple files are uploaded at the same time, should there be multiple file domains?



Does this determine the file?

If you are willing to modify the UploadFile. class. php file, things will still change.
Put (near line 408) in the getSaveName method)
$ SaveName = $ rule (). ".". $ filename ['extension'];
Change
$ SaveName = $ rule ($ filename). ".". $ filename ['extension'];

Then

$upload->savePath = '';$upload->saveRule = 'myFunction';

Define functions
Function myFunction ($ filename) {$ p = in_array ($ filename ['extension'], array ('GIF', 'jpg ', 'PNG '))? 'Image path': 'document path'; return $ p. $ filename ['name'];}

Because a suffix will be added later, you can modify $ filename ['name'] at will.

If you are willing to modify the UploadFile. class. php file, things will still change.
Put (near line 408) in the getSaveName method)
$ SaveName = $ rule (). ".". $ filename ['extension'];
Change
$ SaveName = $ rule ($ filename). ".". $ filename ['extension'];

Then

$upload->savePath = '';$upload->saveRule = 'myFunction';

Define functions
Function myFunction ($ filename) {$ p = in_array ($ filename ['extension'], array ('GIF', 'jpg ', 'PNG '))? 'Image path': 'document path'; return $ p. $ filename ['name'];}

Because a suffix will be added later, you can modify $ filename ['name'] at will.


Import ('org. Net. uploadfile ');
$ Upload = new UploadFile (); // instantiate the upload class
$ Upload-> maxSize = 3145728;
$ Upload-> savePath = '';
$ Upload-> saveRule = 'myfunction ';
If (! $ Upload-> upload () {// upload error message
$ This-> error ($ upload-> getErrorMsg ());
} Else {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}



Private function getSaveName ($ filename ){
$ Rule = $ this-> saveRule;
If (empty ($ rule) {// if no naming rule is defined, the file name remains unchanged
$ SaveName = $ filename ['name'];
} Else {
If (function_exists ($ rule )){
// Use the function to generate a unique file ID
$ SaveName = $ rule ($ filename). ".". $ filename ['extension'];
} Else {
// Use the given file name as the identification number
$ SaveName = $ rule. ".". $ filename ['extension'];
}
}
If ($ this-> autoSub ){
// Use subdirectories to save files
$ Filename ['savename'] = $ savename;
$ SaveName = $ this-> getSubName ($ filename). $ saveName;
}
Return $ saveName;
}






Function my_filename (){
Return date ('ymdhis ', time ().' _ '. mt_rand ();
}

Function myFunction ($ filename ){
$ P = in_array ($ filename ['extension'], array ('GIF', 'jpg ', 'PNG '))? './Public/image/home/img/': './Public/image/home/file /';
Return $ p. $ filename [my_filename ()];
}




Changed: The Upload path does not exist.

It is empty. I have output the following statement in the upload directory.

Since your./Public/image/home/is Public
$ Upload-> savePath = './Public/image/home /';
Only 'IMG/'is left in the function: 'File /'

Since your./Public/image/home/is Public
$ Upload-> savePath = './Public/image/home /';
Only 'IMG/'is left in the function: 'File /'


The moderator is large and the path can be used, but the name is blank now.
The file already exists !. /Public/image/home/img/. jpg

Return $ p. $ filename [my_filename ()];
Should be
Return $ p. my_filename ();

Direct writing

return $p . date('ymdHis_').mt_rand();

Return $ p. $ filename [my_filename ()];
Should be
Return $ p. my_filename ();

Direct writing

return $p . date('ymdHis_').mt_rand();

Thank you, the moderator. the first time I used the framework, several problems were solved by the moderator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.