I have got working a simple file upload thing for my website by uploading each file on to my server. However, now I want the users who upload the file to also add a description and date of upload of the file in to a MySQL Database.
I Can do all of this fine at the moment all I can do is list the files or list the description and date separately. Ideally I would like to display it all in one table with Headings of Date, Description and File but I'm not sure how I go about linking the file link to the description.
If I haven't been very clear about it I'm happy to explain again.
Code: adddocument.php
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Date:<br><input type="text" name="DocDate" id="datepicker"></p>
<p>Description: (max 100 characters):<br><textarea name="Description" rows="4" cols="50" maxlength="100"></textarea></p>
<p>File you wish to upload: <br><input type="file" name="file"></p>
<p><input type="Submit" value="Submit"></p>
</form>
Upload.php
<?php
$name = $_FILES['file']['name'];
$extension =strtolower(substr($name, strpos($name, '.') + 1));
$size = $_FILES['file']['size'];
$max_size= 2097152;
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name)) {
if ($extension=='jpg'||$extension=='jpeg'||$extension=='pdf'||$extension=='docx'||$extension=='doc'){
if ($size<=$max_size){
$location='uploads/';
if (move_uploaded_file($tmp_name, $location.$name)){
echo 'File has been Uploaded';
} else {
echo 'There was an error';
}
} else { echo 'File must be 2mb or less';
} } else { echo 'Invalid File type';}
} else {
echo 'Please chose a file.';
}
}
?>
<?php
$con=mysqli_connect("localhost","pytsuemg_brodie","brodie","pytsuemg_brodie");
$sql="INSERT INTO Documents (DocDate, Description)
VALUES('$_POST[DocDate]','$_POST[Description]')";
mysqli_query($con, $sql);
/* commit transaction */
if (!mysqli_commit($con)) {
print("Transaction commit failed\n");
exit();
}
/* close connection */
mysqli_close($con);
?>
Currently I just list all of the files uploaded using this code.
<?php
$directory ='uploads';
if ($handle = opendir($directory.'/')){
while ($file = readdir($handle)) {
if ($file!='.'&&$file!='..') {
echo '<a href="'.$directory.'/'.$file.'">'.$file. '</a><br>';
}
}
}
?>