Php image uploading client and server implementation

Source: Internet
Author: User

Php image uploading client and server implementation

This article mainly introduces the implementation of php image uploading client and server. It involves php file operation skills, and common methods such as image suffix verification and file rename. It has some reference value, for more information, see

 

 

This example describes how to upload images to the client and server in php. Share it with you for your reference. The details are as follows:

Front-end form code

?

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 = "Start upload" class = "button">

</Form>

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

<? Php

// Upload $ upsize here to determine the size of the uploaded file

$ Uppath = "/attached/"; // File Upload path

// Convert the root directory path

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 upload'); window. close () </script> ";

Return false;

}

// Obtain the 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 to the date plus the file name to avoid repeated upload directories.

Echo $ dest1 = $ uppath. $ filename. "/". date ("ymdhis ")."_".

$ New_file_name; // set the file name to the date plus the file name to avoid duplication.

$ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );

?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.