我完全陷入了我预期的简单问题。当$ filepath变量是要上载的文件的绝对路径并在脚本中定义时,以下Amazon AWS SDK2脚本将文件成功上载到AWS S3。
但是,当我尝试使用一个简单的表单来选择一个文件并将其作为变量传递给AWS脚本时,我将自己束缚在一起。例如,尝试了$ _FILES ['input1'] ['name'],$ _FILES ['input1'] ['tmp_name'],realpath(),file_get_contents()来访问完整路径。也尝试使用javascript,但路径然后被浏览器改为'fakepath'。我显然不明白元素中的type =“file”。
所以,我的问题是:如何让用户从本地磁盘中选择文件并(使用POST操作?)将文件的路径作为变量传递给AWS SDK2上传脚本?我的简单测试表也包含在下面。
简单形式:
<body>
<form id="form1" action="SDK2_script_process.php" method="post" enctype="multipart/form-data">
<input type="file" id="input1" name="input1" />
<input type="submit" name="submit" id="submit" value="Search" >
</form> </body>
流程脚本(SDK2_script_process.php):
<?php
//CONNECTS TO AWS V2 SDK AND UPLOADS FILE
//Literal path to aws.phar file
require_once 'AWSSDKforPHP/aws.phar';
use Aws\S3\S3Client;
use Guzzle\Http\EntityBody;
// Instantiate the S3 client with AWS credentials and optional desired AWS region
$client = S3Client::factory(array(
'key' => 'MYKEY',
'secret' => MYSECRETKEY'
));
//Name of bucket on S3
$bucket = 'mybucket';
//Filename to be saved in S3 Bucket
$filename = "/directoryA/directoryB/filename.extension";
//Literal filepath to file I want to save - THIS WORKS
// $filepath = '../../directory1/directory2/directory3/filename.extension';
// Filepath from simple form - DOES NOT WORK
$filepath = $_FILES['input1']['tmp_name'];
$result = $client->putObject(array(
'Bucket' => $bucket,
'Key' => $filename,
'SourceFile' => $filepath,
'Metadata' => array(
'title' => 'This is the title metadata',
'artist' => 'This is the artist metadata'
)
));
// HEAD object confirms success
$headers = $client->headObject(array(
"Bucket" => $bucket,
"Key" => $filename
));
//print_r($headers->toArray());
echo $result['ObjectURL'];
echo $headers['Metadata']['artist'];
?>
感谢任何帮助,或指向使用此SDK2脚本的不同方式。