Home  >  Article  >  Backend Development  >  PHP上传实例代码并防止重复上传_PHP教程

PHP上传实例代码并防止重复上传_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:38:03865browse

PHP上传实例代码 防止重复上传,本上传实例代码非常合适php新手学习,有详细的注解。
session_start();   
/******以下可用来跟踪用户  
 $sess_id = session_id();  
 $id      = rand(100000000000000,9999999999999999);  
******/  
if( $_POST["Submit"]){   
    /******以下是防止重复上传,适用只允许上传一次  
    if( $_SESSION["name"] == "1"){  
        echo "

请不要重复提交!

";  
        exit;  
    }  
    ******/  
     $file_name =  $_FILES["file"]["name"];   
     $file_size =  $_FILES["file"]["size"];   
     $file_type =  $_FILES["file"]["type"];   
     $file_tn   = time(). $file_name;   
     $save_path = "upfiles/";   
     $messg     = "

上传文件发生以外:

返回重试";   
     $messg_sr  =  $messg;   
    if( $file_type != "application/msword"){//清风提示,这里限制上传格式为word   
         $messg .= "

本次上传文件格式为MS WORD,通常扩展名为.doc

";   
    }   
    if( $file_size > 1048576){//清风提示,这里可写成"if( $file_size > 1*1024*1024){"方便修改   
         $messg .= "

本次上传文件大小不能超过1MB,本文件大小为".round(( $file_size/1024/1024),2)."MB

";   
    }   
    if( $messg !=  $messg_sr){   
        echo  $messg;   
    }else{   
        if(move_uploaded_file( $_FILES["file"]["tmp_name"], $save_path. $file_tn)){   
            // $_SESSION["name"] = "1";#防止重复上传和上面对应   
            //以下是上传成功的各种提示及跳转   
            //echo "

恭喜你!上传文件成功。

";   
            //echo "";   
            //echo "<script>location.href=;</script>";   
            //echo "";#原型   
            echo "
上传成功!
";   
        }else{   
            echo  $messg;   
        }   
    }   
}else{   
?>   
   
   
   
   
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn