Search
 
SCRIPT & CODE EXAMPLE
 

PHP

How to post a mutlipart file using file_get_contents in php

/*
First of all, the first rule of multipart Content-Type 
is to define a boundary that will be used as a delimiter 
between each part (because as the name says, it can have 
multiple parts). The boundary can be any string that is 
not contained in the content body. I will usually use a timestamp:
*/

define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));

/*Once your boundary is defined, you must send it with the 
Content-Type header to tell the webserver what delimiter to expect:
*/
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;


// equivalent to <input type="file" name="uploaded_file"/>
define('FORM_FIELD', 'uploaded_file'); 

//Then we build the content body:

$filename = "/path/to/uploaded/file.zip";
$file_contents = file_get_contents($filename);    

$content =  "--".MULTIPART_BOUNDARY."
".
            "Content-Disposition: form-data; name="".FORM_FIELD.""; filename="".basename($filename).""
".
            "Content-Type: application/zip

".
            $file_contents."
";

// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."
".
            "Content-Disposition: form-data; name="foo"

".
            "bar
";

// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--
";
/*
As you can see, we're sending the Content-Disposition header 
with the form-data disposition, along with the name parameter 
(the form field name) and the filename parameter (the original filename). 
It is also important to send the Content-Type header with the proper 
MIME type, if you want to correctly populate the $_FILES[]['type'] thingy.

If you had multiple files to upload, you just repeat the process with 
the $content bit, with of course, a different FORM_FIELD for each file.
Now, build the context:
*/
$context = stream_context_create(array(
    'http' => array(
          'method' => 'POST',
          'header' => $header,
          'content' => $content,
    )
));

//execute request


file_get_contents('http://url/to/upload/handler', false, $context);
Comment

PREVIOUS NEXT
Code Example
Php :: en php comment convertir une date en français stackoverflow 
Php :: Problem getting updated value from child component to the parent component in a Laravel 9 with Vue 
Php :: aravel cache store does not support tagging 
Php :: laravel nova create resource 
Php :: traduction website with i18n 
Php :: SymfonyStyle 
Php :: wordpress set category front end 
Php :: cpt change link 
Php :: Limit number of words to be displayed on blog post excerpt with Laravel 
Php :: get server name php 
Php :: laravel count soft delete data 
Php :: php blade first child @foreach 
Php :: phpmyadmin timedeconnexion : a placer tt en bas dans "config.inc.php" 
Php :: random record get with pagination in karavel 8 
Php :: Comment faire en sorte que le numéro de téléphone ne soit pas un champ obligatoire dans WooCommerce 
Php :: php artisan seading 
Php :: Input sanitization to prevent XSS 
Php :: codeigniter query Profiling - To disable the profiler 
Php :: example of valid php variables 
Php :: php When I try to run my code in chrome, i see the code that I have made in phpstorm and not the function that it has to do 
Php :: remove database in codeigniter 
Php :: how to cooncet dabase eith laraavel 
Php :: php curl upload linkedin image 
Php :: php crash course could not find driver 
Php :: sail laravel mix hot 
Php :: __sleep and __wakeup 
Php :: HP officejet pro 8720 default password 
Php :: how to echo whole array php 
Php :: Fehler beim Uploadtest der ausgewählten Datei. 
Php :: show number 1 as 00001 laravel 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =