thinkphp upload image successfully, but database field content is null
Public function upload () {
Import (' ORG.Net.UploadFile ');
$upload = new UploadFile ();//instantiation of upload class
$upload->maxsize = 3145728;//Set attachment upload size
$upload->allowexts = array (' jpg ', ' gif ', ' PNG ', ' jpeg ');//Set attachment upload type
$upload->savepath = './public/uploads/';//Set Attachments upload Directory
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//Upload successfully uploaded file information
$info = $upload->getuploadfileinfo ();
}
Save form data including attachment data
$User = M ("Photo"); Instantiating a User object
$User->create (); Creating Data Objects
$User->photo = $info [0][' Tupian ']; Save uploaded photos assemble yourself as needed
$User->add (); Write user data to database
$this->success (' Data saved successfully! ');
}
How to rename an uploaded image and successfully insert it into the database
Reply to discussion (solution)
$User->add ($info);
It is best to put the last $this->success (' Data saved successfully! ') to get inside the IF statement's else.
Otherwise it seems to be unsuccessful and show success.
I'm a novice too.
Print out the $info to see.
$User->add ($info);
It is best to put the last $this->success (' Data saved successfully! ') to get inside the IF statement's else.
Otherwise it seems to be unsuccessful and show success.
I'm a novice too.
Array (1) {[0]=> Array (8) {["Name"]=> string (7) "444.jpg" ["Type"]=> string (Ten) "Image/jpeg" ["Size"]=> Int (2 502) ["Key"]=> int (0) ["extension"]=> string (3) "JPG" ["Savepath"]=> string "./public/uploads/" ["Savename "]=> string (7)" 444.jpg "[" Hash "]=> string (+)" 9a4e6104c8b37bb4df69e059339545d1 "}}
Print out the $info to see.
Array (1) {[0]=> Array (8) {["Name"]=> string (7) "444.jpg" ["Type"]=> string (Ten) "Image/jpeg" ["Size"]=> Int (2 502) ["Key"]=> int (0) ["extension"]=> string (3) "JPG" ["Savepath"]=> string "./public/uploads/" ["Savename "]=> string (7)" 444.jpg "[" Hash "]=> string (+)" 9a4e6104c8b37bb4df69e059339545d1 "}}
Print out the $info to see.
How can I save Savepath to the database?
By
"Savepath" = "./public/uploads/"
"Savename" = "444.jpg"
Know
$User->photo = substr ($info [0][' Savepath '], 1). $info [0][' Savename '];
By
"Savepath" = "./public/uploads/"
"Savename" = "444.jpg"
Know
$User->photo = substr ($info [0][' Savepath '], 1). $info [0][' Savename '];
Well, thanks for the success.
You helped me solve a lot of my problems.
The great God was worshipped by a young woman
Print out the $info to see.
How can I save Savepath to the database?
Indicates that everything about the path configuration should be placed in the configuration file and should not be saved in the database. Only the file name is stored in the database.
In addition, your file is not renamed, it is easy to see the name of the file is overwritten, it is recommended to look for a unique identifier on the Web function, generate a unique identity filename, and then rename and then insert.