如何在提交后创建虚荣URL

时间:2017-10-01 00:55:30

标签: php mysql

您好,我有一个网站设置,用户可以提交项目,并将其添加到上传文件夹和数据库供用户下载,我想拥有它,所以一旦用户提交新项目,它就会创建一个像“ sitename.com/projects.html? projectname “或类似内容。我的代码如下。

P.S我网站上的所有内容都需要了解如何创建网址。

projects.html 主要用于显示最近的项目。

    <?php include("includes/header.php"); ?>

<?php
include_once 'dbconnect.php';

// fetch files
$sql = "select filename, title, description from tbl_files LIMIT 4";
$result = mysqli_query($con, $sql);
?>


<div id="container">
  <div class="wrapper">
    <div id="content">
      <h2>Recent Projects <button style="float: right;"><a href="new-project.html">New Project</a></button></h2>
      <p><table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>File Name</th>
<th>Description</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><?php echo $row['description']; ?></td>
                    <td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
</div>
</p>

    <div id="column">
      <div class="holder">
        <h2>Welcome!</h2>
        <ul id="latestnews">

          <li class="last"> <p><?php
    session_start();
   include_once "vendor/autoload.php";
    $page = new membership\Page(1);
    if ($page->isValid() == true){
         echo "Hello " . $_SESSION["username"] . "!<br /><br />

<a href='logout.html'>Logout</a>\n";
        } elseif ($page->isValid() == false) { echo "<center>Please <a href='login.php'>Log in</a> to share projects.<br /> <a href='register.php'>Need A Account?</a></center>";}
?><br />
      </p>

    <br /></p>
          </li>
        </ul>
      </div>
    </div>
    <br class="clear" />
  </div>
</div>

<?php
error_reporting(E_ALL & ~E_NOTICE);
include('includes/memberlistconfig.php');

// call this file only after database connection
require_once 'functions.php';
?>


<div id="container">
  <div class="wrapper">
    <div id="content">
      <h2>Categories</h2>
      <p>
        <div class="height20"></div>
        <?php echo $emsg; ?>

  <article>
          Click on one of the categories to see what’s inside.
          <ul>
            <?php
            $res = fetchCategoryTreeList();
            foreach ($res as $r) {
              echo  $r;
            }
            ?>
          </ul>
         </article>
</div></p>
<br class="clear" />
  </div>
</div>

  <?php include("includes/footer.php"); ?>

new-project.html 允许用于上传新项目。

  <?php
include_once('includes/header.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) {
    ?>




<div id="container">
  <div class="wrapper">
    <div id="content">
      <h2>New Project</h2>
      <p><center>

<form action='upload.php' method='post' enctype='multipart/form-data'>
                        <legend>Select File to Upload:</legend>
                        <div class='form-group'>
Title: <br /><input type='text' name='title' maxlength="255"/><br /><br />
Description: <br /><textarea type='text' name='description' maxlength="2000"></textarea><br /><br />

                            <input type='file' name='file1' />
                        </div>
                        <div class='form-group'><br />
                            <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></center>

</p><?php } ?>

<br /></div>
</p>

    <div id="column">
      <div class="holder">
        <h2>Project Upload Rules</h2>
        <ul id="latestnews">
          This is this rules you must follow for uploading a project.<br /><br />
 - You must own the project / script.<br />
 - Must be 100% clean / safe.<br />
 - Code must be easy to read.<br />
 - No outdated code.<br />
<br />
If you don’t follow the rules your account who be banned. 
          <br />
      </p>

    <br /></p>
          </li>
        </ul>
      </div>
    </div>
    <br class="clear" />
  </div>
</div>

<?php include_once('includes/footer.php'); ?>

upload.php: 此文件将信息上传到数据库。

<?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));

$title = '';
if(!empty($_POST['title']))
{
   $title = mysqli_real_escape_string($con, $_POST['title']);
}
$description = '';
if(!empty($_POST['description']))
{
   $description = mysqli_real_escape_string($con, $_POST['description']);
}
            // insert file details into database
            $sql = "INSERT INTO tbl_files(filename, created, title, description) VALUES('$filename', '$created', '$title', '$description')";
            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");
}
?>

0 个答案:

没有答案