PHP学习笔记——上传文件到服务端的文件夹下
环境
开发包:appserv-win32-2.5.10
服务器:Apache2.2
数据库:phpMyAdmin
语言:php5,java
平台:windows 10
需求
编写一个PHP脚本页面,可以向服务端上传文件或资源
代码
add.html
<table>
<form method="post" action="add_back.php" enctype="multipart/form-data">
<tr>
<th>作品名</th>
<td><input type="text" style="width:500px; height:30px;" name="w_name"/></td>
</tr>
<tr>
<th>压缩包</th>
<td><input type="file" name="apk_url"/></td>
</tr>
<tr>
<th>源码压缩包</th>
<td><input type="file" name="zip_url"/></td>
</tr>
<tr>
<th>预览图片</th>
<td><input type="file" name="img_url"/></td>
</tr>
<tr>
<th>简介</th>
<td><textarea name="about" style="width:500px;height:150px;"></textarea></td>
</tr>
<tr>
<th></th>
<td><input name="Fruit" type="checkbox" value="mztk" style="width:40px;" />我已阅读并同意<a href="#">免责条款</a></td>
</tr>
<tr>
<td></td>
<td><input class="sc" type="submit" value="上传" /> </td>
</tr>
</form>
</table>
add_back.php
<?php function GetRandStr($len) { $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); $charsLen = count($chars) - 1; shuffle($chars); $output = ""; for ($i=0; $i<$len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } error_reporting(0); //不会报错 session_start(); $u_id = $_SESSION["id"];//获取当前用户ID $apk_file = "../apk/"; $zip_file = "../zip/";//你可以加上,文件类型,大小等判断 $img_file = "../img/";//设置文件保存目录 注意包含 $time = date('Y-m-d H:i:s',time());//文件名加上时间码 $s = str_replace(' ', '', $time);//去掉空格 $s = str_replace(':','',$s);//去掉: $btime = date('YmdHis',time());//文件名加上时间码 $name = $_POST['w_name']; //名称 $about = $_POST['about']; //关于 $apk = $_FILES['apk_url'][name];//获取文件名 $zip = $_FILES['zip_url'][name];//获取文件名 $img = $_FILES['img_url'][name];//获取文件名 $apk = substr($apk, -4);//取文件后缀 $zip = substr($zip, -4); $img = substr($img, -4); $name = GetRandStr(5); $apk = $btime.$name.$apk ;//存储文件名=时间+随机符+后缀 $zip = $btime.$name.$zip ; $img = $btime.$name.$img ; //echo $apk; //$_FILES['img']['tmp_name'],存的文件夹.存的名字 if( move_uploaded_file($_FILES['apk_url']['tmp_name'], $apk_file.$apk) && move_uploaded_file($_FILES['zip_url']['tmp_name'], $zip_file.$zip) && move_uploaded_file($_FILES['img_url']['tmp_name'], $img_file.$img) ){ //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$img_file$img' height='200px' width='300px'></center>"; echo "保存成功!"; //插入数据库 include('../mysql.php');//连接数据库 session_start(); //获取当前用户 $u_id = $_SESSION["id"]; //echo $u_id; //echo "<br/>"; //echo $name; //echo "<br/>"; //echo $about; //echo "<br/>"; //echo "第一个路径:$apk_file$apk"; //echo "<br/>"; //echo "第二个路径:$zip_file$zip"; //echo "<br/>"; //echo "第三个路径:$img_file$img"; //echo "<br/>"; $sql = "INSERT INTO word(w_id,u_id,w_name,apk_url,zip_url,img_url,about,w_time)VALUES('$btime$u_id','$u_id','$name','$apk_file$apk','$zip_file$zip','$img_file$img','$about','$time')"; if(mysql_query($sql,$conn)){ echo '写入数据库成功!<a href="user_zuoping.php">返回作品</a>'; } else { echo '写入数据库失败,已删除保存文件!<a href="javascript:history.back(-1);">返回</a>'; unlink ($apk_file.$apk); unlink ($zip_file.$zip); unlink ($img_file.$img); } }else{ echo '保存失败!<a href="javascript:history.back(-1);">返回</a>'; //$myfile路径如:zip/2015-09-16142918 s.jpg unlink ($apk_file.$apk); unlink ($zip_file.$zip); unlink ($img_file.$img); } ?>