[英]Error when trying to upload multiple files to S3 using PHP
I am working on a project to be able to upload files to an Amazon S3 bucket
from a website using PHP. 我正在研究一个项目,以便能够使用PHP从网站将文件上传到
Amazon S3 bucket
。
However, I am hitting an issue where it comes with the following error: 但是,我遇到了以下错误的问题:
fopen(uploadDoc/1.jpg): failed to open stream: No such file or directory
fopen(uploadDoc / 1.jpg):无法打开流:没有此类文件或目录
I am looping through the multiple files to upload them individually like so: 我正在遍历多个文件以分别上传它们,如下所示:
if (isset($_FILES['files']))
{
for ($i = 0; $i < 10; $i++)
{
if (!empty($_FILES['files']['name'][$i]))
{
$name = $_FILES['files']['name'][$i];
$tmp_name = $_FILES['files']['tmp_name'][$i];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
if ($ext == 'zip') { $temp_file_path = "uploadZip/{$name}"; $s3Key = "zip"; }
else { $temp_file_path = "uploadDoc/{$name}"; $s3Key = "docs"; }
try
{
$body = fopen($temp_file_path, 'rb');
$s3->putObject([
'Bucket' => AWS_BUCKET,
'Key' => "{$s3Key}/{$name}",
'Body' => $body,
'ACL' => "public-read"
]);
}
catch (S3Exception $e)
{
die('There was an error uploading ' . $e->getMessage());
}
fclose($body);
unlink($temp_file_path);
}
}
}
When I have tried to upload a single file using the same code but not in a loop, it works fine, so I am really confused. 当我尝试使用相同的代码而不是循环上传单个文件时,它可以正常工作,所以我真的很困惑。
Below is the form being used to upload the files: 以下是用于上传文件的表格:
<form action="" method="post" enctype="multipart/form-data">
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input id="file" type="file" name="files[]"><br>
<input name="upload" type="submit" value="Upload" class="general-btn blue">
</form>
Any help will be greatly appreciated 任何帮助将不胜感激
I have discovered that I have made some errors in my code. 我发现自己在代码中犯了一些错误。
Firstly, I have missed one line of code from within the PHP code snippet provided above, this is: 首先,我错过了上面提供的PHP代码片段中的一行代码,这是:
if ($ext == 'zip') { $temp_file_path = "uploadZip/{$tmp_file_name}"; $s3Key = "zip"; }
else { $temp_file_path = "uploadDoc/{$tmp_file_name}"; $s3Key = "docs"; }
move_uploaded_file($tmp_name, $temp_file_path);
try
The second error made was within the HTML code snippet above, which was giving all the inputs the same file ID, by changing the IDs to have a different ID the files upload successfully with no errors 发生的第二个错误是在上述HTML代码段内,该代码段通过将ID更改为其他ID,使所有输入具有相同的文件ID,文件成功上传且没有错误
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.