php 文件上传实现

upload.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="doAction.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:
<input type=‘file‘ name=‘myFile‘ />
<br />
<input type="submit" value="upload" />
</form>


</body>
</html>

doAction.php

<?php
//print_r($_FILES);
$filename=$_FILES[‘myFile‘][‘name‘];
$type=$_FILES[‘myFile‘][‘type‘];
$tmp_name=$_FILES[‘myFile‘][‘tmp_name‘];
$size=$_FILES[‘myFile‘][‘size‘];
$error=$_FILES[‘myFile‘][‘error‘];

//remove file
//move_uploaded_file($tmp_name,$destination);
//move_uploaded_file($tmp_name,"./".$filename);
//copy($src,$dst)

if($error == UPLOAD_ERR_OK)
{
  if(move_uploaded_file($tmp_name,"./".$filename))
  {
    echo ‘upload success‘;
  }
  else
  {
    echo "upload failed";
  }
}
else
{
  switch($error)
  {
    case 1:
      echo "error :1 --超过了php.ini文件中upload_max_filesize的值"

      break;
    case 2:
      echo "error :2 --上传文件的大小超过了表单MAX_FILE_SIZE的值";
      break;
    case 3:
      echo "error :3 --文件只有部分上传";
      break;
    case 4:
      echo "error :4 --没有文件上传";
      break;
    case 6:
      echo "error :6 --找不到临时文件夹";
      break;
    case 7:
      echo "error :7 --写入失败";
      break;
    case 8:
      echo "error :8 --上传的文件被php扩展程序中断";
      break;
   }
}
?>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。