php下传文件的简单代码

WBOY
Release: 2016-06-13 12:51:28
Original
816 people have browsed it

php上传文件的简单代码
在php编程中,文件上传可以说是太常见的了,分享一个php 上传文件的简单代码,适合初学的朋友。

1、文件上传界面 up_file.html
  

 <HTML>
    <HEAD>
    <TITLE>文件上传界面</TITLE>
    </HEAD>
    <BODY>

    <table>
    <tr align="center">
    <td><form ENCTYPE="multipart/form-data" NAME="SubmitForm" ACTION="upload.php" METHOD="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"><input type="hidden" name="UploadAction" value="1">
    </td></tr>
    <tr align="center">
    <td><input NAME="UploadFile" TYPE="file" SIZE="30"></td>
    </tr>
    <tr align="center">
    <td><input NAME="submit" VALUE="提交" TYPE="submit"></td>
    <td><input NAME="reset" VALUE="重置" TYPE="reset"></td>
    </tr>
    </form>
    </table>
    </center>
    </BODY>
    </HTML>
Copy after login


2、文件上传代码 upload.php
    <HTML>
    <HEAD>
    <TITLE>文件上传代码</TITLE>
    </HEAD>
    <BODY>
    <?
    /**
     @文件上传代码 保存上传的文件
     @site http://www.jbxue.com
   */
    $TimeLimit=60; /*设置超时限制时间 缺省时间为30秒 设置为0时为不限时 */
    set_time_limit($TimeLimit);

    If(($UploadFile !="none" ))
    {  
        $UploadPath=AddSlashes(dirname($PATH_TRANSLATED))."\my_files\"; //上载文件存放路径

        $FileName=$UploadPath.$UploadFile_name; //上载文件名

        if($UploadFile_size < 1024) //上载文件大小
        {
      $FileSize=(string)$UploadFile_size." 字节"; 
     }
         elseif ($UploadFile_size <(1024 * 1024))
         {
      $FileSize=number_format((double)($UploadFile_size/1024), 1) . " KB";
         }
      else{
      $FileSize=number_format((double)($UploadFile_size / (1024 * 1024)), 1) . " MB";
         }
       
        if(!file_exists($FileName))
        {
             if(copy($UploadFile,$FileName))
             {  echo "文件 $UploadFile_name ($FileSize)上载成功!"; }
             else
             { echo "文件 $UploadFile_name上载失败!"; }

             unlink($UploadFile);
        }
        else
        { echo "文件 $UploadFile_name已经存在!"; }
    }
    else
    { echo "你没有选择任何文件上载!"; }

    set_time_limit(30); //恢复缺省超时设置

    ?>

    <BR><A HREF = "upload.php">返回</A>

    </BODY>
    </HTML>
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!