PHP traditional file upload and Base64-bit file Upload

Source: Internet
Author: User
PHP traditional file upload and Base64-bit file Upload

I. basic knowledge

By using the Global Array $ _ FILES of PHP, you can upload FILES from the client computer to a remote server.

The first parameter is the input name of the form, and the second subscript can be "name", "type", "size", "tmp_name" or "error ". Like this:

$ _ FILES ["file"] ["name"]-name of the uploaded file

$ _ FILES ["file"] ["type"]-type of the file to be uploaded

$ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes

$ _ FILES ["file"] ["tmp_name"]-name of the temporary copy of the file stored on the server

$ _ FILES ["file"] ["error"]-error code caused by file upload

This is a very simple file Upload method. Based on security considerations, you should add restrictions on which users have the right to upload files.

II. common uploads

  1. If ($ _ FILES ["file"] ["type"] = "image/gif ")
  2. | ($ _ FILES ["file"] ["type"] = "image/jpeg ")
  3. | ($ _ FILES ["file"] ["type"] = "image/pjpeg "))
  4. & ($ _ FILES ["file"] ["size"] <20000 ))
  5. {
  6. If ($ _ FILES ["file"] ["error"]> 0)
  7. {
  8. Echo "Return Code:". $ _ FILES ["file"] ["error"]."
    ";
  9. }
  10. Else
  11. {
  12. Echo "Upload:". $ _ FILES ["file"] ["name"]."
    ";
  13. Echo "Type:". $ _ FILES ["file"] ["type"]."
    ";
  14. Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
    ";
  15. Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
    ";
  16. If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
  17. {
  18. Echo $ _ FILES ["file"] ["name"]. "already exists .";
  19. }
  20. Else
  21. {
  22. Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
  23. "Upload/". $ _ FILES ["file"] ["name"]);
  24. Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
  25. }
  26. }
  27. }
  28. Else
  29. {
  30. Echo "Invalid file ";
  31. }
  32. ?>

3. php reads and saves base64 encoded image content

  1. If ($ _ POST ['submit ']) {
  2. $ Image_info = getimagesize ($ _ FILES ['file'] ['tmp _ name']); // $ _ FILES ['file'] ['tmp _ name'] indicates the file path.
  3. $ Base64_image_content = "data: {$ image_info ['Mime ']}; base64 ,". chunk_split (base64_encode (file_get_contents ($ _ FILES ['file'] ['tmp _ name']);
  4. Echo $ base64_image_content; die;
  5. If (preg_match ('/^ (data: \ s * image \/(\ w +); base64,)/', $ base64_image_content, $ result )){
  6. $ Type = $ result [2];
  7. $ New_file = "./test. {$ type }";
  8. If (file_put_contents ($ new_file, base64_decode (str_replace ($ result [1], '', $ base64_image_content )))){
  9. Echo 'new file saved successfully: ', $ new_file;
  10. }
  11. }
  12. }
  13. ?>


File upload, PHP

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.