当前位置:Gxlcms > PHP教程 > PHP上传图片的简单例子(入门参考)

PHP上传图片的简单例子(入门参考)

时间:2021-07-01 10:21:17 帮助过:19人阅读

2、admin_upfile.php

  1. date_default_timezone_set('PRC');
  2. //这里上传 $upsize判断上传文件的大小
  3. $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] : "/attached/"; //文件上传路径
  4. $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform"; //回传到上页面编辑框所在Form的Name
  5. $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"]; //回传到上页面编辑框的Name
  6. //转换根目录的路径
  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 "";
  28. //echo $f['type'];
  29. return false;
  30. }
  31. //获得文件扩展名
  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. //新文件名
  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; //设置文件名为日期加上文件名避免重复 上传目录
  40. $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //设置文件名为日期加上文件名避免重复
  41. $r = move_uploaded_file($f['tmp_name'], $dest);
  42. if ($f['size'] > 0) {
  43. echo "";
  44. echo "";
  45. }
  46. ?>

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. ?>

人气教程排行