我正在尝试编写一个上传表单,根据isset($ _ POST [])中给出的名称在S3中创建一个新文件夹。我遇到的问题是,我似乎可以使用$ _POST或$ _FILES,而不是两者,至少是顺序。
我尝试嵌套if(isset())s,但这似乎没有影响任何东西,从我正在阅读的内容来看,这样的嵌套是不受欢迎的。
编辑:问题的简单解释是在isset($ _ POST)之后上传没有做任何事情,因为它永远不会到达isset($ _ FILES)。也就是说,如果我只使用硬编码路径上传,它会将文件存储在s3中就好了。以下是相关代码的简要说明:
首先,在选择要查看的课程时,POST已完成,因此我可以获取用于存储本课程的新文件夹的名称(因此它们不会全部转储到存储桶中)。
//view description and files for that lesson
if(isset($_POST['viewlesson']))
{
$select = "SELECT * FROM lessons WHERE idlessons='$_POST[hidden]'";
$selq = mysqli_query($con, $select);
?>
<table align="center" cellpadding="0" cellspace="0" border="0">
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Subject</strong></td>
<td><strong>Grade</strong></td>
<td><strong>Tag</strong></td>
</tr>
<?php
while($row = mysqli_fetch_array($selq, MYSQLI_ASSOC))
{
echo "<form action=index.php method=post>";
echo "<tr>";
echo '<td>'.'<input type=text name=id readonly value="'.$row['idlessons'].'"></td>';
echo '<td>'.'<input type=text name=name readonly value="'.$row['Name'].'"></td>';
echo '<td>'.'<input type=text name=grade readonly value="'.$row['Grade'].'"></td>';
echo '<td>'.'<input type=text name=subject readonly value="'.$row['Subject'].'"></td>';
echo '<td>'.'<input type=text name=tag readonly value="'.$row['Tag'].'"></td>';
echo "</tr>";
}
?>
</table>
<?php
//debug to display post before upload
$tokey = $_POST['name'];
echo $tokey;
这是在“查看”选择课程后加载的表单。如果我把它从这个集中取出并在视图之前上传到它,文件将上传正常,但我必须指定我自己的文件夹。因此,我把它留在里面,因为我不希望用户能够上传到任何东西。
//upload form
?>
<center>
<form enctype="multipart/form-data" action="viewdb.php" method="POST">
<input name="file" type="file">
<input type="submit" value="Upload">
</form>
</center>
<?php
}
最后,我们将putObject方法添加到s3。选择文件并按下“上传”时会发生这种情况。我想要这个工作的方式是使用$ tokey作为新的文件夹名称,但由于isset没有首先触发,我不能这样做。
//upload file to the bucket
if(isset($_FILES['file']))
{
$file = $_FILES['file'];
$name = $file['name'];
$tmp_name = $file['tmp_name'];
$extension = explode('.', $name);
//$extenstion = strtolower(end($extenstion));
$key = md5(uniqid());
$tmp_file_name = "{$key}.{extenstion}";
$tmp_file_path = "files/{$tmp_file_name}";
move_uploaded_file($tmp_name, $tmp_file_path);
try
{
$s3->putObject([
'Bucket' => $config['s3']['bucket'],
//'Key' => "uploads/{$name}",
'Key' => "{$tokey}/{$name}",
'Body' => fopen($tmp_file_path, 'rb'),
'ACL' => 'public-read'
]);
unlink($tmp_file_path);
}
catch (S3Exception $e)
{
die("There was an error uploading your file.");
}
}
我尝试使用unset($ _ POST);在每个if语句结束时仍无济于事。有没有更好的方法来构造这个,所以如果(isset())语句可以成功运行,或者我在开玩笑说这可以做到吗?非常感谢对此的任何启发!
最佳,
-bromeo