我正在使用Microsoft Azure服务开发Web应用程序。我正在使用this链接将图像上传到我的Blob存储,但是文档中缺少一些零碎的片段,这导致了某些问题。例如,我们如何定义变量$ fileToUpload并将其与变量$ myfile关联?另外,如果我们希望在应用程序运行时选择从系统上载文件,那么我们应该在哪个变量中拥有映像的绝对文件路径?到目前为止,我编写的用于上传图片的代码如下:
function imageupload(string $current_user){
$pt_id = $_POST['patientid'];
if (strcmp($pt_id,"")==0){
echo "Select PatientID to continue";
}else{
$accountexists = load($current_user, $pt_id);
if ($accountexists == 0){
$error = "Patient Id does not exist";
echo $error;
}else{
$img_path = $_POST['uploadI'];
if (strcmp($img_path, "")==0){
echo "Enter valid image path <br />";
}
else{
require_once 'vendor/autoload.php';
$connectionString = '****';
// Create blob client.
$blobClient = BlobRestProxy::createBlobService($connectionString);
# Create the BlobService that represents the Blob service for the storage account
$containerName = $current_user;
$num = rand(10,10000);
$num2 = (string) $num;
$fileToUpload = $pt_id."_".$num2;
# Upload file as a block blob
echo "Uploading image: ".PHP_EOL;
echo $img_path;
echo "<br />";
$content = fopen($img_path, "r");
//Upload blob
$blobClient->createBlockBlob($containerName, $fileToUpload, $content);
echo "Image uploaded successfully! <br />";
}
}
}
}
在这段代码中,我将图像的路径存储在变量$ img_path中。这是我稍后用于显示图像的代码:
// Gets all the images from the hospital's container and filters out the images corresponding to a particular patient.
// For the patient, the image would be saved on cloud with name as: <patientid>_<some random number>
// Eg- patient id= pt1 , so image name could be- pt1_3682
function uploadedimage(string $current_user, string $HospitalId, string $PatientId){
$containerName = $HospitalId;
$url = "";
try{
// Get blob.
require_once 'vendor/autoload.php';
// use MicrosoftAzure\Storage\Common\ServicesBuilder;
$connectionString = '****';
$blobClient = BlobRestProxy::createBlobService($connectionString);
$blob_list = $blobClient->listBlobs($containerName);
$blobs = $blob_list->getBlobs();
$strlen = strlen($PatientId);
$array_user = array();
// echo "These are the blobs present in the container: ";
foreach($blobs as $blob)
{
$name = $blob->getName();
$namesub = substr($name,0, $strlen);
if (strcmp($namesub, $PatientId) == 0){
array_push($array_user, $blob);
}
}
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
$url = "";
try {
foreach($array_user as $blob)
{
$name = $blob->getName();
$url = $blob->getUrl();
echo "<img src=\"".$url."\" alt=\"image post\"></br></br>";
}
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
}
但是我似乎无法在指定的html中查看图像。我得到的只是默认文本“ image post”。因此,我得出的结论是我的代码中缺少某些内容,原因是这些图像没有被上传,但我似乎无法从azure教程中弄清楚。
编辑:我检查了我的Blob属性,它们的大小均为0,这意味着它们未正确上传。我也意识到有些图像和我的git文件夹中的代码一起被推送到云中。因此,如果在我的Azure代码所在的目录中已经存在名称为 abc.png 的特定图像,并且在从Web应用程序上传该图像时,如果在路径文本框中仅指定 abc.png ,该图片会以正确的大小上传到我的容器中,但是如果该图片出现在桌面上并且我为该图片(或某些图片)指定了本地系统的完整文件路径其他没有在Azure上的git目录中显示为 C:/Desktop/abc.png 的图像,则仅将其上传为0字节图像(实际上是未正确上传)。如果它无法从我的系统中拍摄图像,而只能拍摄它的git目录中已经存在的图像,那会是什么问题?