php 上传文件

表单必须用 enctype="multipart/form-data"

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>upload</title>
</head>
<body>
<form action="./upload_process.php" method="post" enctype="multipart/form-data">
    <div>
        <input type="file" name="user_file" />
    </div>
    <div>
        <input type="submit" />
    </div>
</form>
</body>
</html>

保存文件时,检查文件类型,本来想使用$_FILES["user_file"]["type"],结果发现在各个浏览器下不一致。

只能使用文件名检查。
$_FILES["user_file"]["type"]
firefox下:application/zip 或 application/octet-stream
ie下:application/x-zip-compressed
chrome下:空

<?php
var_dump($_FILES["user_file"]);

$tmp = explode('.',$_FILES["user_file"]["name"]);
$last = count($tmp) -1;
if($tmp[$last]!='zip')
{
    echo 'only allow zip';
    exit;
}
$new_name_and_path = './files/'.md5($_FILES["sns_file"]["tmp_name"]).'.zip';
move_uploaded_file($_FILES["user_file"]["tmp_name"],$new_name_and_path);
?>
原文地址:https://www.cnblogs.com/sink_cup/p/php_upload_file.html