使用php搜索查询来搜索和下载mp3文件

时间:2016-03-24 11:26:26

标签: php mysql

我刚刚在PHP中创建了一个搜索函数,用于搜索我的数据库表并返回结果。现在我如何使搜索结果成为一个按钮来下载我的数据库中的mp3文件?当文件上传到文件夹时,文件路径存储在表中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM tbl_uploads
            WHERE (`file` LIKE '%".$query."%') OR (`file` LIKE '%".$query."%')") or die(mysql_error());


        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<p><h3>".$results['file']."</h3>".$results['file']."</p>";

            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

如果您尝试让PHP检索该文件并将其作为下载文件提供,请参阅here

答案 1 :(得分:0)

替换它:

logout()

有了这个

echo "<p><h3>".$results['file']."</h3>".$results['file']."</p>";

这可以让你更进一步

但你应该考虑看看这个答案: Create download link for music or video