作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将文件从本地存储上传到 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存储,请引用以下步骤
{
"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/
我是一名优秀的程序员,十分优秀!