I'm trying to write an upload form that creates a new folder in S3 based off of the name given in the isset($_POST[]). The problem I'm having is that it seems I can either use $_POST or $_FILES, not both, at least sequentially.
I tried nesting the if(isset())s, but that seems to not affect anything and from what I'm reading, nesting like this is frowned upon.
Here is a rundown of the code in question:
First, while selecting a lesson to view, a POST is done so I can get a name for the new folder to store this lesson (so they aren't all dumped in a bucket).
//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;
Here is the form that loads once a lesson is selected to "View". If I take it out of this isset and upload to it before the view, the file will upload fine, but I have to specify my own folder. As such, I left it inside as I don't want the user to be able to upload to nothing.
//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
}
Lastly, we have the putObject method to s3. This is what happens when a file is selected and "Upload" is pressed. The way I wanted this to work was by using the $tokey as the new folder name, but since the isset isn't firing in the first place, I can't do that.
//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.");
}
}
I tried using unset($_POST); at the end of each if statement still to no avail. Is there a better way to structure this so both if(isset()) statements can run successfully, or am I kidding myself that this can be done? Any enlightenment on this is greatly appreciated!
Best,
-bromeo
Aucun commentaire:
Enregistrer un commentaire