php上传图片客户端和服务器端实现方法

2025-05-29 0 86

本文实例讲述了php上传图片客户端服务器端实现方法。分享给大家供大家参考。具体如下:

前台表单代码

?

1

2

3

4
<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>

后端php代码

?

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
<?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['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);

?>

希望本文所述对大家的php程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php上传图片客户端和服务器端实现方法 https://www.kuaiidc.com/101721.html

相关文章

发表评论
暂无评论