PHP文件上传

<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file1">文件名:</label>
    <input type="file" name="file1" id="file"><br>
    <label for="file2">文件名:</label>
    <input type="file" name="file2" id="file"><br>
    <input type="submit" name="submit" value="提交">
</form>

</body>
</html>

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> upload_file.php </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
<?php
var_dump($_FILES);
echo '<hr>';
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
//$temp = explode(".", $_FILES["file1"]["name"]);
//$extension = end($temp);     // 获取文件后缀名
echo "上传文件大小: " . $_FILES["file1"]["size"] . "<br>";
$extension = pathinfo($_FILES["file1"]["name"], PATHINFO_EXTENSION);     // 获取文件后缀名
echo "上传文件扩展名: " . $extension . "<br>";

if ((($_FILES["file1"]["type"] == "image/gif")
|| ($_FILES["file1"]["type"] == "image/jpeg")
|| ($_FILES["file1"]["type"] == "image/jpg")
|| ($_FILES["file1"]["type"] == "image/pjpeg")
|| ($_FILES["file1"]["type"] == "image/x-png")
|| ($_FILES["file1"]["type"] == "image/png"))
&& ($_FILES["file1"]["size"] < 204800)   // 小于 200 kb
&& in_array($extension, $allowedExts))
{
    if ($_FILES["file1"]["error"] > 0)
    {
        echo "错误:: " . $_FILES["file1"]["error"] . "<br>";
    }
    else
    {
        echo "上传文件名: " . $_FILES["file1"]["name"] . "<br>";
        echo "文件类型: " . $_FILES["file1"]["type"] . "<br>";
        echo "文件大小: " . ($_FILES["file1"]["size"] / 1024) . " kB<br>";
        echo "文件临时存储的位置: " . $_FILES["file1"]["tmp_name"] . "<br>";
        
        // 判断当期目录下的 upload 目录是否存在该文件
        // 如果没有 upload 目录,你需要创建它,upload 目录权限为 777
        if (file_exists("upload/" . $_FILES["file1"]["name"]))
        {
            echo $_FILES["file1"]["name"] . " 文件已经存在。 ";
        }
        else
        {
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["file1"]["tmp_name"], "upload/" . $_FILES["file1"]["name"]);
            echo "文件存储在: " . "upload/" . $_FILES["file1"]["name"];
        }
    }
}
else
{
    echo "非法的文件格式";
}
?>

 </body>
</html>

 

 

array (size=2)
  'file1' => 
    array (size=5)
      'name' => string 'Desert_2.jpg' (length=12)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\wamp\tmp\php6150.tmp' (length=23)
      'error' => int 0
      'size' => int 56256
  'file2' => 
    array (size=5)
      'name' => string 'Chrysanthemum_s.jpg' (length=19)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\wamp\tmp\php6171.tmp' (length=23)
      'error' => int 0
      'size' => int 62254
上传文件大小: 56256
上传文件扩展名: jpg
上传文件名: Desert_2.jpg
文件类型: image/jpeg
文件大小: 54.9375 kB
文件临时存储的位置: C:\wamp\tmp\php6150.tmp
文件存储在: upload/Desert_2.jpg

 

posted @ 2017-06-11 16:33  sky20080101  阅读(74)  评论(0)    收藏  举报