How Do I Add Title, Description, & Keywords Form To This?

Viewed 270

Hello I would like to know how to add a description, title, & keywords to my file upload script? I already have the file upload working.

So I want people to be able to enter a title and description and keywords and save it to the database.

P.S: I already have the file upload system working. User uploads file saves it to a temp folder and uploads it to database so you don’t need to mess with the file uploading part just adding the title, keywords, & description.

File Upload Script: This allows file uploads.

    <?php
<?php 
include_once 'dbconnect.php';

// fetch files
$sql = "select filename from tbl_files";
$result = mysqli_query($con, $sql);
?>
<?php
session_start();
include_once "vendor/autoload.php";
$page = new membership\Page(1);
if ($page->isValid() == true) {
    ?>

    <center>
        <div class='container'>
            <div class='row'>
                <div class='col-xs-8 col-xs-offset-2 well'>
                    <form action='upload.php' method='post' enctype='multipart/form-data'>
                        <legend>Select File to Upload:</legend>
                        <div class='form-group'>
<input type='textname' name='title' />
<input type='textname' name='desc' />
                            <input type='file' name='file1' />
                        </div>
                        <div class='form-group'>
                            <input type='submit' name='submit' value='Upload' class='btn btn-info'/>
                        </div>
                        <?php if (isset($_GET['st'])) { ?>
                            <div class='alert alert-danger text-center'>
                                <?php
                                if ($_GET['st'] == "success") {
                                    echo "File Uploaded Successfully!";
                                } else {
                                    echo 'Invalid File Extension!';
                                }
                                ?>
                            </div>
                        <?php } ?>
                    </form></div></div></center>

<?php } ?>

upload.php: This is the file that controls the file upload.

    <?php include('dbconnect.php'); ?>
<?php
//check if form is submitted
if (isset($_POST['submit']))
{
    $filename = $_FILES['file1']['name'];

    //upload file
    if($filename != '')
    {
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $allowed = ['zip', 'rar', 'php', 'html', 'sql'];

        //check if file type is valid
        if (in_array($ext, $allowed))
        {
            // get last record id
            $sql = 'select max(id) as id from tbl_files';
            $result = mysqli_query($con, $sql);
            if (count($result) > 0)
            {
                $row = mysqli_fetch_array($result);
                $filename = ($row['id']+1) . '-' . $filename;
            }
            else
                $filename = '1' . '-' . $filename;

            //set target directory
            $path = 'uploads/';

            $created = @date('Y-m-d H:i:s');
            move_uploaded_file($_FILES['file1']['tmp_name'],($path . $filename));

            // insert file details into database
            $sql = "INSERT INTO tbl_files(filename, created) VALUES('$filename', '$created')";
            mysqli_query($con, $sql);
            header("Location: new-project.html?st=success");
        }
        else
        {
            header("Location: new-project.html?st=error");
        }
    }
    else
        header("Location: new-project.html");
}
?>

MySQL:

    CREATE TABLE IF NOT EXISTS `tbl_files` (
  `id` int(9) NOT NULL AUTO_INCREMENT,
  `filename` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Also I need help on creating the tables for the keywords, title, & description.


EDIT: : So I got everything working but now I can’t get the title to display on the page it’s just blank. Anyway here’s the code for the project display:

    <?php
    include_once 'dbconnect.php';

    // fetch files
    $sql = "select filename from tbl_files";
    $result = mysqli_query($con, $sql);
    ?>
    <div class="gboxtop"></div>

    <div div="button-pro">
    <button><a href="new-project.html">New Project</a></button>
    </div>
      <div class="left">
        <div class="left_articles">

          <h2><a href="#">Lastest Projects</a></h2>

    <div class="row">
            <div class="col-xs-8 col-xs-offset-2">
    <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>File Name</th>
                            <th>View</th>
                            <th>Download</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $i = 1;
                    while($row = mysqli_fetch_array($result)) { ?>
                    <tr>
                        <td><?php echo $i++; ?></td>
                        <td><?php echo $row['title']; ?></td>
                        <td><a href="uploads/<?php echo $row['filename']; ?>" target="_blank">View</a></td>
                        <td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</td>
                    </tr>
                    <?php } ?>
                    </tbody>
                </table>
    </div></div></div>
</div></div></div></div>

//Start Table Style  
    <style>table.blueTable {
      border: 1px solid #1C6EA4;
      background-color: #EEEEEE;
      width: 100%;
      text-align: left;
      border-collapse: collapse;
    }
    table.blueTable td, table.blueTable th {
      border: 1px solid #AAAAAA;
      padding: 3px 2px;
    }
    table.blueTable tbody td {
      font-size: 13px;
    }
    table.blueTable tr:nth-child(even) {
      background: #D0E4F5;
    }
    table.blueTable thead {
      background: #1C6EA4;
      background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
      background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
      background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
      border-bottom: 2px solid #444444;
    }
    table.blueTable thead th {
      font-size: 15px;
      font-weight: bold;
      color: #FFFFFF;
      border-left: 2px solid #D0E4F5;
    }
    table.blueTable thead th:first-child {
      border-left: none;
    }

    table.blueTable tfoot {
      font-size: 14px;
      font-weight: bold;
      color: #FFFFFF;
      background: #D0E4F5;
    }
    .button-pro {
    float: right;
    }
    </style>
1 Answers

run:

ALTER TABLE tbl_files ADD title VARCHAR(255) AFTER created;

edit:

<input type='text' name='title' maxlength="255"/>

add before // insert file details into database:

$title = '';
if(!empty($_POST['title']))
{
   $title = mysqli_real_escape_string($con, $_POST['title']);
}

edit:

$sql = "INSERT INTO tbl_files(filename, created, title) VALUES('$filename', '$created', '$title')";

the rest by analogy

Related