【问题标题】:upload file from local website to blob storage将文件从本地网站上传到 Blob 存储
【发布时间】:2020-11-27 02:00:36
【问题描述】:

我想将文件从本地存储上传到 Azure Blob 容器。下面是失败的 PHP 代码。

我使用 XAMPP 作为本地服务器。 我创建了一个本地网页,将文件上传到本地 MySQL 服务器和 Azure blob 存储上的容器。

当我尝试上传文件时,以下代码失败。 (但是,如果我删除了上传到 azure blob 的部分代码,那么代码可以正常工作,并且文件会上传到本地 MySQL 服务器。)我在将文件上传到 Azure Blob 时遇到问题

<?php
  require_once "vendor/autoload.php";
  use MicrosoftAzure\Storage\Blob\BlobRestProxy;
  use MicrosoftAzure\Storage\Common\ServiceException;
  
  // If the upload button is clicked ... 
  if (isset($_POST['upload'])) {

    //upload to azure blob container

    //Enter deployment key
    $connectionString = ""; 

    $blobClient = BlobRestProxy::createBlobService($connectionString);
    $file_name = $_FILES['myFile']['name'];
    $tmp = explode('.' , $file_name);
    $extension = end($tmp);
    $file_name = mt_rand().".".$entension;
    $file_tmp = $FILES['myFile']['tmp_name'];


    $containerName = 'new'; //Enter Container Name
    $keyname = $file_name;
    $content = fopen($file_tmp, "r");
    $blobClient = BlobRestProxy::createBlobService($connectionString);

       #upload file as a block blob
       //*upload blob*
       $blobClient->createBlockBlob($containerName, $file_name, $content);
       echo "Blob upload successful";

    //upload to local mysql server
    $filename = $_FILES["uploadfile"]["name"]; 
    $tempname = $_FILES["uploadfile"]["tmp_name"];     
        $folder = "image/".$filename; 
          
    $db = mysqli_connect("localhost", "root", "", "siddemo"); 
  
        //Get all the submitted data from the form
        $sql = "INSERT INTO images (filename) 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 images");
?> 

【问题讨论】:

  • 能否提供错误信息?
  • @JimXu 感谢您的评论。显然,我看到一个白色的空白屏幕作为 Web 浏览器中的输出。 (不显示错误)
  • @SiddarthBhaire 我建议你调试你的应用程序来检查你不能运行哪一行代码
  • @JimXu require_once "vendor/autoload.php";是打破代码的行
  • @SiddarthBhaire 你能告诉我你是如何安装 azure blob SDK 的吗?您缺少的接缝正在运行 composer install

标签: php azure


【解决方案1】:

我将我的建议总结如下

如果我们想使用 PHP Azure Blob SDK 将文件上传到 Azure Blob Storage,请参考以下步骤

  1. 在项目的根目录中创建一个名为 composer.json 的文件
{
  "require": {
    "microsoft/azure-storage-blob": "*"
  }
}
  1. 在您的项目根目录中下载composer.phar

  2. 安装sdk

php composer.phar install
  1. 代码
     // name of the uploaded file
     $filename = $_FILES['myfile']['name'];
    // the physical file on a temporary uploads directory on the server
    $file = $_FILES['myfile']['tmp_name'];
    $size = $_FILES['myfile']['size'];
    $connectionString="";
    $blobClient = BlobRestProxy::createBlobService($connectionString);
    $content = fopen($file, "r"); 
    //Upload blob
    $containerName = "upload";;
    $blobClient->createBlockBlob($containerName, $filename, $content);
    echo "File uploaded Azure Blob successfully";

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 2017-09-21
    • 1970-01-01
    • 2021-03-07
    • 2021-05-23
    • 2017-01-24
    • 2017-08-19
    • 2019-10-30
    • 2019-04-10
    相关资源
    最近更新 更多