php实例:文件上传及错误代码

发布时间:2020-05-18编辑:脚本学堂
本文介绍下,php实现文件上传的一个简单例子,以及文件上传错误代码的相关内容,有需要的朋友,可以作个参考。

1,html表单部分
 

复制代码 代码示例:
<html>
 <head>
  <title>php文件上传-www.jb200.com</title>
 </head>
 <body>
  <h3>文件上传</h3>
  选择上传文件:<br>
  <form action="uploader.php" method="post" enctype="multipart/form-data">
  <input type="file" name="file" size="45">
  <br>
  <input type="submit" value="上传文件">
  </form>
 </body>
</html>

2,文件上传程序 uploader.php
 

复制代码 代码示例:

<?php
/**
* 接收文件上传信息
* by www.jb200.com
*/

  if( $_FILES['file']['name'] != "" )
  {
    copy ( $_FILES['file']['tmp_name'],
     "C:/Apache/htdocs/" . $_FILES['file']['name'] )
    or die( "Could not copy file" );
  }
  else
  {
    die( "No file specified" );
  }
?>
<html>
 <head>
  <title>上传成功</title>
 </head>
 <body>
  <h3>文件上传成功...</h3>
  <ul>
  <li>文件名称: <?php echo $_FILES['file']['name']; ?></li>
  <li>文件大小: <?php echo $_FILES['file']['size']; ?> bytes</li>
  <li>文件类型: <?php echo $_FILES['file']['type']; ?></li>
  </ul>
  <a href="<?php echo $_FILES['file']['name']; ?>">点这里查看文件</a>
 </body>
</html>

附:文件上传的错误代码
 

UPLOAD_ERR_OK          No error occurred.
UPLOAD_ERR_INI_SIZE    The uploaded file exceeds the maximum value specified in the php.ini file.
UPLOAD_ERR_FORM_SIZE   The uploaded file exceeds the maximum value specified by the MAX_FILE_SIZE hidden widget.
UPLOAD_ERR_PARTIAL     The file upload was canceled and only part of the file was uploaded.
UPLOAD_ERR_NOFILE      No file was uploaded.