Home > Backend Development > PHP Tutorial > Simple example of uploading images in PHP (reference for getting started)

Simple example of uploading images in PHP (reference for getting started)

WBOY
Release: 2016-07-25 08:59:55
Original
1298 people have browsed it
Copy the code

2, admin_upfile.php

  1. date_default_timezone_set('PRC') ;
  2. //Upload $upsize here to determine the size of the uploaded file
  3. $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] : "/attached/"; //File upload path
  4. $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform"; //Back to the Name of the Form where the edit box is located on the previous page
  5. $editName = isset($_REQUEST["editName"] ) ? $_REQUEST["editName"] : $_REQUEST["editName"]; //Return to the Name of the edit box on the previous page
  6. //Convert the path to the root directory
  7. if (strpos($uppath, "/") = = 0) {
  8. $i = 0;
  9. $thpath = $_SERVER["SCRIPT_NAME"];
  10. $thpath = substr($thpath, 1, strlen($thpath));
  11. while (strripos($thpath, "/ ") !== false) {
  12. $thpath = substr($thpath, strpos($thpath, "/") + 1, strlen($thpath));
  13. $i = ++$i;
  14. }
  15. $ pp = "";
  16. for ($j = 0; $j < $i; ++$j) {
  17. $pp .="../";
  18. }
  19. $uppaths = $pp . substr($ uppath, 1, strlen($thpath));
  20. }
  21. $filename = date("y-m-d");
  22. if (is_dir($uppaths . $filename) != TRUE)
  23. mkdir($uppaths . $filename, 0777) ;
  24. // if(is_dir($filename."/".$ctime)!=TRUE) mkdir($filename."/".$ctime,0777);
  25. $f = $_FILES['file1'];
  26. if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $ f["type"] != "image/x-png") {
  27. echo "<script>alert('Only files in image format can be uploaded');window.close()</script>";
  28. //echo $f['type'];
  29. return false;
  30. }
  31. //Get file extension
  32. $temp_arr = explode(".", $f["name"]);
  33. $file_ext = array_pop ($temp_arr);
  34. $file_ext = trim($file_ext);
  35. $file_ext = strtolower($file_ext);
  36. //New file name
  37. $new_file_name = md5(date("YmdHis")) . '.' . $file_ext;
  38. //$new_file_name = md5(date("YmdHis") . '_' . rand(10000, 99999)) . '.' . $file_ext;
  39. $dest = $uppaths . $filename . "/ " . date("ymdhis") . "_" . $new_file_name; //Set the file name to the date plus the file name to avoid repeated uploading of directories
  40. $dest1 = $uppath . $filename . "/" . date("ymdhis" ) . "_" . $new_file_name; //Set the file name to the date plus the file name to avoid duplication
  41. $r = move_uploaded_file($f['tmp_name'], $dest);
  42. if ($f['size'] > 0) {
  43. echo "<script>window.opener.document." . $formName . "." . $editName . ".value='" . $dest1 . "'</script>";
  44. echo "<script>alert('Image uploaded successfully');window.close()</script>";
  45. }
  46. ?>
Copy code

3、admin_upload.php

  1. 图片上传_bbs.it-home.org
  2. $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] . "/" : "/attached/"; //文件上传路径
  3. $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform"; //回传到上页面编辑框所在Form的Name
  4. $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"]; //回传到上页面编辑框的Name
  5. ?>
  6. 正在上传文件,请稍候...
  • 图 片 上 传
  • 选择文件:

  • 复制代码


    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template