PHP File Upload Code Usage example parsing

Source: Internet
Author: User
Tags php file upload

PHP File Upload code writing process 1. First determine whether to upload the file 2. If there is any more to determine whether the upload error 3. If an error occurs, an error message 4 is indicated. If no error is found, then determine the file type 5. If the type meets the criteria, then determine whether the file 6 is present in the specified directory. If you do not move the file to the specified directory, you must know a few things to upload the file in PHP.

Copy Code

Description: The form of the action= "upload.php" refers to the click on the form of the submission, the upload command will be sent to this page called upload.php to deal with. Method= "POST" means to send by post, enctype= "Multipart/form-data" attribute specifies what type of content to use when submitting this form, and when the form requires binary data, such as the file content, use the "multipart/ Form-data ", this property is necessary if you want to upload a file. The type= "file" in input specifies that the input should be treated as a file, and there will be a browse button behind input.

A PHP processing page upload.php

    1. if ($_files[' myfile ' [' name ']! = ') {
    2. if ($_files[' myfile ' [' Error '] > 0) {
    3. echo "Error status:". $_files[' myfile ' [' Error '];
    4. } else {
    5. Move_uploaded_file ($_files[' myfile ' [' Tmp_name '], "uploads/". $files [' myfile '] [' name ']);
    6. echo "";
    7. }
    8. } else{
    9. echo "";
    10. }
    11. ?>
Copy Code

Upgrade the above PHP code. 1, upload.php

    1. PHP File Upload Code _bbs.it-home.org
Copy Code

2, uploadprocess.php

  1. Receive

  2. $username =$_post[' username '];
  3. $fileintro =$_post[' Fileintro '];

  4. echo $username. $fileintro;

  5. Get file information
  6. /* echo "
    ";
  7. Print_r ($_files);
  8. echo "
  9. ";
  10. */
  11. Get the size of a file
  12. $file _size=$_files[' myfile ' [' Size '];
  13. if ($file _size>2*1024*1024) {
  14. echo "";
  15. Exit ();
  16. }
  17. Get file type
  18. $file _type=$_files[' myfile ' [' type '];
  19. if ($file _type!= "Image/jpeg" && $file _type!= "Image/pjpeg") {
  20. echo "File type can only be in JPG format";
  21. Exit ();
  22. }
  23. Determine if the upload is OK
  24. if (Is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {
  25. Get uploaded files to the directory you want
  26. $upload _file=$_files[' myfile ' [' tmp_name '];

  27. Prevent picture overlay problems, create a folder for each user

  28. $user _path=$_server[' Document_root ']. " /file/up/". $username;
  29. if (!file_exists ($user _path)) {
  30. mkdir ($user _path);
  31. }
  32. $move _to_file= $user _path. " /". $_files[' myfile ' [' name '];
  33. Prevent users from uploading the same user name issue
  34. $file _true_name=$_files[' myfile ' [' name '];
  35. $move _to_file= $user _path. " /". Time (). Rand (1,1000). substr ($file _true_name,strripos ($file _true_name,". "));
  36. Echo $upload _file. $move _to_file;
  37. Chinese to Transcode
  38. if (Move_uploaded_file ($upload _file,iconv ("Utf-8", "gb2312", "$move _to_file"))) {
  39. echo $_files[' myfile ' [' Name ']. " Upload Success ";
  40. }else{
  41. echo "Upload failed";
  42. }
  43. }else{
  44. echo "Upload failed";
  45. }
  46. ?>
Copy Code

Note: For example a picture file pic.jpg, with STRRCHR processing, STRRCHR (pic.jpg, '. '), it will return. jpg, do you understand? The function returns the character of the specified character after the position of the last occurrence of the string. With substr () we can take the JPG, so that we get the file suffix, to determine whether the upload file conforms to the specified format. This program puts the specified format in an array, which can be added as needed when actually used.

Look at the generation of random number file name section, see Mt_srand () This function, the manual called him "sow a better random number generator seed", is actually the initialization of a random number function, the parameter is (double) microtime () * 1000000, here if not this is the parameter will automatically set a random number, of course, this does not meet the need, so that the random number has a certain length, to ensure that the upload file does not have duplicate name.

  • 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.