Demo of PHP file upload code

Source: Internet
Author: User

PHP has been developing for a long time and many users are familiar with PHP. Here I will share the PHP file upload code and discuss it with you. PHP itself is a simple and powerful language. PHP has core features such as powerful string and array processing capabilities, and greatly improved support for Object-Oriented Programming (PHP5 and later versions ). Using standard and optional extension modules, PHP applications can connect to more than a dozen databases such as MySQL or Oracle, draw, create PDF files, and create parsed XML files. You can also use the C language to write your own PHP extension module.

For example, provide a PHP interface function in an existing code library. You can also run PHP In Windows, use COM to control other Windows applications such as Word and Excel, or Use ODBC to connect to the database. In China, PHP is a commonly used network programming language. ASP3.0 has already been abandoned by Microsoft and will not be updated any more. Some people may think that PHP should also be something that will soon become a new thing, right? However, on the contrary, PHP is not only developing rapidly, but also leaving ASP far behind. It is gradually becoming the most widely used and popular language on the Internet.

PHP file upload code

 
 
  1. <Html><Head> 
  2. <Title>Upload File Form</Title></Head>
  3. <Body>
  4. <Form Enctype="Multipart/form-data" Action="" Method="Post">
  5. Select a file:<Br> 
  6. <Input Name="Upload_file" Type="File"><Br> 
  7. <Input Type="Submit" Value="Upload files">
  8. </Form>
  9. </Body> 
  10. </Html>
  11. <? 
  12. $Upload_file= $ _ FILES ['upload _ file'] ['tmp _ name'];
  13. $Upload_file_name= $ _ FILES ['upload _ file'] ['name'];
  14.  
  15. If ($ upload_file ){
  16. $File_size_max=1000* 1000; // 1 MB limit the maximum file upload capacity (bytes)
  17. $Store_dir="D :/"; // Storage location of uploaded files
  18. $Accept_overwrite=1; // Whether to overwrite the same file
  19. // Check the file size
  20. If ($ upload_file_size>$ File_size_max ){
  21. Echo "sorry, your file capacity exceeds the limit ";
  22. Exit;
  23. }
  24.  
  25. // Check the read/write File
  26. If (file_exists ($ store_dir. $ upload_file_name )&&! $ Accept_overwrite ){
  27. Echo "files with the same file name exist ";
  28. Exit;
  29. }
  30.  
  31. // Copy the file to the specified directory
  32. If (! Move_uploaded_file ($ upload_file, $ store_dir. $ upload_file_name )){
  33. Echo "failed to copy the file ";
  34. Exit;
  35. }
  36.  
  37. }
  38.  
  39. Echo"<P>You uploaded the file :";
  40. Echo $ _ FILES ['upload _ file'] ['name'];
  41. Echo"<Br>";
  42. // The original name of the client machine file.
  43.  
  44. Echo "the MIME type of the file is :";
  45. Echo $ _ FILES ['upload _ file'] ['type'];
  46. // MIME type of the file, which must be supported by the browser, for example, "image/gif ".
  47. Echo"<Br>";
  48.  
  49. Echo "Upload file size :";
  50. Echo $ _ FILES ['upload _ file'] ['SIZE'];
  51. // Size of the uploaded file, in bytes.
  52. Echo"<Br>";
  53.  
  54. Echo "the file is temporarily stored after being uploaded :";
  55. Echo $ _ FILES ['upload _ file'] ['tmp _ name'];
  56. // Temporary file name stored on the server after the file is uploaded.
  57. Echo"<Br>";
  58.  
  59.  
  60. $Erroe= $ _ FILES ['upload _ file'] ['error'];
  61. Switch ($ Erroe ){
  62. Case 0:
  63. Echo "uploaded successfully"; break;
  64. Case 1:
  65. Echo "the uploaded file exceeds the limit of the upload_max_filesize option in php. ini."; break;
  66. Case 2:
  67. Echo "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. "; Break;
  68. Case 3:
  69. Echo "only part of the file is uploaded"; break;
  70. Case 4:
  71. Echo "no file is uploaded"; break;
  72. }
  73. ?> 


The above is the detailed PHP file upload code, and I hope to help you.


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.