【发布时间】: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