环境
开发包:appserv-win32-2.5.10
服务器:Apache2.2
数据库:phpMyAdmin
语言:php5,java
平台:windows 10
需求
编写一个PHP脚本页面,可以向服务端上传文件或资源
代码
add.html
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 "您的文件已经上传完毕 上传图片预览: "; echo "保存成功!"; //插入数据库 include('../mysql.php');//连接数据库 session_start(); //获取当前用户 $u_id = $_SESSION["id"]; //echo $u_id; //echo "
"; //echo $name; //echo "
"; //echo $about; //echo "
"; //echo "第一个路径:$apk_file$apk"; //echo "
"; //echo "第二个路径:$zip_file$zip"; //echo "
"; //echo "第三个路径:$img_file$img"; //echo "
"; $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 '写入数据库成功!返回作品'; } else { echo '写入数据库失败,已删除保存文件!返回'; unlink ($apk_file.$apk); unlink ($zip_file.$zip); unlink ($img_file.$img); } }else{ echo '保存失败!返回'; //$myfile路径如:zip/2015-09-16142918 s.jpg unlink ($apk_file.$apk); unlink ($zip_file.$zip); unlink ($img_file.$img); } ?>