Home  >  Article  >  Backend Development  >  Upload pictures (with content that can be modified, non-function)

Upload pictures (with content that can be modified, non-function)

WBOY
WBOYOriginal
2016-07-25 09:07:02825browse

Implement the function of uploading pictures

  1. date_default_timezone_set("PRC");
  2. header("content-type:text/html;charset=utf-8");
  3. require_once("./function1.php");
  4. if (isset($_GET['act']) && ($_GET['act'] == "uploads")){
  5. //Determine whether the uploaded file is legal
  6. if(!is_uploaded_file($_FILES['avator'][ tmp_name])){
  7. msg("The uploaded file is illegal");
  8. }
  9. //Determine the file size
  10. $allowed_size = 2*1024*1024;
  11. if($_FILES['avator']['size']> ;$allowed_size){
  12. msg("The uploaded picture is too large, please modify it and re-upload it");
  13. }
  14. //Judge the type
  15. $allowed_type = array("jpg","bmp","gif","jpeg ","png","zip","rar","txt");
  16. $path_info_arr = pathinfo($_FILES['avator']['name']);//Get the array of file types
  17. $extension = strtolower($path_info_arr['extension']);
  18. if(!in_array($extension,$allowed_type)){
  19. msg("The uploaded type is not allowed");
  20. }
  21. //Rename
  22. if(!file_exists( "./uploads/")){
  23. @mkdir("./uploads/",0777,true);
  24. }
  25. $new_name = date("YmdHis").rand(1000,9999).".".$ extension;
  26. //Move
  27. $res = move_uploaded_file($_FILES['avator']['tmp_name'],"./uploads/{$new_name}");
  28. if($res){
  29. echo "< ;a href='./uploads/{$new_name}'>View uploaded files";
  30. }
  31. }else{
  32. ?>
  33. Please upload files
  34. }
Copy code


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