Home > Backend Development > PHP Tutorial > PHP image upload client and server side implementation method_PHP tutorial

PHP image upload client and server side implementation method_PHP tutorial

WBOY
Release: 2016-07-13 09:58:55
Original
1416 people have browsed it

PHP upload image client and server-side implementation method

This article mainly introduces the PHP image upload client and server-side implementation method, involving the skills of PHP file operation, involving Common methods such as image suffix verification and file renaming have certain reference value. Friends in need can refer to it

The example in this article describes the client and server-side implementation methods of uploading images in PHP. Share it with everyone for your reference. The details are as follows:

Front-end form code

?

1

2

3

4

1 2 3 4

Backend php code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

//这里上传 $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['pic'];

if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png")

{

echo "<script>alert('只能上传图片格式的文件');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")) . '.' . $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['tmp_name'], $dest);

?>

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<🎜>//Upload here $upsize determines the size of the uploaded file<🎜> <🎜>$uppath = "/attached/"; //File upload path<🎜> <🎜>//Convert the path to the root directory<🎜> <🎜>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['pic'];<🎜> <🎜>if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png")<🎜> <🎜>{<🎜> <🎜>echo "<script>alert('Only files in image format can be uploaded');window.close()</script>"; return false; } //Get file extension $temp_arr = explode(".", $f["name"]); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //New file name $new_file_name = md5(date("YmdHis")) . '.' . $file_ext; echo $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //Set the file name with the date plus the file name to avoid duplication Upload directory echo $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //Set the file name with the date plus the file name to avoid duplication $r = move_uploaded_file($f['tmp_name'], $dest); ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/976548.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976548.htmlTechArticlephp upload image client and server side implementation method This article mainly introduces php upload image client and server side The implementation method involves the skills of operating PHP files and the image suffix verification...
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