php初学者用文件上传实例
1,表单部分,代码如下:
- <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">
- 文件名: <input type="text" name="title" />
- 上传文件: <input type="file" name="file" />
- <input type="submit" vlaue="提交" />
- </form>
- </body>
- </html>
2,处理表单页面,代码如下:
- <?php
- $mkdir_file_dir = mkdir('./img/'.$_post['title'],0777);
- $tmp_file_name = $_files['file']['tmp_name'];
- $file_name = $_files['file']['name'];
- $file_dir = './img/'.$_post['title'].'/';
- if(is_dir($file_dir))
- {
- move_uploaded_file($tmp_file_name,$file_dir.$file_name);
- $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);
- }
- ?>
|