Welcome to zewaren.net. This site presents myself and mostly archives the solutions to some problems I once had.

How to send a file via HTTP specifying a custom filename, using PHP

Not so frequently asked questions and stuff: 
\n";
} else {
 srand((double)microtime()*1000000);
 $boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
    
    $data = "";
 
    $header = "";
    $header .= "POST $service HTTP/1.1\r\n";
    $header .= "Host: $host\r\n";
    $header .= "Content-Type: multipart/form-data; boundary=$boundary\r\n"; 
    
    $APOST = array("a" => "b", "c" => "d");
    
        foreach($APOST AS $index => $value){
            $data .="--$boundary\r\n";
            $data .= "Content-Disposition: form-data; name=\"".$index."\"\r\n";
            $data .= "\r\n".$value."\r\n";
            $data .="--$boundary\r\n";
        } 
    
  $data .= "--$boundary\r\n";
   $content_file = join("", file($filetosend));
    $content_type = "";
 $file_name = $filenametosend;
   $data .="Content-Disposition: form-data; name=\"userfile\"; filename=\"$file_name\"\r\n";
 $data .= "Content-Type: $content_type\r\n\r\n";
 $data .= "".$content_file."\r\n";
 $data .="--$boundary--\r\n";
        
    $header .= "Content-length: " . strlen($data); 
    $header .= "\r\n\r\n";
    $out = $header.$data;
    
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 4096);
    }
    fclose($fp);
}
?>

Pages

Subscribe to Front page feed