stream_context_set_params 


php128 apg

SETS parameters for a stream/wrapper/context.



<?php

bool stream_context_set_params 
res $context 
                                                     
arr $params )


where,

$context The stream or context to 
                                  apply the parameters too
                                                                                
$params 
An array of parameters to setshould be 
                 an associative 
array of the structure:
                  
                  
$params['paramname'] = "paramvalue"
                  
?>

 $context 


The stream or context to apply the parameters too.

A STREAM WRAPPER is an abstraction of a file system that allows PHP to use the same set of methods to access both local files and remote resources.

Provide a facility for managing and querying user-defined STREAM WRAPPERS.

In this system the registered REGISTERED STREAM WRAPPERS can be seen in the below table.


PROTOCOLS MEANING
file:// LOCAL File System
http:// URL HTTP
ftp:// URL FTP
php:// I/O streams
zlib:// Data Compression stream
data:// DATA (RFC-2397)
glob:// Find Filenames
phar:// PHP Archive
ssh2:// Secure Shell 2
rar:// RAR
ogg:// Audio
expect:// Interaction of stream processes
ed48

 $params 

SUPPORTED PARAMETERS
Parameters Purpose
notification Name of user-defined callback function to be called whenever
a stream triggers a notification.
Only supported for
http:// and ftp:// stream wrappers.
options Array of options as in
context options and parameters.
ed48


  Accessing http:// or https://  


Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method.

A Host: header is sent with the request to handle name-based virtual hosts.

If you have configured a user_agent string using your php.ini file or the stream context, it will also be included in the request.

Attribute Supported
Restricted by:
allow_url_fopen
Yes
Allows Reading Yes
Allows Writing No
Allows Appending No
Allows Simultaneous Reading and Writing N/A
Supports
stat
No
Supports
unlink
No
Supports rename No
Supports
mkdir
No
Supports
rmdir
No
ed48

HTTPS is only supported when the openssl extension is enabled.


Examlpes:

http://example.com
http://example.com/file.php?var1=val1&var2=val2
http://user:[email protected]

https://example.com
https://example.com/file.php?var1=val1&var2=val2
https://user:[email protected]


<?php

Context options 
for http:// and https:// transports. 

str method

    GET
POST, or any other HTTP method
    supported by the remote server
.

    
Defaults to GET.
    
    
arr or str header

    Additional headers to be sent during request
.
    
Values in this option will override other values,
    (
such as User-agent:, Host:, and Authentication:).
    
    
str user_agent

    Value to send with User
-Agentheader.
    
This value will only be used 
    
if user-agent is not specified 
    in the header context option above
.

    
By default the user_agent php.ini setting is used.
    
    
str content

    Additional data to be sent after the headers
.
    
Typically used with POST or PUT requests.
    
    
str proxy

    URI specifying address of proxy server
.
    
    
bool request_fulluri

    When set to TRUE
the entire URI 
    will be used when constructing the request
.
    (
HTTP/1.0). 
    While 
this is a non-standard request format,
    
some proxy servers require it.

    
Defaults to FALSE.
    
    
int follow_location

    Follow Location header redirects
Set to 0 to disable.

    
Defaults to 1.
    
    
int max_redirects

    The max number of redirects to follow
.
    
Value 1 or less means 
    that no redirects are followed
.

    
Defaults to 20.
    
    
float protocol_version

    HTTP protocol version
.

    
Defaults to 1.0.           

?>

  Accessing ftp:// or ftps://  


Allows read access to existing files and creation of new files via FTP.

If the server does not support passive mode ftp, the connection will fail.

You can open files for either reading or writing, but not both simultaneously.

If the remote file already exists on the ftp server and you attempt to open it for writing but have not specified the context option overwrite, the connection will fail.

If you need to overwrite existing files over ftp, specify the overwrite option in the context and open the file for writing.

Attribute Supported
Restricted by:
allow_url_fopen
Yes
Allows Reading Yes
Allows Writing Yes
(new files/existing files with
overwrite
Allows Appending Yes
Allows Simultaneous
Reading and Writing
No
Supports
stat
filesize, filetype, file_exists, is_file and is_dir
elements only.
As of PHP 5.1.0: filemtime.
Supports
unlink
Yes
Supports
rename
Yes
Supports
mkdir
Yes
Supports
rmdir
Yes
ed48


Examlpes:

ftp://example.com/pub/file.txt
ftp://user:[email protected]/pub/file.txt

ftps://example.com/pub/file.txt
ftps://user:[email protected]/pub/file.txt


<?php

Context options 
for ftp:// and ftps:// transports. 

bool overwrite

    Allow overwriting of already existing files
    on remote server

    
Applies to write mode, (uploading), only.

    
Defaults to FALSE.
    
    
int resume_pos

    File offset at which to begin transfer

    
Applies to read mode, (downloading), only.

    
Defaults to 0 (Beginning of File).
    
    
str proxy

    Proxy FTP request via http proxy server
.
    
Applies to file read operations only.           

?>

  1 EXERCISE   

<?php

define
('PA1W''file://' __DIR__ '/adds/love.txt');

echo 
PA1W "<br><br>";

$path01 PA1W;

$fl01 fopen($path01,"r");
//  MUST EXIST

$opts01 = [ 'https' => [ 'method'=>"POST",
          
'header' => "Accept-language: en"] ]; 

$cntxt01 stream_context_set_params($fl01$opts01);


if(
$cntxt01 == TRUE)
{
echo 
'<br>STREAM CONTEXT IS SET AS EXPECTED<br><br>';
}
else
{
echo 
'<br>STREAM CONTEXT IS NOT SET AS EXPECTED<br><br>';
}

?>

 RESULT   

STREAM CONTEXT IS SET AS EXPECTED

  2 EXERCISE   

<?php

define
('PA2W''file://' __DIR__ '/adds'); 

$path02 PA2W;

echo 
$path02 "<br><br>"

$fl02 fopen($path02 '/sw7.jpg' ,"r");
//  MUST EXIST

$opts02 = [ 'https' => [ 'method'=>"POST",
          
'header' => "Accept-language: en"] ]; 

$cntxt02 stream_context_set_params($fl02$opts02);


if(
$cntxt02 == TRUE)
{
echo 
'<br>STREAM CONTEXT IS SET AS EXPECTED<br><br>';
}
else
{
echo 
'<br>STREAM CONTEXT IS NOT SET AS EXPECTED<br><br>';
}

?>

 RESULT   

STREAM CONTEXT IS SET AS EXPECTED

  3 EXERCISE   

<?php

define
('PA3W''file://' __DIR__ '/adds');  

echo 
PA3W "<br><br>"

$path03 PA3W;

$fl03 = @fopen($path03 '/sw7.png' ,"r");
//  DOES NOT EXIST

$opts03 = [ 'https' => [ 'method'=>"POST",
          
'header' => "Accept-language: en"] ]; 

$cntxt03 stream_context_set_params($fl03$opts03);

if(
$cntxt03 == TRUE)
{
echo 
'<br>STREAM CONTEXT IS SET AS EXPECTED<br><br>';
}
else
{
echo 
'<br>STREAM CONTEXT IS NOT SET AS EXPECTED<br><br>';
}

?>

 RESULT   

This code when executed in:

PHP 7.4.XX

Warning ... and the message:

STREAM CONTEXT IS NOT SET AS EXPECTED

PHP 8.0.XX

Fatal error ...