Php implements file downloading to a specified folder, saving the file path or name to the database

Source: Internet
Author: User
Php allows you to upload files to a specified folder, store the file path or name to the database. how can this problem be solved? Please help ., HTMLcode & lt; formaction = & quot; method = & quot; post & quot; enctype = & quo php uploads files to a specified folder, and stores the file path or name in the database, how to implement it?
Do you have any experts? Help me.

------ Solution --------------------
HTML code

------ Solution --------------------
Complete database operations:
PHP code
    
------ Solution --------------------
I want
PHP code
Define (UPLOAD_ROOT, 'Upload folder path'); function fake_random_name ($ string, $ key) {# Pseudo-code generates a pseudo-random file name based on the user name and upload time as the uploaded User name # The decoding condition is that the user name and upload time of the file owner can be determined based on the file name # required the user's key can be extracted from the database for decryption. The key is the first five characters of the user name. # for specific encryption and decryption code implementation, refer to the N methods under the Mcrypt extension of the PHP Standard Extension Library. return $ fake_random_name ;} if (! Is_writbale (UPLOAD_ROOT) {die ('you need chown the dir for your uploading file, make it writable ~ ');} Else {move_uploaded_file ($ _ FILES ['form name from POST'] ['tmp _ name'];/*** this method will automatically call is_upload_file () method to check whether the FILE is uploaded by the valid http rfc1867 protocol * Of course, you can also limit whether it is the legal FILE type you need $ _ FILE [$ _ POST [name] [type], for example, whether it is pdf imgfiles can all be */$ dbh = new mysqli ('localhost ', 'root', 'sa ', 'Your _ db_name '); # The generated file can include the DSN and put it outside the root directory to protect the connection file $ sec_key = substr ($ _ SESSION ['loginname ); # key $ file_name = fake_random_name ($ _ FILES ['form name from POST '] ['tmp _ name'], $ sec_key ); # encrypted name $ q_str = "insert into tb (owner, location) values ($ _ SEESION ['loginname'], UPLOAD_ROOT.DIRECTORYSLASH. $ file_name) ";/SQL $ dbh-> query ($ q_str); if ($ dbh-> num_rows () = 1) echo" Upload successful, details of the inserted database file (encrypted )";}
------ Solution --------------------
Set the file storage path, upload the last file through the move_uploaded_file function, and insert the uploaded file name to the database.
------ Solution --------------------
Learning and splitting

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.