Understanding stream upload and form upload

Source: Internet
Author: User

Stream upload:

$post_input = ‘php://input‘;$save_path = dirname( __FILE__ );$postdata = file_get_contents( $post_input );if ( isset( $postdata ) && strlen( $postdata ) > 0 ) {    $filename = $save_path . ‘/‘ . uniqid() . ‘.jpg‘;    $handle = fopen( $filename, ‘w+‘ );    fwrite( $handle, $postdata );    fclose( $handle );    if ( is_file( $filename ) ) {        echo ‘Image data save successed,file:‘ . $filename;        exit ();    }else {        die ( ‘Image upload error!‘ );    }}else {    die ( ‘Image data not detected!‘ );}
View code

Standard form upload:

if (!$_FILES[‘Filedata‘]) {    die ( ‘Image data not detected!‘ );}if ($_FILES[‘Filedata‘][‘error‘] > 0) {    switch ($_FILES [‘Filedata‘] [‘error‘]) {        case 1 :            $error_log = ‘The file is bigger than this PHP installation allows‘;            break;        case 2 :            $error_log = ‘The file is bigger than this form allows‘;            break;        case 3 :            $error_log = ‘Only part of the file was uploaded‘;            break;        case 4 :            $error_log = ‘No file was uploaded‘;            break;        default :            break;    }    die ( ‘upload error:‘ . $error_log );} else {    $img_data = $_FILES[‘Filedata‘][‘tmp_name‘];    $size = getimagesize($img_data);    $file_type = $size[‘mime‘];    if (!in_array($file_type, array(‘image/jpg‘, ‘image/jpeg‘, ‘image/pjpeg‘, ‘image/png‘, ‘image/gif‘))) {        $error_log = ‘only allow jpg,png,gif‘;        die ( ‘upload error:‘ . $error_log );    }    switch($file_type) {        case ‘image/jpg‘ :        case ‘image/jpeg‘ :        case ‘image/pjpeg‘ :            $extension = ‘jpg‘;            break;        case ‘image/png‘ :            $extension = ‘png‘;            break;        case ‘image/gif‘ :            $extension = ‘gif‘;            break;    }    }if (!is_file($img_data)) {    die ( ‘Image upload error!‘ );}
View code

 

PHP input stream PHP: // Input

When using XML-RPC, the server obtains client data mainly through the PHP input stream input instead of the $ _ post array. So here we will mainly discuss PHP input stream PHP: // Input

For PHP: // input introduction, the PHP official manual provides a clear overview of it:

"PHP: // input allows you to read raw post data. it is a less memory intensive alternative to $ http_raw_post_data and does not need any special PHP. INI directives. PHP: // input is not available with enctype = "multipart/form-data ".

The translation is like this:

"PHP: // input can read post data that has not been processed. Compared with $ http_raw_post_data, it puts less pressure on memory and does not require special PHP. ini settings. PHP: // input cannot be used for enctype = multipart/form-data"

How should we understand this overview? I divide it into three parts to gradually understand:

  1. Read Post Data
  2. Cannot be used for multipart/form-Data Type
  3. PHP: // input vs $ http_raw_post_data
Read Post Data

Phper must be familiar with the built-in variable $ _ post. $ _ What are the associations and differences between post and PHP: // input? In addition, the most common method for the client to interact with the server is post and get. Since PHP: // input is used as the PHP input stream, can it read get data? These two questions are what we need to discuss in this section.

For details, see:

Http://www.nowamagic.net/academy/detail/12220520

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.