thinkphp3.1 upload pictures and documents, how to save separately?
Reply to discussion (solution)
According to the file suffix, determine the upload directory selection
According to the file suffix, determine the upload directory selection
$upload->savepath= './public/image/home/news/';
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
This upload directory is not only set one, how to set two, thinkphp upload class for the first time.
thinkphp3.1 does not provide the appropriate solution, so you need to design your own
You can inherit the UploadFile class, overriding the Save method
You can also use your own upload class to complete
If you can get the file suffix $type
if ($type = = '. jpg ') {
$upload->savepath= './public/image/home/new1/';
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
}else if ($type = = '. txt ') {
$upload->savepath= './public/image/home/new2/';
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
}
What if I upload a picture of a document with 2 files?
If you can get the file suffix $type
if ($type = = '. jpg ') {
$upload->savepath= './public/image/home/new1/';
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
}else if ($type = = '. txt ') {
$upload->savepath= './public/image/home/new2/';
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
}
If you upload multiple files at the same time, should there be more than one file domain?
So that's not the file judgment?
If you're willing to change the UploadFile.class.php file, there's still a turnaround.
Put the Getsavename method in the (near line 408)
$saveName = $rule (). ".". $filename [' extension '];
Change into
$saveName = $rule ($filename). ".". $filename [' extension '];
And then
$upload->savepath = '; $upload->saverule = ' myFunction ';
Defining functions
function MyFunction ($filename) { $p = In_array ($filename [' extension '], array (' gif ', ' jpg ', ' png '))? ' Picture path ': ' Document path '; Return $p. $filename [' name '];}
Since the suffix is added later, $filename [' name '] How to deform you feel free to
If you're willing to change the UploadFile.class.php file, there's still a turnaround.
Put the Getsavename method in the (near line 408)
$saveName = $rule (). ".". $filename [' extension '];
Change into
$saveName = $rule ($filename). ".". $filename [' extension '];
And then
$upload->savepath = '; $upload->saverule = ' myFunction ';
Defining functions
function MyFunction ($filename) { $p = In_array ($filename [' extension '], array (' gif ', ' jpg ', ' png '))? ' Picture path ': ' Document path '; Return $p. $filename [' name '];}
Since the suffix is added later, $filename [' name '] How to deform you feel free to
Import (' ORG.Net.UploadFile ');
$upload = new UploadFile ();//instantiation of upload class
$upload->maxsize = 3145728;
$upload->savepath = ";
$upload->saverule = ' myFunction ';
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
Private Function Getsavename ($filename) {
$rule = $this->saverule;
if (empty ($rule)) {//does not have a naming convention defined, the file name remains the same
$saveName = $filename [' name '];
}else {
if (function_exists ($rule)) {
Use a function to generate a unique file identification number
$saveName = $rule ($filename). ".". $filename [' extension '];
}else {
Use the given file name as the identification number
$saveName = $rule. ".". $filename [' extension '];
}
}
if ($this->autosub) {
Saving files using subdirectories
$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, these three places, the prompt upload path does not exist.
is empty, I upload the directory does not exist in the previous sentence output.
Since your./public/image/home/is public, then
$upload->savepath = './public/image/home/';
Only ' img/' is left in the function: ' file/'
Since your./public/image/home/is public, then
$upload->savepath = './public/image/home/';
Only ' img/' is left in the function: ' file/'
Moderator big, the path can be used, but now is the name is empty.
File already exists!./public/image/home/img/.jpg
Return $p. $filename [My_filename ()];
should be for
Return $p. My_filename ();
Can write directly
Return $p. Date (' Ymdhis_ '). Mt_rand ();
Return $p. $filename [My_filename ()];
should be for
Return $p. My_filename ();
Can write directly
Return $p. Date (' Ymdhis_ '). Mt_rand ();
NN, thank you moderator greatly, the first time with the framework, a number of issues are moderator greatly resolved.