Home  >  Article  >  Backend Development  >  To upload a file, first create a directory and then upload it to the directory_PHP tutorial

To upload a file, first create a directory and then upload it to the directory_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:38829browse

1. Form part:

Copy code The code is as follows:



<br>my is upfile app!! <br>




File name:
Upload file:





2. Process the form Page:
Copy code The code is as follows:

$mkdir_file_dir = mkdir(' ./img/'.$_POST['title'],0777); //Get the title and create a folder under the final directory to store the files 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'].'/'; //Final save directory
if(is_dir($file_dir))
{
move_uploaded_file($tmp_file_name,$file_dir.$file_name); / /Move the file to the final saving directory
$img_url = $file_dir.$file_name;
$link = mysql_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
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322762.htmlTechArticle1, form part: Copy the code 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="...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn