Upload files to create a directory and then upload to the directory

  • 2020-03-31 21:23:07
  • OfStack

1. Form part:
 
<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"> 
 The file name : <input type="text" name="title" /> 
 Upload a file : <input type="file" name="file" /> 
<input type="submit" vlaue=" submit " /> 
</form> 
</body> 
</html> 

2. Handle the form page:
 
<?php 
$mkdir_file_dir = mkdir('./img/'.$_POST['title'],0777); //To get the title, create a folder under the final directory to hold the files specified in the category
$tmp_file_name = $_FILES['file']['tmp_name']; //Get the temporary file after uploading
$file_name = $_FILES['file']['name']; //The 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 Final save 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
} 
?> 

Related articles: