First understand the files Function
The $ _ files array contains the following content:
$ _ Files ['myfile'] ['name'] original name of the client file.
$ _ Files ['myfile'] ['type'] indicates the mime type of the file, which must be supported by the browser, for example, "image/gif ".
$ _ Files ['myfile'] ['SIZE'] size of the uploaded file, in bytes.
$ _ Files ['myfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded, which is generally the default file name. You can specify upload_tmp_dir in the php tutorial. ini, but the putenv () function setting does not work.
$ _ Files ['myfile'] ['error'] error Code related to the file upload. ['Error'] is added in php 4.2.0. The following is its description: (they become constants after php3.0)
Upload_err_ OK
Value: 0. If no error occurs, the file is uploaded successfully.
Upload_err_ini_size
Value: 1; the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
Upload_err_form_size
Value: 2; the size of the uploaded file exceeds the value specified by the max_file_size option in the html form.
Upload_err_partial
Value: 3; only part of the file is uploaded.
Upload_err_no_file
Value: 4; no file is uploaded.
Value: 5; the size of the uploaded file is 0.
Php code
<? Php
$ Mkdir_file_dir = mkdir ('./img/'. $ _ post ['title'], 0777); // create an image-related directory when uploading a file
$ Tmp_file_name = $ _ files ['file'] ['tmp _ name']; // temporary file name obtained after successful upload
$ File_name = $ _ files ['file'] ['name']; // original file name
$ File_dir = './img/'. $ _ post ['title']. '/'; // assign the Created directory to a variable as the final storage directory.
If (is_dir ($ file_dir ))
{
Move_uploaded_file ($ tmp_file_name, $ file_dir. $ file_name); // start to move the file
}
?>
Html code
<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">
Title: <input type = "text" name = "title"/>
Upload file: <input type = "file" name = "file"/>
<Input type = "submit" vlaue = "submit"/>
</Form>
</Body>
</Html>
After a file is submitted through post, it is saved in the c: windowstemp Temporary Folder through $ _ files ["photo"] ["tmp_name"], we can easily obtain the uploaded temporary file and save it to the path we specified. below is the solution move_uploaded_file ($ _ files ["photo"] ["tmp_name"], $ path)