A simple example of PHP image uploading (quick start)

Source: Internet
Author: User
A simple example of PHP image uploading (quick start)

2. admin_upfile.php

  1. Date_default_timezone_set ('prc ');
  2. // Upload $ upsize here to determine the size of the uploaded file
  3. $ Uppath = isset ($ _ REQUEST ["upPath"])? $ _ REQUEST ["upPath"]: "/attached/"; // file Upload path
  4. $ FormName = isset ($ _ REQUEST ["formName"])? $ _ REQUEST ["formName"]: "myform"; // upload the Name of the Form in the edit box of the previous page.
  5. $ EditName = isset ($ _ REQUEST ["editName"])? $ _ REQUEST ["editName"]: $ _ REQUEST ["editName"]; // The Name of the edit box on the upload page.
  6. // Convert the root directory path
  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 "script" alert ('only images can be uploaded '); window. close () script ";
  28. // Echo $ f ['type'];
  29. Return false;
  30. }
  31. // Obtain the file extension
  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. // New file name
  37. $ New_file_name = md5 (date ("YmdHis"). '.'. $ file_ext;
  38. // $ New_file_name = md5 (date ("YmdHis"). '_'. rand (10000,999 99). '.'. $ file_ext;
  39. $ Dest = $ uppaths. $ filename. "/". date ("ymdhis"). "_". $ new_file_name; // Set the file name to the date plus the file name to avoid repeated Upload directories
  40. $ Dest1 = $ uppath. $ filename. "/". date ("ymdhis"). "_". $ new_file_name; // Set the file name to the date plus the file name to avoid duplication
  41. $ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
  42. If ($ f ['size']> 0 ){
  43. Echo "scriptpolicw.opener.doc ument.". $ formName. ".". $ editName. ". value = '". $ dest1. "' script";
  44. Echo "script" alert ('image uploaded successfully'); window. close () script ";
  45. }
  46. ?>

3. admin_upload.php

  1. Picture upload_bbs.it-home.org
  2. $ Uppath = isset ($ _ REQUEST ["upPath"])? $ _ REQUEST ["upPath"]. "/": "/attached/"; // file Upload path
  3. $ FormName = isset ($ _ REQUEST ["formName"])? $ _ REQUEST ["formName"]: "myform"; // upload the Name of the Form in the edit box of the previous page.
  4. $ EditName = isset ($ _ REQUEST ["editName"])? $ _ REQUEST ["editName"]: $ _ REQUEST ["editName"]; // The Name of the edit box on the upload page.
  5. ?>

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.