简单讲解下PHP服务端接受客户端上传的文件
客户端上传文件到服务器,带参数,并且进行用户验证,方法很简单
直接贴代码
<?php
//强制显示错误
error_reporting(E_ALL);
// error_reporting = E_ALL & ~E_NOTICE
ini_set('display_errors', '1');
$UserId = str_replace("'", "", $_REQUEST["UserId"]);//接收用户ID
$Token = str_replace("'", "", $_REQUEST["Token"]);//接收Token
$sql = "SELECT * FROM `im_user` WHERE `UserId` = '$UserId'";
$request = mysql_fetch_assoc(mysql_query($sql));
//进行验证,如果验证通过,处理接收文件
if (md5($UserId.$request["UserToken"]."qwertyuiop") == $Token)
{
//判断是否有文件上传
if (isset($_FILES['file'])) {
//定义文件
$name = time().rand(10000000,99999999);
$target_path = "./uploadfile/";//接收文件目录
// echo $target_path.$name.$_FILES['file']['type'];
// $target_path = $target_path.basename($_FILES['file']['name']);
$target_path = $target_path.basename($name.".FT");
//将文件从临时目录拷贝到指定目录
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
//上传成功,可进行进一步操作,将路径写入数据库等.
echo "The file ".basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!" . $_FILES['file']['error'];
}
exit;
}else
{
echo "没有文件";
}
}else
{
echo "认证不通过!";
}
?>
网友评论