Home > Backend Development > PHP Tutorial > PHP implements client-side and server-side methods for uploading images

PHP implements client-side and server-side methods for uploading images

墨辰丷
Release: 2023-03-31 16:36:01
Original
1852 people have browsed it

This article mainly introduces the client and server-side implementation methods of uploading images in PHP, involving the skills of operating files in PHP, and common methods such as image suffix verification and file renaming. It has certain reference value and friends in need can refer to it. Next

The example in this article describes the client and server-side implementation methods of uploading pictures in PHP. The details are as follows:

Front-end form code

<form name="form1" method="post" action="admin_upfile.php" enctype="multipart/form-data">
  <input type="file" name="pic">
  <input type="submit" name="Submit" value="开始上传" class="button">
</form>
Copy after login

Back-end php code

<?php
//这里上传 $upsize判断上传文件的大小
$uppath = "/attached/"; //文件上传路径
//转换根目录的路径
if (strpos($uppath, "/") == 0)
{
  $i = 0;
  $thpath = $_SERVER["SCRIPT_NAME"];
  $thpath = substr($thpath, 1, strlen($thpath));
  while (strripos($thpath, "/") !== false)
  {
 $thpath = substr($thpath, strpos($thpath, "/") + 1, strlen($thpath));
 $i = ++$i;
  }
  $pp = "";
  for ($j = 0; $j < $i; ++$j)
  {
 $pp .= "../";
  }
  $uppaths = $pp . substr($uppath, 1, strlen($thpath));
}
$filename = date("y-m-d");
if (is_dir($uppaths . $filename) != TRUE)
  mkdir($uppaths . $filename, 0777);
$f = $_FILES[&#39;pic&#39;];
if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png")
{
  echo "<script>alert(&#39;只能上传图片格式的文件&#39;);window.close()</script>";
  return false;
}
//获得文件扩展名
$temp_arr = explode(".", $f["name"]);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//新文件名
$new_file_name = md5(date("YmdHis")) . &#39;.&#39; . $file_ext;
echo $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" . 
$new_file_name; //设置文件名为日期加上文件名避免重复 上传目录
echo $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" . 
$new_file_name; //设置文件名为日期加上文件名避免重复
$r = move_uploaded_file($f[&#39;tmp_name&#39;], $dest);
?>
Copy after login

Summary: The above is the entire content of this article, I hope it can help Everyone’s learning helps.

Related recommendations:

How to combine php template with js upload plug-in to achieve refresh-free upload

##php Number and number operation verification code

php uses chain call method to continuously call functions

The above is the detailed content of PHP implements client-side and server-side methods for uploading images. For more information, please follow other related articles on the PHP Chinese website!

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