Teach you to make file PHP upload effective _php tutorial

Source: Internet
Author: User
PHP after a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. PHP is a server-side scripting language for creating dynamic Web pages. Like ASP and ColdFusion, users can mix PHP and HTML to write Web pages, and when a visitor browses to the page, the server first processes the PHP commands in the page and then sends the processed results along with the HTML content to the browser on the access side.

However, unlike ASP or ColdFusion, PHP is a source code open program with good cross-platform compatibility. Users can run PHP on the Windows NT system and on many versions of UNIX systems, and can run PHP as a built-in module or CGI program for the Apache server. In addition to being able to precisely control the display of Web pages, users can also send HTTP headers by using PHP. Users can set up cookies through PHP, manage user identification, and redirect users ' browsing pages. PHP has a very powerful database support capability to access almost all of the more popular database systems available today. In addition, PHP can be integrated with multiple external libraries, providing users with more useful functions, such as generating PDF files.

The user can enter the PHP command code directly in the Web page, so no special development environment is required. In the Web page, all of the PHP code is placed in the. In addition, users can choose to use forms such as the. The PHP engine automatically recognizes and processes all the code in the page between the PHP delimiters.

The syntax structure of the PHP scripting language is very similar to the grammar style of the C and Perl languages. The variable does not need to be declared before the user can use the variable. The process of creating an array using PHP is also very simple. PHP also has the basic object-oriented component function, which can greatly facilitate the user to effectively organize and encapsulate the code they write. Here to introduce you about the PHP upload problem.

Example 1. Make file PHP upload effective

Please refer to Functions Is_uploaded_file () and Move_uploaded_file () for further information. The following example processes file uploads that are 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. $ Uploaddir = '/var/www/uploads/' ;
  5. $ UploadFile = $uploaddir. BaseName ($_files[' userfile ' [' name ']);
  6. Echo ' < Pre > ';
  7. if (Move_uploaded_file ($_files[' userfile ' [' tmp_name '], $uploadfile)) {
  8. echo "File is valid, and was successfully UPLOADED.N";
  9. } else {
  10. echo "Possible file upload attack!n";
  11. }
  12. Echo ' Here's some more debugging info: ';
  13. Print_r ($_files);
  14. print " pre>";
  15. ?>

A script that accepts PHP uploads files should implement any logically necessary checks in order to decide what to do with the file next. For example, you can use the $_files[' userfile ' [' size '] variable to exclude files that are too large or too small, or you can exclude file types and files that do not conform to a standard by $_files[' userfile ' [' type '] variables, But just consider this as the first step in a series of checks, because this value is completely controlled by the client and is not checked on the PHP side. From PHP 4.2.0, you can also plan what to do next with different error codes by $_files[the ' userfile ' [' ERROR '] variable. In any case, either remove the file from the temp directory or move it to a different location.

If no uploaded file is selected in the form, the value of PHP variable $_files[' userfile ' [' size '] will be null for 0,$_files[' userfile ' [' tmp_name ']. If the file has not been moved elsewhere and has not been renamed, the file will be deleted at the end of the form request.


http://www.bkjia.com/PHPjc/446565.html www.bkjia.com true http://www.bkjia.com/PHPjc/446565.html techarticle PHP After a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. PHP is a server-side scripting language for creating Dynamic Web pages ...

  • 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.