Php 从数据库中选择文件时,不会生成下载链接

Php 从数据库中选择文件时,不会生成下载链接,php,hyperlink,download,Php,Hyperlink,Download,我试图生成一个链接,以便从我的数据库下载一个文件。我首先从下拉列表中选择一个文件,然后它会将您引导到另一个页面,并在该页面中创建您的链接,但当我想这样做时,如果单击“下载文件”,它会返回上一个页面,而不会创建链接。 这是我的下载.php: <html> <body> <title>Download your friend's uploads</title> <form action="download2.php" method="post"&

我试图生成一个链接,以便从我的数据库下载一个文件。我首先从下拉列表中选择一个文件,然后它会将您引导到另一个页面,并在该页面中创建您的链接,但当我想这样做时,如果单击“下载文件”,它会返回上一个页面,而不会创建链接。 这是我的下载.php:

<html>
<body>
<title>Download your friend's uploads</title>
<form action="download2.php" method="post">
 <?php
session_start();
$username =$_SESSION["uname"];
?>

<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT imagesnotes FROM userdata where sharedpeople='$username'");
echo "File or Image";

echo'<select name="imagesnotes">';
echo'<option value="" selected="selected">Select a File</option>';

while($row = mysqli_fetch_array($sql))

{
    echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>';
}
echo'</select></p><p>';

mysqli_close($con);
 ?>
 <td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td>
</form>
</body>
</html>
用户从中选择图像并单击提交按钮

以下是我的下载2.php:

<?php
session_start();
$username =$_SESSION["uname"];
?>

<?php

 $con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $imagesnotes = $_POST['imagesnotes'];

 $query = "SELECT imagesnotes From userdata where imagesnotes='$imagesnotes' and sharedpeople='$username'";
 $res = mysqli_query($con,$query);
 $res or die('Error, query failed');
 if(mysqli_num_rows($res) == 0){
 echo "<br>Database is empty </br>";
 } 
 else{
 while(list($id) = mysqli_fetch_array($res)){
?>

 <br><a href="download.php?id=<?php=$id;?>">download your file</a></br>
 <?php
 }
 }

 mysqli_close($con);

?>
<?php
if ($_GET['id'] != "") {
  $id = $_GET['id'];
  // Query your database to get the filename corresponding to $id and save it in a $filename variable
  header('Location: uploads/'.$filename);
  // Dont forget to change "uploads" to your real uploads folder
}
?>
在这里,它必须创建一个链接来下载文件。我做错了什么?你能帮我吗?谢谢

更新:

因此,我的uploaded2.php的最终版本如下:

<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php 
if(isset($_GET['imagesnotes'])) 
{
// if id is set then get the file with the id from database
$con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$id    = $_GET['imagesnotes'];
$query = "SELECT imagesnotes, type, size, content FROM datas WHERE imagesnotes = '$id'";

$result = mysqli_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

mysqli_close($con);
}
?>

<?php

 $con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $imagesnotes = $_POST['imagesnotes'];

 $query = "SELECT imagesnotes From userdata where imagesnotes='$imagesnotes' and sharedpeople='$username'";
 $res = mysqli_query($con,$query);
 $res or die('Error, query failed');
 if(mysqli_num_rows($res) == 0){
 echo "<br>Database is empty </br>";
 } 
 else{
 while(list($id) = mysqli_fetch_array($res)){
?>

 <br><a href="download.php?id=<?php echo $id;?>">download your file</a></br>
 <?php
 }
 }


 mysqli_close($con);

?>
id | filename
-----------------
1  | myfile.zip
2  | myfile2.rar
但它仍然把我带到上一页。为什么会这样? 更新2:所以当我下载文件时,我会下载损坏的文件。这是我的uploadfile.php:

<html>
<body>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<title>Uploading Page</title>
<b>Hello again,<?php echo $username ?>. From here, you can select your image or file and upload our database.</b>
<form method="post" action ="uploaded.php" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
下面是my Upload.php以确认更新操作:

<html>
<body>
<title>Uploading Page</title>

<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con=mysqli_connect("localhost","root","","webpage");
if (mysqli_connect_errno()) 
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

$query = "INSERT INTO datas (username,imagesnotes,size,type,content) ".
"VALUES ('$username','$fileName', '$fileSize', '$fileType', '$content')";

$res=mysqli_query($con,$query);
$res or die('Error, query failed'); 

