thinkphp upload image successfully, but database field content is null

Source: Internet
Author: User
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.

  • Related Article

    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.