php文件上传功能

仅仅实现一个小的上传功能,并没有过多注意代码的质量

html

<html>
   <head><title>test the php</title></head>
   <body>
   <h1> upload new file to website</h1>
      <form action="test3.php" method="post" enctype="multipart/form-data">
	     <div>
		    <input type="hidden" name="MAX_SIZE" value="100000000"/>
			<lable for="userfile" >upload a file :</lable>
			<input type="file" name="userfile" id="userfile" />
			<input type="submit" value="submit" />
		 </div>
	  </form>

   </body>
</html>

test3.php

<?php

   if($_FILES['userfile']['error'] > 0)
   {
      echo "upload error"."</br>";
	  exit;
   }
   else
   {
      echo "upload ok"."</br>";
   }

   if(file_exists("/var/www/".$_FILES['userfile']['name']))
   {
      echo "file  already exists"."</br>";
	  exit;
   }
   else
   {
     move_uploaded_file($_FILES['userfile']['tmp_name'], "/var/www/".$_FILES['userfile']['name']); //必须修改上传文件名,否则脚本退出会自动删除上传文件
	 echo "upload finished<br>";
   }
?>

注意:php.ini文件中有关上传的参数配置

file_uploads, upload_tmp_dir, upload_max_filesize, post_max_size


test.php

<html>
   <head><title>test the php</title></head>
   <body>
   <h1> upload new file to website</h1>
      <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post" enctype="multipart/form-data">
	     <div>
		    <input type="hidden" name="MAX_SIZE" value="100000000"/>
			<lable for="userfile" >upload a file :</lable>
			<input type="file" name="userfile" id="userfile" />
			<input type="submit" value="submit" />
		 </div>
	  </form>
   </body>
</html>

<?php

if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
{
   if($_FILES['userfile']['error'] > 0)
   {
      echo "upload error"."</br>";
	  exit;
   }
   else
   {
      echo "upload ok"."</br>";
   }

   if(file_exists("/var/www/".$_FILES['userfile']['name']))
   {
      echo "file  already exists"."</br>";
	  exit;
   }
   else
   {
     move_uploaded_file($_FILES['userfile']['tmp_name'], "/var/www/".$_FILES['userfile']['name']);
	 echo "upload finished<br>";
   }
}
?>

利用HTTP/Upload方式:

<html>
   <head><title>test the php</title></head>
   <body>
   <h1> upload new file to website</h1>
      <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post" enctype="multipart/form-data">
	     <div>
		    <input type="hidden" name="MAX_SIZE" value="100000000"/>
			<lable for="userfile" >upload a file :</lable>
			<input type="file" name="userfile" id="userfile" />
			<input type="submit" value="submit" name="ok"/>
		 </div>
	  </form>
   </body>
</html>

<?php
require('HTTP/Upload.php');
if(isset($_POST['ok']))
{
   echo "test...<br>";
   $upload = new HTTP_Upload();
   $file = $upload->getFiles('userfile');
   if($file == null)
   {
      echo "file == NULL <br>";
	  exit;
   }
   //print_r($file);
   echo $_FILES['userfile']['name']."</br>";
   if($file->isValid())
   {
     $file->moveTo('/var/www/');
     echo "upload ok<br>";
   }
   else
   {
     echo $file->errorMsg();
   }
}
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值