Home > Backend Development > PHP Tutorial > Simple examples and instructions for uploading files with PHP

Simple examples and instructions for uploading files with PHP

WBOY
Release: 2016-07-29 09:12:22
Original
765 people have browsed it

Upload filesExample

First build a form

<code><span><<span>html</span>></span><span><<span>body</span>></span><span><<span>form</span><span>action</span>=<span>"upload.php"</span><span>method</span>=<span>"post"</span><span>enctype</span>=<span>"multipart/form-data"</span>></span><span><<span>input</span><span>type</span>=<span>"file"</span><span>name</span>=<span>"file"</span>/></span><span><<span>input</span><span>type</span>=<span>"submit"</span><span>value</span>=<span>"submit"</span><span>name</span>=<span>"submit"</span>></span><span></<span>form</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
Copy after login
that can

upload files

. In the form, after submission, it will be handed over to upload.php for processing. Let’s write the simplest upload handler:

<code><span><?php</span>
var_dump(<span>$_POST</span>);
var_dump(<span>$_FILES</span>);

<span>$uploadPath</span> = <span>'./upload/'</span>;
<span>$tempFileName</span> = <span>$_FILES</span>[<span>'file'</span>][<span>'tmp_name'</span>];
<span>$uploadFileName</span> = <span>$uploadPath</span>.<span>$_FILES</span>[<span>'file'</span>][<span>'name'</span>];
<span>if</span>(move_uploaded_file(<span>$tempFileName</span>,<span>$uploadFileName</span>)){
  <span>echo</span><span>'upload success'</span>;
}<span>else</span>{
  <span>echo</span><span>'upload fail'</span>;
}</code>
Copy after login

Visit the submission page and submit the form. You can see the following output:

<code><span>array</span> (size=<span>1</span>)
  <span>'submit'</span> => string <span>'submit'</span> (length=<span>6</span>)
<span>array</span> (size=<span>1</span>)
  <span>'file'</span> =>
    <span>array</span> (size=<span>5</span>)
      <span>'name'</span> => string <span>'laravel-quickstart-welcome.png'</span> (length=<span>30</span>)
      <span>'type'</span> => string <span>'image/png'</span> (length=<span>9</span>)
      <span>'tmp_name'</span> => string <span>'/tmp/phpYKQKaY'</span> (length=<span>14</span>)
      <span>'error'</span> => int <span>0</span><span>'size'</span> => int <span>91148</span>
upload success</span></code>
Copy after login

The output illustrates two points: The content submitted by the input type=file will appear in the

$_FILES
    array. Use input's
  1. name field as its index in the $_FILESarray. The content submitted by input with type=file will continue to appear in the $_POST
  2. array. The structure of the array corresponding to each file in the
  3. $_FILES
array is as follows:

name
  • The file name of the uploaded filetype
  • The type of the uploaded filetmp_name
  • PHP
  • After uploading the file, ​​it will be stored in the temporary directory first. tmp_name displays the path of the temporary fileerror
  • to store the error code
  • size
  • The size of the uploaded file, ​​in bytesSo PHP
  • The idea of ​​uploading files
is relatively simple. After the file is uploaded, the information will be saved in the

$_FILES array. You can verify the file as needed (such as the suffix name), and then use the move_uploaded_file function to copy the temporary file Go to your upload directory. Notes

from form must set the

enctype="multipart/form-data"
    attribute, otherwise it cannot be uploaded
  • The folder in the upload path must exist, otherwise an error will be reported
<code>failed to open stream: No such file or directory
</code>
Copy after login
  • Do not use other function copies To upload a file, please use the
  • move_uploaded_file
  • function. Because this function will verify whether the temporary file is a file uploaded through PHP, this can avoid mistakenly copying system files to the upload directory.

    If necessary, you can use

    is_uploaded_file
  • to determine whether a file was uploaded through PHP.
  • If the file corresponding to the path of the second parameter of move_uploaded_file
  • already exists, it will be overwritten.
  • Reference:
  • PHP file upload

      PHP: move_uploaded_file - Manual
    • ').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
    • ').text(i)); }; $numbering.fadeIn(1700); }); });
    The above introduces simple examples and instructions for uploading files in PHP, including uploading files and indexing. I hope it will be helpful to friends who are interested in PHP tutorials.

    Related labels:
    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