教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP上传文件代码之入门代码

PHP上传文件代码之入门代码

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP上传文件代码之入门代码等资源,欢迎您收藏本站,我们将为您提供最新的PHP上传文件代码之入门代码资源
在php中文件上传我们都是利用表单的post来实例了,注意:
标签的 enctype 属性规定了在提交表单时要使用哪种内容类型。在表单需要二进制数据时,比如文件内容,请使用 \"multipart/form-data\"

这是一种非常简单文件上传方式。基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4535')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4535>


<!DOCTYPE html>  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
    <meta http-equiv="Content-Language" content="zh-cn" />  
    </head>  
    <body>  
    <form action="/upload.php" method="post" enctype="multipart/form-data">  
    <label for="file">Filename:</label>  
    <input type="file" name="file" id="file" /><br />  
    <input type="submit" name="submit" value="Submit" />  
    </form>  
    <?php  
    if($_POST){  
     if ( $_FILES["file"]["size"] < 2000000 )  
       {  
       if ($_FILES["file"]["error"] > 0)  
         {  
         echo "Return Code: " . $_FILES["file"]["error"] . "<br />";  
         }  
       else 
         {  
         echo "Upload: " . $_FILES["file"]["name"] . "<br />";  
         echo "Type: " . $_FILES["file"]["type"] . "<br />";  
         echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";  
         echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";  
       
         if (file_exists("upload/" . $_FILES["file"]["name"]))  
           {  
           echo $_FILES["file"]["name"] . " already exists. ";  
           }  
         else 
           {  
           move_uploaded_file($_FILES["file"]["tmp_name"],  
           "upload/" . $_FILES["file"]["name"]);  
           echo "Stored in: " . "upload/" . $_FILES["file"]["name"];  
           }  
         }  
       }  
     else 
       {  
       echo "Invalid file";  
       }  
    }  
    ?>  
    </body>  
    </html> 


第一个参数是表单的 input name,第二个下标可以是 "name", "type", "size", "tmp_name" 或 "error"。就像这样:

$_FILES["file"]["name"] - 被上传文件的名称
$_FILES["file"]["type"] - 被上传文件的类型
$_FILES["file"]["size"] - 被上传文件的大小,以字节计
$_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
$_FILES["file"]["error"] - 由文件上传导致的错误代码

实例

首先是上传的提交页面upfile.html

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5895')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5895>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="max_file_size" value="33554432">
<input type="file" name="file">
<input type="submit" name="submit" value="上传" />
</form>

1. PHP支持HTML以POST的方法传输文件,但是form中必须声明enctype的属性="multipart/form-data",否则整个form表单将不起任何作用。

2. form表单中必须含有一个name为MAX_FILE_SIZE的隐藏域,这个隐藏域用来指定用户最大能上传的文件大小,必须声明在所有其他input标签之前。如果文件超大的话,浏览器直接就可以给出提示,不必浪费上传的时间。

下面就是上传处理文件upload.php

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6804')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6804>

<?php
 
    /* 设定上传目录 */
    $dest_dir='uploads';
 
    /* 检测上传目录是否存在 */
    if( !is_dir($dest_dir) || !is_writeable($dest_dir) )
    {
        die("上传目录 ".$dest_dir." 不存在或无法写入");
    }
 
    /* 设置允许上传文件的类型 */
    $type=array("rar","zip","txt","c");
 
    /* 获取上传文件信息 */
    $upfile=&$HTTP_POST_FILES['file'];
 
    /* 获取文件后缀名函数 */
    function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
 
    /* 判断上传文件类型 */
    if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) )
     {
        $text=implode(",",$type);
        echo "对不起,您只能上传以下类型文件: ",$text,"<br>";
     }
     else
     {
        /* 设置文件名为"日期_文件名" */
        $dest=$dest_dir.'/'.date("ymdHis")."_".$upfile['name'];
 
        /* 移动上传文件到指定文件夹 */
        $state=move_uploaded_file($upfile['tmp_name'],$dest);
 
        if ($state)
        {
            print("文件上传成功!<br>");
            print("文件名:".$dest."<br>");
            print("上传的文件大小:".( round($upfile['size'] / 1024,2) )." KB<br>");
        }
        else
        {
            /* 处理错误信息 */
            switch($upfile['error'])
            {
                case 1 : die("上传文件大小超出 php.ini:upload_max_filesize 限制<br>");
                case 2 : die("上传文件大小超出 MAX_FILE_SIZE 限制<br>");
                case 3 : die("文件仅被部分上传<br>");
                case 4 : die("没有文件被上传<br>");
                case 5 : die("找不到临时文件夹<br>");
                case 6 : die("文件写入失败<br>");
            }
        }
     }
 
?>

您可能感兴趣的文章:
php文件上传代码大全(实例分享)
php入门教程(索引)
php入门实例查询数据库记录
php 文件上传实例剖析
php图片上传代码一例
php 文件上传简单实例
php多文件上传实现代码
php多文件上传的简单示例分析
web安全之文件上传漏洞攻击与防范方法
突破php上传文件大小限制的配置方法

[关闭]
~ ~