Home  >  Article  >  Backend Development  >  php file upload function

php file upload function

WBOY
WBOYOriginal
2016-07-30 13:30:311078browse

Only implement a small upload function without paying too much attention to the quality of the code

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[&#39;userfile&#39;][&#39;error&#39;] > 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>";
   }
?>

Note: Parameter configuration related to upload in the php.ini file

file_uploads, upload_tmp_dir , upload_max_filesize, post_max_size

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the PHP file upload function, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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