0

I need to upload audio files in wav format to an Azure container using the Azure SDK for PHP but the content of the wav does not upload. Indeed, I only have a 0 bytes .wav file in my container so i'm not able to use it.

I have tested several codes but this is the best one I have. I am not an expert in PHP but I force him to use this language to integrate it into a CRM.

When I load a text file it also uploads empty so the problem doesn't come from the way I read the file.

Thanks a lot for your help.

    <?php

    require_once 'vendor/autoload.php';

    use WindowsAzure\Common\ServicesBuilder;
    use MicrosoftAzure\Storage\Blob\BlobRestProxy;
    use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
    use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
    use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
    use MicrosoftAzure\Storage\Blob\Models\PublicAccessType;

    $connectionString = "DefaultEndpointsProtocol=https;AccountName=".getenv('ACCOUNT_NAME').";AccountKey=".getenv('ACCOUNT_KEY');

    // Create blob client.
    $blobClient = BlobRestProxy::createBlobService($connectionString);

    $fileToUpload = "audio.wav";

    if (!isset($_GET["Cleanup"])) {

          $containerName = "cs-blob-input";

        try {

            // Getting local file so that we can upload it to Azure
            $myfile = fopen($fileToUpload, "w") or die("Unable to open file!");
            fclose($myfile);

            # Upload file as a block blob
            echo "Uploading BlockBlob: ".PHP_EOL;


            $content = fopen($fileToUpload, "r");

            //Upload blob
            $blobClient->createBlockBlob($containerName, $fileToUpload, $content);

            // List blobs.
            $listBlobsOptions = new ListBlobsOptions();
            echo "These are the blobs present in the container: ".PHP_EOL;

            do{
                $result = $blobClient->listBlobs($containerName, $listBlobsOptions);
                foreach ($result->getBlobs() as $blob)
                {
                    echo $blob->getName().PHP_EOL;
                }

                $listBlobsOptions->setContinuationToken($result->getContinuationToken());
            } while($result->getContinuationToken());

        }
        catch(ServiceException $e){
            $code = $e->getCode();
            $error_message = $e->getMessage();
            echo $code.": ".$error_message."<br />";
        }
        catch(InvalidArgumentTypeException $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->getCode();
            $error_message = $e->getMessage();
            echo $code.": ".$error_message."<br />";
        }
    }
    else
    {

        try{
            // Delete container.
            echo "Deleting Container".PHP_EOL;
            echo $_GET["containerName"].PHP_EOL;
            echo "<br />";
            $blobClient->deleteContainer($_GET["containerName"]);
        }
        catch(ServiceException $e){
            $code = $e->getCode();
            $error_message = $e->getMessage();
            echo $code.": ".$error_message."<br />";
        }
    }
    ?>

1 Answer 1

1

The problem is here:

// Getting local file so that we can upload it to Azure
$myfile = fopen($fileToUpload, "w") or die("Unable to open file!");
fclose($myfile);

What is happening is, you open a file "audio.wav" for writing. Based on official docs (See the mode parameter 'w') your file will be created if it not exists. Then, if it exists, it will be truncated to zero size. Then you close the file.

After that you do:

$content = fopen($fileToUpload, "r");

Which reads an empty file. So the uploaded content is => 0 in size

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Anton, thank your for your answer. When I open the file for Reading instead of Writing, i can upload the content of a text file but not an audio. Do you know if fopen works to open a wav file ?
@MatthieuRousseau you need to open a file in a binary read mode fopen($fileToUpload, "rb") -> the mode is set to rb . Keep in mind that fopen just opens the file and return a handler to you, so after that you need to do something like fread . See the second example here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.