Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why don't you just send files using <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" rel="nofollow">Data URI scheme</a> rather than implement <a href="http://www.w3.org/TR/SOAP-attachments" rel="nofollow">SoapAttachment</a> ? Here is an example : </p> <p><strong>Client</strong> </p> <pre><code>$client = new SoapClient(null, array( 'location' =&gt; "http://localhost/lab/stackoverflow/a.php?h=none", 'uri' =&gt; "http://localhost/", 'trace' =&gt; 1 )); // Method 1 Array // File to upload $file = "golf3.png"; // First Example $data = array(); $data['name'] = $file; $data['data'] = getDataURI($file, "image/png"); echo "Example 1: "; echo ($return = $client-&gt;upload($data)) ? "File Uploaded : $return bytes" : "Error Uploading Files"; // Method 2 Objects // File to upload $file = "original.png"; // Second Example $attachment = new ImageObj($file); $param = new SoapVar($attachment, SOAP_ENC_OBJECT, "ImageObj"); $param = new SoapParam($param, "param"); echo "Example 2: "; echo ($return = $client-&gt;uploadObj($attachment)) ? "File Uploaded : $return bytes" : "Error Uploading Files"; </code></pre> <p><strong>Output</strong> </p> <pre><code>Example 1: File Uploaded : 976182 bytes Example 2: File Uploaded : 233821 bytes </code></pre> <p><strong>Server</strong></p> <pre><code>class UploadService { public function upload($args) { $file = __DIR__ . "/test/" . $args['name']; return file_put_contents($file, file_get_contents($args['data'])); } public function uploadObj($args) { $file = __DIR__ . "/test/" . $args-&gt;name; $data = sprintf("data://%s;%s,%s", $args-&gt;mime, $args-&gt;encoding, $args-&gt;data); return file_put_contents($file, file_get_contents($data)); } } try { $server = new SOAPServer(NULL, array( 'uri' =&gt; 'http://localhost/' )); $server-&gt;setClass('UploadService'); $server-&gt;handle(); } catch (SOAPFault $f) { print $f-&gt;faultstring; } </code></pre> <p><strong>Client Util</strong></p> <pre><code>// Function Used function getDataURI($image, $mime = '') { return 'data: ' . (function_exists('mime_content_type') ? mime_content_type($image) : $mime) . ';base64,' . base64_encode(file_get_contents($image)); } // Simple Image Object class ImageObj{ function __construct($file, $mime = "") { $this-&gt;file = $file; $this-&gt;name = basename($file); if (function_exists('mime_content_type')) { $this-&gt;mime = mime_content_type($file); } elseif (function_exists('finfo_open')) { $this-&gt;mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file); } else { $this-&gt;mime = $mime; } $this-&gt;encoding = "base64"; $this-&gt;data = base64_encode(file_get_contents($file)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload