Home > Backend Development > PHP Tutorial > PHP traditional file upload and Base64-bit file upload

PHP traditional file upload and Base64-bit file upload

WBOY
Release: 2016-07-25 08:42:19
Original
1906 people have browsed it

1. Basic knowledge

By using PHP’s global array $_FILES, 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"] - the name of the uploaded file

$_FILES["file"]["type"] - the type of file being uploaded

$_FILES["file"]["size"] - The size of the uploaded file, in bytes

$_FILES["file"]["tmp_name"] - The 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 way to upload files. For security reasons, you should add restrictions on who has permission to upload files.

2. Commonly used 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. ?>

Copy code

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'] is 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. ?>

Copy code


File upload, PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template