echo "<br>File $fileName uploaded<br> <a href = 'default.php'>Return</a> ";
} 
mysqli_close($con);
?>
</body>
</html>
我做错了什么?谢谢 更新3:现在我正在将文件上载到服务器,而不是数据库。但是现在我有一个问题,我不能把那个文件作为下载链接。我该怎么做? 这是我下载的yours.php:

<html>
<body>
<title>Download your uploads</title>

<?php
session_start();
$username =$_SESSION["uname"];
?>
<form action="downloadyours2.php" method="post">

<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT imagesnotes FROM datas where username='$username'");
echo "File or Image";

echo'<select name="imagesnotes">';
echo'<option value="" selected="selected">Select a File</option>';

while($row = mysqli_fetch_array($sql))

{
    echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>';
}
echo'</select></p><p>';

mysqli_close($con);
 ?>
<td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td>
</form>
</body>
</html>
 <?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
 if(isset($_GET['imagesnotes'])) 
    {
    $con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $imagesnotes = $_GET['imagesnotes'];
        // Set database name
        $db = "webpage";
        // If $_GET['imagesnotes'] is set then get the file with that filename from database
        // Expecting a single result row: let's use mysqli_fetch_array(query(...), MYSQLI_ASSOC) function
        $sql = mysqli_fetch_array(mysqli_query($con, "SELECT type, size FROM datas WHERE imagesnotes='$imagesnotes'"), MYSQLI_ASSOC);
    header("Content-length:" . $sql['size']);
    header("Content-Disposition: attachment; filename=" . $imagesnotes);
    header("Content-Transfer-Encoding: Binary");
    header("$target_file");
    echo $sql['content'];
    }
    mysqli_close($con);
?>
</body>
</html>
这是我下载的yours2.php:

<!DOCTYPE html>
<html>
<head>
<title>Your download link is ready</title>
</head>
<body>
<?php
    // No need to query the database again, you get the filename from POST data
 echo '<br /><a href="downloadedyours.php?imagesnotes=' . $_POST['imagesnotes'] . '">Download your file</a><br />';
 ?>
</body>
</html>
这是我下载的yours.php:

<html>
<body>
<title>Download your uploads</title>

<?php
session_start();
$username =$_SESSION["uname"];
?>
<form action="downloadyours2.php" method="post">

<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT imagesnotes FROM datas where username='$username'");
echo "File or Image";

echo'<select name="imagesnotes">';
echo'<option value="" selected="selected">Select a File</option>';

while($row = mysqli_fetch_array($sql))

{
    echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>';
}
echo'</select></p><p>';

mysqli_close($con);
 ?>
<td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td>
</form>
</body>
</html>
 <?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
 if(isset($_GET['imagesnotes'])) 
    {
    $con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $imagesnotes = $_GET['imagesnotes'];
        // Set database name
        $db = "webpage";
        // If $_GET['imagesnotes'] is set then get the file with that filename from database
        // Expecting a single result row: let's use mysqli_fetch_array(query(...), MYSQLI_ASSOC) function
        $sql = mysqli_fetch_array(mysqli_query($con, "SELECT type, size FROM datas WHERE imagesnotes='$imagesnotes'"), MYSQLI_ASSOC);
    header("Content-length:" . $sql['size']);
    header("Content-Disposition: attachment; filename=" . $imagesnotes);
    header("Content-Transfer-Encoding: Binary");
    header("$target_file");
    echo $sql['content'];
    }
    mysqli_close($con);
?>
</body>
</html>
例如,如果文件为:document.docx

header('Location: uploaddir/document.docx');
行得通 如果.htcaccess配置正确,文件将自动下载。 因此,如果这还不适用于您:

我在download2.php的第一个文件中没有看到对$_GET['id']的管理,在download2.php的第二个文件中没有看到创建download.php第一个文件的链接。所以它只打印download.php页面内容。 您应该重定向到另一个页面download3.php?它管理$_GET['id']重定向到真实的文件链接,或者首先在download.php中执行,如果没有提供$_GET['id'],则打印download.php

如果尚未完成,请在MySQL表中按如下方式分配和标识:

<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php 
if(isset($_GET['imagesnotes'])) 
{
// if id is set then get the file with the id from database
$con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$id    = $_GET['imagesnotes'];
$query = "SELECT imagesnotes, type, size, content FROM datas WHERE imagesnotes = '$id'";

$result = mysqli_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

mysqli_close($con);
}
?>

