PHP 原生 上传文件

//html页面
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<body>
<form action="upload.php" method="post"
 enctype="multipart/form-data">
 <label for="file">文件名:</label>
 <input type="file" name="file" id="file" />
 <br />
 <input type="submit" name="submit" value="上传" />
</form>
</body>
</html>

//PHP文件
/**
 * 判断图片上传是否成功
 * */

if (count($_FILES)) {
    $retFiles = uploadFile("file");
    var_dump($retFiles);
    if ($retFiles['status'] == 1) {
        echo $retFiles['message'];
    } else if ($retFiles['status'] == 0) {
        echo "上传图片名称: " . $retFiles["name"] . "
"
; echo "图片类型: " . $retFiles["type"] . "
"
; echo "图片大小: " . ($retFiles["size"] / 1024) . " Kb
"
; echo "临时文件: " . $retFiles["temp"] . "
"
; echo "存储路径: " .dirname(__FILE__). "/upload/img" . $retFiles["name"]; } } function uploadFile($fileName) { $files = $_FILES[$fileName]; if (!in_array($files["type"], array("image/gif","image/jpeg","image/pjpeg","image/jpeg", "image/png"))) { return array("status" => 1, "message" => "上传文件格式不正确"); } if ($files["error"] > 0) { return array("status" => 1, "message" => "上传错误"); } if (file_exists("upload/" . $files["name"])) { return array("status" => 1, "message" => $files["name"]." 已经存在"); } move_uploaded_file($files["tmp_name"], dirname(__FILE__)."/upload/img/".$files["name"]); return array("status" => 0, "name" => $files["name"], "type" => $files["type"], "size" => $files['size']/1024, "temp" => $files["tmp_name"]); }

你可能感兴趣的:(PHP)