How to Make PHP File Upload take effect

Source: Internet
Author: User
Tags file upload script php file upload php file upload script

PHP has been developing for a long time and many users are familiar with PHP. Here I will share my personal understanding and discuss it with you. PHP is a server scripting language used to create dynamic web pages. Like ASP and ColdFusion, users can use PHP and HTML to write WEB pages. When a visitor browses this page, the server first processes the PHP commands on the page, then, the processed results and HTML content are sent to the browser at the access end.

However, unlike ASP or ColdFusion, PHP is an open source code program with good cross-platform compatibility. You can run PHP on Windows NT and many versions of Unix systems, and run PHP as a built-in module of the Apache server or CGI program. In addition to precisely controlling the content displayed on the web page, you can also use PHP to send HTTP headers. Users can set cookies through PHP, manage user identification, and redirect user browsing pages. PHP has very powerful database support functions and can access almost all of the more popular database systems. In addition, PHP can be integrated with multiple external libraries to provide users with more practical functions, such as generating PDF files.

You can directly enter the PHP Command code on the WEB page without any special development environment. On the WEB page, all PHP code is placed in. In addition, you can also choose to use the form such. The PHP engine automatically identifies and processes all code on the page between PHP delimiters.

The syntax structure of PHP script is very similar to that of C and Perl. You do not need to declare variables before using them. Using PHP to create an array is also very simple. PHP also has basic object-oriented component functions, which greatly facilitates users to effectively organize and encapsulate their own code. Here we will introduce you to the PHP Upload question.

Example 1. Make the PHP File Upload take effect

For more information, see the is_uploaded_file () and move_uploaded_file () functions. The following example processes the File Upload provided by the form.

 
 
  1. <?php 
  2. // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead  
  3. // of $_FILES.  
  4.  
  5. $uploaddir = '/var/www/uploads/';  
  6. $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);  
  7.  
  8. echo '<pre>';  
  9. if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {  
  10.    echo "File is valid, and was successfully uploaded.n";  
  11. } else {  
  12.    echo "Possible file upload attack!n";  
  13. }  
  14.  
  15. echo 'Here is some more debugging info:';  
  16. print_r($_FILES);  
  17.  
  18. print "</pre>";  
  19.  
  20. ?> 

Accept the PHP file upload script. In order to determine the operations to be performed on the file, you should perform any logical check. For example, you can use the $ _ FILES ['userfile'] ['SIZE'] variable to exclude FILES that are too large or too small, you can also use the $ _ FILES ['userfile'] ['type'] variable to exclude FILES with different file types and certain standards, however, this is only the first step in a series of checks, because this value is completely controlled by the client and not checked on the PHP side. Starting from PHP 4.2.0, you can use the $ _ FILES ['userfile'] ['error'] variable to plan the next step based on different error codes. In any case, you can either delete the file from the temporary directory or move it to another place.

If no uploaded file is selected in the form, the value of the PHP variable $ _ FILES ['userfile'] ['SIZE'] is 0, $ _ FILES ['userfile'] ['tmp _ name'] will be empty. If the file is not moved to other places or renamed, the file will be deleted at the end of the form request.


Related Article

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.