<?php

 $con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $imagesnotes = $_POST['imagesnotes'];

 $query = "SELECT imagesnotes From userdata where imagesnotes='$imagesnotes' and sharedpeople='$username'";
 $res = mysqli_query($con,$query);
 $res or die('Error, query failed');
 if(mysqli_num_rows($res) == 0){
 echo "<br>Database is empty </br>";
 } 
 else{
 while(list($id) = mysqli_fetch_array($res)){
?>

 <br><a href="download.php?id=<?php echo $id;?>">download your file</a></br>
 <?php
 }
 }


 mysqli_close($con);

?>
id | filename
-----------------
1  | myfile.zip
2  | myfile2.rar
然后将其放在download.php顶部或新的download3.php中不要忘记在download2.php中上载链接:

<?php
session_start();
$username =$_SESSION["uname"];
?>

<?php

 $con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $imagesnotes = $_POST['imagesnotes'];

 $query = "SELECT imagesnotes From userdata where imagesnotes='$imagesnotes' and sharedpeople='$username'";
 $res = mysqli_query($con,$query);
 $res or die('Error, query failed');
 if(mysqli_num_rows($res) == 0){
 echo "<br>Database is empty </br>";
 } 
 else{
 while(list($id) = mysqli_fetch_array($res)){
?>

 <br><a href="download.php?id=<?php=$id;?>">download your file</a></br>
 <?php
 }
 }

 mysqli_close($con);

?>
<?php
if ($_GET['id'] != "") {
  $id = $_GET['id'];
  // Query your database to get the filename corresponding to $id and save it in a $filename variable
  header('Location: uploads/'.$filename);
  // Dont forget to change "uploads" to your real uploads folder
}
?>
文件名:download2.php

用途:打印下载文件的链接

注:没什么可说的

<!DOCTYPE html>
<html>
<head>
<title>Your download link is ready</title>
</head>
<body>
<?php
    // No need to query the database again, you get the filename from POST data
    echo '<br /><a href="download3.php?imagesnotes=' . $_POST['imagesnotes'] . '">Download your file</a><br />';
?>
</body>
</html>
回复你的更新3

首先,确保上传功能正常工作。这意味着您应该通过上载php脚本上载文件,然后通过FTP客户端下载,以查看文件是否正确上载。如果是这样的话,请着手修复您的下载功能

$_FILES["fileToUpload"]["name"]
您的下载功能中有几个错误

$_FILES["fileToUpload"]["name"]
你为什么期望它能重新运行看

这并不意味着什么——瞧

没有打印任何内容,因为其中没有存储任何内容

以下是您的下载功能的外观。无需调用数据库,您将文件名作为$\u GET参数传递

<?php
    session_start();
    $username = $_SESSION["uname"];
    if(isset($_GET['imagesnotes'])) {
        $filename = $_GET['imagesnotes'];
        $dir = "uploads/";
        $file = $dir . $filename;
        if (file_exists($filename)) {
        header('Content-Description: File Transfer');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: Binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
    }
    else
        echo "File not found!";
    exit;
    }
?>

也许这只是一个错误:download.php?id=应该是download.php?id=@m_pro_\m感谢您的帮助它显示了文件名,但它仍然将我带到上一页,我如何才能下载所选的文件?应该感谢你的帮助。但指向上一页的方向错误仍然存在。如何生成链接并下载文件?尝试更改$fp=fopen$tmpName,'r';使用$fp=fopen$tmpName,'rb';不,我也一样。可能是因为像utf8\u general\u ci这样的blob字符发生了吗?可能。为什么不考虑将文件存储在文件夹中而不是数据库表中的BLB呢?这会让你的生活更轻松。我是如何在不使用数据库的情况下提供这些信息的,或者我是如何将文件上传到一个文件夹的?阅读或只是谷歌的php文件上传,你会发现大量的例子
<?php
    session_start();
    $username = $_SESSION["uname"];
    if(isset($_GET['imagesnotes'])) {
        $filename = $_GET['imagesnotes'];
        $dir = "uploads/";
        $file = $dir . $filename;
        if (file_exists($filename)) {
        header('Content-Description: File Transfer');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: Binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
    }
    else
        echo "File not found!";
    exit;
    }
?>