通过手动设置的文件ID下载文件

时间:2014-04-27 09:52:13

标签: php mysql

我想用PHP创建文件ID的文件下载部分。

我的理念是:

  • 我通过文件管理器/ ftp客户端上传文件,

  • 我决定一个文件ID。然后在“Admin_Index.php”页面中我注册文件 Id使用我上传的文件路径。

  • 用户可以从“PAGE.html”文件下载,下载链接 可用id。并在“download.php”我查询并检查 文件ID是否存在。如果存在,那么查询的路径将 存储在php标题中并重定向..

但它无法正常工作..文件代码如下所示..

Admin_index.php代码

<strong>Welcome to Admin Panel For File Registration Into DB</strong><br>
<form action="dbreg.php" method="post">
File Id : <input type="text" name="fid" value="" /><br />
Path : <input type="url" name="fpath" value="" maxlength="250"/><br />
<input type="submit" value="Add to DB" />
</form>

<?php 
        $iden=$_GET['idn'];
        if ($iden==null and $iden=="")
        {
        echo 'Register DB Here';
        }
        if ($iden==2)
        {
        echo 'File ID With path Successfully Added';
        }
        if ($iden==1)
        {
        echo 'File Id Already Exist , Please try with a new one';
        }

        ?>

连接代码.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "user";
$mysql_password = "******";
$mysql_database = "database";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

“dbreg.php”代码

<?php
session_start();
include('connection.php');
$id=$_POST['fid'];
$path=$_POST['fpath'];

$qry="select id from files where id= $id";
$result=mysql_query($qry);

if($result) {
    if($result->num_rows == 0)
    {  $idn=2; // means the id does not exist 
        mysqli_query("INSERT INTO files(id, path) VALUES ('$id', '$path')");
        header('location: index.php?idn=2');  }
    else if($result->num_rows >0)
    {  $idn=1; // means the id does exist 
    header('location: index.php?idn=1'); } } 

?>

“PAGE.html”代码

<body>
Downlaod Section<br>
<ol>
<li><a href="download.php?id=110">File 1</a></li>
<li><a href="download.php?id=212">File 2</a></li>
<li><a href="download.php?id=314">File 3</a></li>
<li><a href="download.php?id=550">File 4</a></li>
</body>

“download.php”代码

<?php
session_start();
include('connection.php');
$idd=$_GET['id'];
$qry="select id,path from files where id= $idd";
$result=mysql_query($qry);

if($result) {
    if($result->num_rows == 0)
    { $idn=0;
        echo '404 Error! No file exists with that ID'; }
    else if($result->num_rows == 1)
    { $idn=1; // file id exist
        $row = mysql_fetch_assoc($result);
    $filename=$row['path']; 
header("$filename");} }
     else { echo 'Error! File ID is Required.';}

0 个答案:

没有答案