gpt4 book ai didi

php - 将文件从本地网站上传到 Blob 存储

转载 作者:行者123 更新时间:2023-12-03 05:31:37 26 4
gpt4 key购买 nike

我想将文件从本地存储上传到 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");
?>

最佳答案

我总结我的建议如下

如果我们想使用PHP Azure Blob SDK将文件上传到Azure Blob存储,请引用以下步骤

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

  • 安装sdk

  • php composer.phar install
  • 代码
  •      // 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";

    关于php - 将文件从本地网站上传到 Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64971674/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com