note 100979 modified in wrappers.data by danbrown

From: Date: Thu, 18 Nov 2010 15:38:29 +0000
Subject: note 100979 modified in wrappers.data by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174157@lists.php.net to get a copy of this message
When passing plain string without base64 encoding, do not forget to pass the string through URLENCODE(), because PHP automatically urldecodes all entities inside passed string (and therefore all + get lost, all % entities will be converted to the corresponding characters). In this case, PHP is strictly compilant with the RFC 2397. Section 3 states that passes data should be either in base64 encoding or urlencoded. VALID USAGE: <?php $fp = fopen('data:text/plain,'.urlencode($data), 'rb'); // urlencoded data $fp = fopen('data:text/plain;base64,'.base64_encode($data), 'rb'); // base64 encoded data ?> Demonstration of invalid usage: <?php $data = 'Günther says: 1+1 is 2, 10%40 is 20.'; $fp = fopen('data:text/plain,'.$data, 'rb'); // INVALID, never do this echo stream_get_contents($fp); // Günther says: 1 1 is 2, 10@ is 20. // ERROR $fp = fopen('data:text/plain,'.urlencode($data), 'rb'); // urlencoded data echo stream_get_contents($fp); // Günther says: 1+1 is 2, 10%40 is 20. // OK // Valid option 1: base64 encoded data $fp = fopen('data:text/plain;base64,'.base64_encode($data), 'rb'); // base64 encoded data echo stream_get_contents($fp); // Günther says: 1+1 is 2, 10%40 is 20. // OK ?> --was-- When passing plain string without base64 encoding, do not forget to pass the string through URLENCODE(), because PHP automatically urldecodes all entities inside passed string (and therefore all + get lost, all % entities will be converted to the corresponding characters). In this case, PHP is strictly compilant with the RFC 2397. Section 3 states that passes data should be either in base64 encoding or urlencoded. VALID USAGE: <?php $fp = fopen('data:text/plain,'.urlencode($data), 'rb'); // urlencoded data $fp = fopen('data:text/plain;base64,'.base64_encode($data), 'rb'); // base64 encoded data ?> Demonstration of invalid usage: <?php $data = 'Günther says: 1+1 is 2, 10%40 is 20.'; $fp = fopen('data:text/plain,'.$data, 'rb'); // INVALID, never do this echo stream_get_contents($fp); // Günther says: 1 1 is 2, 10@ is 20. // ERROR $fp = fopen('data:text/plain,'.urlencode($data), 'rb'); // urlencoded data echo stream_get_contents($fp); // Günther says: 1+1 is 2, 10%40 is 20. // OK // Valid option 1: base64 encoded data $fp = fopen('data:text/plain;base64,'.base64_encode($data), 'rb'); // base64 encoded data echo stream_get_contents($fp); // Günther says: 1+1 is 2, 10%40 is 20. // OK http://php.net/manual/en/wrappers.data.php

« previous php.notes (#174157) next »