title: Upload and Download images in Azure Blob Storage
body: <p>I am trying to upload an image from a HTML form to Azure Blob Storage using the Azure PHP SDK. The problem appears when I try to download the image. The result page can be seen at the bottom of the post.</p>
  
  <p>I store the image using the temporary name and I think this is one of the two problems. I am not sure but the second problem is to when downloading the image. Do I have to convert it from the getContentStream() to image  ?</p>
  
  <blockquote>
    <p>$_FILES['driverLicenseFront']['tmp_name']</p>
  </blockquote>
  
  <p>This is the html form to upload the image:</p>
  
  <pre><code>&lt;form role="form" method="POST" action="{path_to_controller}" data-toggle="validator" enctype="multipart/form-data"&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="driverLicenseFront"&gt;Upload Driver's License(Front)&lt;/label&gt;
          &lt;input type="file" id="driverLicenseFront" name="driverLicenseFront"&gt;
      &lt;/div&gt;
      &lt;submit button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>In the controller I store the file like this:</p>
  
  <pre><code>// First check if there is a container
  $blob = New Blob($_SESSION['userid']);
  $blob-&gt;createContainerIfNotExists();
  // Upload image to Azure Blob Storage
  $content = fopen($_FILES['driverLicenseFront']['tmp_name'].'', "r");
  $blob-&gt;uploadToContainer($content,'DriverLicenseFrontSide');
  </code></pre>
  
  <blockquote>
    <p><strong>Blob</strong> is my custom class to handle blobs</p>
  </blockquote>
  
  <p>I need to download the file using a link:</p>
  
  <pre><code>&lt;a href="../controller/blobs.php?blob_name=DriverLicenseFrontSide" target="_new"&gt;Download&lt;/a&gt;
  </code></pre>
  
  <p>I catch the request in the controller:</p>
  
  <pre><code>if(isset($_GET['blob_name'])){
      $blob = New Blob($_SESSION['userid']);
      $blob-&gt;downloadBlob($_GET['blob_name']);
  } 
  </code></pre>
  
  <p>The function of the Blob class:</p>
  
  <pre><code>public function downloadBlob($blob_name){
      try {
          // Get blob.
          $blob = $this-&gt;blobRestProxy-&gt;getBlob($this-&gt;containerName, $blob_name.'.jpg');
          fpassthru($blob-&gt;getContentStream());
      }
      catch(ServiceException $e){
          // Handle exception based on error codes and messages.
          // Error codes and messages are here:
          // http://msdn.microsoft.com/library/azure/dd179439.aspx
          $code = $e-&gt;getCode();
          $error_message = $e-&gt;getMessage();
          echo $code.": ".$error_message."&lt;br /&gt;";
      }
  }
  </code></pre>
  
  <p>The result:</p>
  
  <blockquote>
    <p>����JFIF,,���ICC_PROFILE�mntrRGB XYZ
    �$acsp���-)�=ޯ�U�xB��ʃ9 descDybXYZ�bTRC�dmdd  ��gXYZ
    hgTRC�lumi |meas �$bkpt �rXYZ �rTRC�tech �vued
    ��wtptpcprt�7chad�,descsRGB IEC61966-2-1 black scaledXYZ
    $����curv #(-27;@EJOTY^chmrw|�������������������������...</p>
  </blockquote>