At some point in your career, you will want to create true dynamic web applications. Maybe decide to write your social network. As a developer, you will try to interest users, give them the opportunity to upload photos, music, share files. In this case, you need to know and be able to work with the PHP function move uploaded file.
Data collection form
PHP supports uploading any files using the POST method. Therefore, to transfer data to the server, or save it to the local machine, you need an HTML form. Using markup, select a separate place where the user can select a file. Scenarios need to be set up so that data is sent without interruption. Consider this process using the example of uploading a photo for a profile.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="signup_form" enctype="multipart/form-data"> <fieldset> <label for="first_name">First Name:</label> <input type="text" name="first_name" size="20"> <label for="last_name">Last Name:</label> <input type="text" name="last_name" size="20"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <label for="user_pic"> :</label> <input type="file" name="user_pic" size="30"> <input type="submit" value=""> <input type="reset" value=""> </fieldset> </form>
Note the attribute enctype = "multipart / form-data". Thanks to him, the form is configured to receive data. In order to prevent users from inventing to download seals for 25 MB, in the hidden field, it is necessary to set a limit on the size of images. For a profile image, 2 MB is sufficient, as in this example.
Sending image
In the form above, the action attribute contains <? Php echo $ _SERVER ['PHP_SELF'];?>, Which means that the image will be received and processed in the same document. If this confuses you and you want a separate script for each action, insert the script name in the place of the construction, for example, create_profile.php. Now all the code, including working with the PHP function move uploaded file, write in this file.
<?php
The code contains auxiliary variables:
- $ Upload_dir contains the path to the directory to upload. Instead of HOSW_WWW_ROOT, write the absolute path to the working directory on the server or on your computer.
- The variable $ image_fieldname is the name of the field for the image in the HTML form.
As you can see, before calling move uploaded file, there are lines that check the loaded image. Of course, safe PHP applications are a myth, but at least minimal protection must be present.
The $ php_errors error check array does not start from zero, but from one. Under the index [0] is $ _FILES [$ image_fieldname] ['error'], which, if successful, returns the number 0. In PHP, $ _FILES is a special array containing all the information about the file. To access this data, an index is used with the name of the input field, which we previously displayed in a separate variable $ image_fieldname:
When move uploaded file PHP does not work
It seems to you that you did everything right, and the interpreter gives an error? Does the file refuse to load into the designated directory and appear in completely unexpected places? First of all, check php.ini for the allowed size in the upload_max_filesize parameter. Most often this error occurs with Denver. So open php.ini (usually located in the php folder) and just set the size you need:
;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;; ; Whether to allow HTTP file uploads. ; http://php.net/file-uploads file_uploads=On ; Temporary directory for HTTP uploaded files (will use system default if not ; specified). ; http://php.net/upload-tmp-dir upload_tmp_dir="***" ; Maximum allowed size for uploaded files. ; http://php.net/upload-max-filesize upload_max_filesize=5M ; Maximum number of files that can be uploaded via a single request max_file_uploads=50
If you are working on a server, ask your provider what size files are allowed to be uploaded. On a free hosting, the maximum amount is usually 2 MB. The second reason code may fail is because the directory is incorrectly assigned. Check the second argument to the move_uploaded_file function, it must include the path and be sure to name.