Skip to content
Advertisement

Blob file upload – i am uploading image file from local system to azure blob container. Once uploaded, the file size shows as 0B with no extension

I have written a PHP code to upload files from the azure web app (using an upload button from a browser) to azure blob container. I am successfully able to upload the files to the blob container, however, once uploaded the file sits in azure blob as 0 bytes without any extension. I am unable to view the file as well and the following message is shown ‘The file ‘1721405003’ may not render correctly as it contains an unrecognized extension’.

Below is my php code to upload files to blob –

<?php
    error_reporting(0); 
?>
<?php
  require_once "vendor/autoload.php";
  use MicrosoftAzureStorageBlobBlobRestProxy;
  use MicrosoftAzureStorageCommonServiceException;
  use WindowsAzureCommonServicesBuilder;
  
  // If upload button is clicked ... 
  if (isset($_POST['upload'])) {

  //upload to azure blob container
  $connectionString = "conn key"; //Enter deployment key
  $containerName = 'sidblobcontainer';
  $blobClient = BlobRestProxy::createBlobService($connectionString);
  $file_name = $_FILES['myFile']['name'];
  $ext = pathinfo($file_name, PATHINFO_EXTENSION);
  $content = fopen($file_name, "r");
  $blob_name = "myblob".'.'.$ext;

  try {
    //Upload blob
    $blobClient->createBlockBlob($containerName, $blob_name, $content);
    echo "successfull";
    
  } catch (ServiceException $e) {
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message.PHP_EOL;
 }


    //upload to azure mysql database server
    $filename = $_FILES["myFile"]["name"]; 
    $tempname = $_FILES["myFile"]["tmp_name"];     
    $folder = "image/".$filename; 
        
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $db = mysqli_connect("demoserversid.mysql.database.azure.com", 
          "siddarth@demoserversid", "****", "images"); 
  
        // Get all the submitted data from the form 
        $sql = "INSERT INTO demo (siddemo) VALUES ('$filename')"; 
  
        // Execute query 
        mysqli_query($db, $sql); 
          
        // Now let's move the uploaded image into the folder: image 
        if (move_uploaded_file($tempname, $folder))  { 
            $msg = "Image uploaded successfully"; 
        }else{ 
            $msg = "Failed to upload image"; 
      }
  } 
  $result = mysqli_query($db, "SELECT * FROM demo");
?> 
  
<!DOCTYPE html> 
<html> 
<head>
<style> #content{ 
    width: 50%; 
    margin: 20px auto; 
    border: 1px solid #cbcbcb; 
} 
form{ 
    width: 50%; 
    margin: 20px auto; 
} 
form div{ 
    margin-top: 5px; 
} 
#img_div{ 
    width: 80%; 
    padding: 5px; 
    margin: 15px auto; 
    border: 1px solid #cbcbcb; 
} 
#img_div:after{ 
    content: ""; 
    display: block; 
    clear: both; 
} 
img{ 
    float: left; 
    margin: 5px; 
    width: 300px; 
    height: 140px; 
}</style>
<title>Image Upload</title> 
<body>Upload to azure</body>
<link rel="stylesheet" type= "text/css" href ="style.css"/> 
<div id="content"> 
  
  <form method="POST" action="" enctype="multipart/form-data"> 
      <input type="file" name="uploadfile" value=""/> 
        
      <div> 
          <button type="submit" name="upload">UPLOAD</button> 
        </div> 
  </form> 
</div> 
</body> 
</html>

Here is the message that I see in blob container

Advertisement

Answer

I removed the part that related to mysql and the code below works for me:

<?php
    error_reporting(0); 
?>
<?php
  require_once "vendor/autoload.php";
  use MicrosoftAzureStorageBlobBlobRestProxy;
  use MicrosoftAzureStorageCommonServiceException;
  use WindowsAzureCommonServicesBuilder;
  
  // If upload button is clicked ... 
  if (isset($_POST['upload'])) {
      
 
  $connectionString = "<conn str>"; //Enter deployment key
  $containerName = '<container name>';
  $blobClient = BlobRestProxy::createBlobService($connectionString);

  $file_name = $_FILES['fileToUpload']['name'];
  error_log($file_name);
  $ext = pathinfo($file_name, PATHINFO_EXTENSION);
  $content = fopen($_FILES['fileToUpload']["tmp_name"], "r");
  $blob_name = "myblob".'.'.$ext;

  try {
    //Upload blob
    $blobClient->createBlockBlob($containerName, $blob_name, $content);
    echo "successfull";
    
  } catch (ServiceException $e) {
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message.PHP_EOL;
 }
} 

?> 


<!DOCTYPE html> 
<html> 
<head>
<style> #content{ 
    width: 50%; 
    margin: 20px auto; 
    border: 1px solid #cbcbcb; 
} 
form{ 
    width: 50%; 
    margin: 20px auto; 
} 
form div{ 
    margin-top: 5px; 
} 
#img_div{ 
    width: 80%; 
    padding: 5px; 
    margin: 15px auto; 
    border: 1px solid #cbcbcb; 
} 
#img_div:after{ 
    content: ""; 
    display: block; 
    clear: both; 
} 
img{ 
    float: left; 
    margin: 5px; 
    width: 300px; 
    height: 140px; 
}</style>
<title>Image Upload</title> 
<body>Upload to azure</body>
<link rel="stylesheet" type= "text/css" href ="style.css"/> 
<div id="content"> 

  <form method="POST" action="" enctype="multipart/form-data"> 
      <input type="file" name="fileToUpload" id="fileToUpload"/> 
        
      <div> 
          <button type="submit" name="upload">UPLOAD</button> 
        </div> 
  </form> 
</div> 
</body>
</html>

Result:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement