gpt4 book ai didi

php - Blob 文件上传 - 我正在将图像文件从本地系统上传到 azure blob 容器。上传后,文件大小显示为0B,无扩展名

转载 作者:行者123 更新时间:2023-12-03 02:31:18 27 4
gpt4 key购买 nike

我编写了一段 PHP 代码,用于将文件从 azure web 应用程序(使用浏览器中的上传按钮)上传到 azure blob 容器。我成功地将文件上传到 blob 容器,但是,上传后,文件以 0 字节的形式位于 azure blob 中,没有任何扩展名。我也无法查看该文件,并显示以下消息“文件‘1721405003’可能无法正确呈现,因为它包含无法识别的扩展名”。

下面是我的 php 代码,用于将文件上传到 blob -

<?php
error_reporting(0);
?>
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\ServiceException;
use WindowsAzure\Common\ServicesBuilder;

// If upload button is clicked ...
if (isset($_POST['upload'])) {

//upload to azure blob container
$connectionString = "conn key"; //Enter deployment key
$containerName = 'sidblobcontainer';
$blobClient = BlobRestProxy::createBlobService($connectionString);
$file_name = $_FILES['myFile']['name'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$content = fopen($file_name, "r");
$blob_name = "myblob".'.'.$ext;

try {
//Upload blob
$blobClient->createBlockBlob($containerName, $blob_name, $content);
echo "successfull";

} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message.PHP_EOL;
}


//upload to azure mysql database server
$filename = $_FILES["myFile"]["name"];
$tempname = $_FILES["myFile"]["tmp_name"];
$folder = "image/".$filename;

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = mysqli_connect("demoserversid.mysql.database.azure.com",
"siddarth@demoserversid", "****", "images");

// Get all the submitted data from the form
$sql = "INSERT INTO demo (siddemo) 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 demo");
?>

<!DOCTYPE html>
<html>
<head>
<style> #content{
width: 50%;
margin: 20px auto;
border: 1px solid #cbcbcb;
}
form{
width: 50%;
margin: 20px auto;
}
form div{
margin-top: 5px;
}
#img_div{
width: 80%;
padding: 5px;
margin: 15px auto;
border: 1px solid #cbcbcb;
}
#img_div:after{
content: "";
display: block;
clear: both;
}
img{
float: left;
margin: 5px;
width: 300px;
height: 140px;
}</style>
<title>Image Upload</title>
<body>Upload to azure</body>
<link rel="stylesheet" type= "text/css" href ="style.css"/>
<div id="content">

<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="uploadfile" value=""/>

<div>
<button type="submit" name="upload">UPLOAD</button>
</div>
</form>
</div>
</body>
</html>

Here is the message that I see in blob container

最佳答案

我删除了与 mysql 相关的部分,下面的代码对我有用:

<?php
error_reporting(0);
?>
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\ServiceException;
use WindowsAzure\Common\ServicesBuilder;

// If upload button is clicked ...
if (isset($_POST['upload'])) {


$connectionString = "<conn str>"; //Enter deployment key
$containerName = '<container name>';
$blobClient = BlobRestProxy::createBlobService($connectionString);

$file_name = $_FILES['fileToUpload']['name'];
error_log($file_name);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$content = fopen($_FILES['fileToUpload']["tmp_name"], "r");
$blob_name = "myblob".'.'.$ext;

try {
//Upload blob
$blobClient->createBlockBlob($containerName, $blob_name, $content);
echo "successfull";

} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message.PHP_EOL;
}
}

?>


<!DOCTYPE html>
<html>
<head>
<style> #content{
width: 50%;
margin: 20px auto;
border: 1px solid #cbcbcb;
}
form{
width: 50%;
margin: 20px auto;
}
form div{
margin-top: 5px;
}
#img_div{
width: 80%;
padding: 5px;
margin: 15px auto;
border: 1px solid #cbcbcb;
}
#img_div:after{
content: "";
display: block;
clear: both;
}
img{
float: left;
margin: 5px;
width: 300px;
height: 140px;
}</style>
<title>Image Upload</title>
<body>Upload to azure</body>
<link rel="stylesheet" type= "text/css" href ="style.css"/>
<div id="content">

<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload"/>

<div>
<button type="submit" name="upload">UPLOAD</button>
</div>
</form>
</div>
</body>
</html>

结果:

enter image description here

关于php - Blob 文件上传 - 我正在将图像文件从本地系统上传到 azure blob 容器。上传后,文件大小显示为0B,无扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65050161/

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