Php beginners use file Upload instances

Source: Internet
Author: User

Form part:
The code is as follows:

<Html>
<Head>
<Title>
My is upfile app !!
</Title>
<Meta http-equiv = "content-type" content = "text/html; charset = utf-8"/>
</Head>
<Body>
<Form enctype = "multipart/form-data" method = "post" action = "upfile_add.php tutorial">
File name: <input type = "text" name = "title"/>
Upload file: <input type = "file" name = "file"/>
<Input type = "submit" vlaue = "submit"/>
</Form>
</Body>
</Html>

2. Process the form page:
The code is as follows:

<? Php
$ Mkdir_file_dir = mkdir ('. /img /'. $ _ post ['title'], 0777); // obtain the title and create a folder under the final directory to store the file specified by the category.
$ Tmp_file_name = $ _ files ['file'] ['tmp _ name']; // Get the uploaded temporary file
$ File_name = $ _ files ['file'] ['name']; // source file
$ File_dir = './img/'. $ _ post ['title']. '/'; // Finally save the Directory
If (is_dir ($ file_dir ))
{
Move_uploaded_file ($ tmp_file_name, $ file_dir. $ file_name); // move the file to the final save Directory
$ Img_url = $ file_dir. $ file_name;
$ Link = mysql tutorial _ connect ("localhost", "root ","");
Mysql_select_db ("fenye ");
Mysql_query ("set names utf8 ");
$ Insert = "insert into upfiles (title, img_url) values ('{$ _ post ['title']}', '{$ img_url }')";
Mysql_query ($ insert); // store the file path in the database tutorial
}
?>

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.