-2

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>';
}
}
}
?>
user3316499
  • 1
  • 1
  • 8

1 Answers1

1

You know where the file is ($location.$name), store that information in the database table as well as the name and description.

Use that to generate the URL to the file.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I can store ($location.$name) in my database and then view it in a table like I want to but I still can't download it from clicking on it in the table which is what I need. Any Ideas? – user3316499 Feb 18 '14 at 15:56
  • Figure out how the file path relates to the URI. Transform it appropriately. Stick it in an href attribute of an a element. – Quentin Feb 18 '14 at 15:57
  • I know that but I don't know how do it, thats why I asked the question on here. Any help with the code – user3316499 Feb 18 '14 at 16:00
  • 1
    You need to experiment and Google a bit on using `href`. It's not that hard and you can't expect to get "Magical answers" to suddenly fall into your lap along with a "silver platter". `LINK` something to that effect. Once you get the link into your DB, you can just query the row(s) using a `foreach` loop type of thing. @user3316499 - I believe Quentin has already explained it rather well and deserves a `+1` on top of that, and then some. ;-) – Funk Forty Niner Feb 18 '14 at 16:15