我正在php中下载文件。。我已经在一个页面中显示了所有文件,并在用户单击下载按钮时下载。。文件名已存储在循环中的数组会话中。。单击下载按钮时,会打开新页面downloadfiles.php并下载该文件。。问题是我只能手动传递会话数组值。。如何根据用户点击按钮进行传递?
当前我使用$file=$_SESSION['imagefilename][0];手动下载数据库中的第一个文件。。如何根据用户点击自动分配[0]值?
显示.php
<?php
//pass session email here
$email = $_SESSION['email'];
$image = mysql_query("SELECT * FROM image WHERE email='$email' ");
$numrows = mysql_num_rows($image);
if($numrows!=0)
{
$_SESSION['imagefilename'] = array();
while ($row = mysql_fetch_assoc($image))
{
//get the image path
$path = $row['path'];
//get the image name
$name = $row['name'];
$_SESSION['imagefilename'][] = $name;
$_SESSION['imgpath'] = $path;
echo "<br>Your image: '$name' <br><img src='$path' width='300' height='200'><br><a href='/images/downloadfiles.php' data-role='button' data-theme='e' target='_self' name='download'>Download</a><br><a href='deleteimg.php' data-role='button' data-theme='e' target='_self'>Delete</a></br>";
}
}else
echo "No image found!";
?>
图像/下载文件.php
<?php
session_start();
// Define the path to file
$file = $_SESSION['imagefilename'][0];
if(!$file)
{
// File doesn't exist, output error
die('File not found');
}
else
{
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
?>
我正在php中下载文件。。我已经在一个页面中显示了所有文件,并在用户单击下载按钮时下载。。文件名已存储在循环中的数组会话中。。单击下载按钮时,会打开新页面downloadfiles.php并下载该文件。。问题是我只能手动传递会话数组值。。如何根据用户点击按钮进行传递?
当前我使用$file=$_SESSION['imagefilename][0];手动下载数据库中的第一个文件。。如何根据用户点击自动分配[0]值?
您需要将文件的ID嵌入下载链接url:
echo "<br>Your[..snip..]<a href='/images/downloadfiles.php?id=$idx' etc...
^^^^^^^^
然后在下载脚本中检索:
session_start();
$id = $_GET['id'];
if (!isset($_SESSION['imagefilename'][$id])) {
die("Invalid file id");
}
为什么要使用会话?只需将图像id作为get参数发送,然后查看电子邮件即可。
display.php(在while fetch_assoc中)
<a href='/images/downloadfiles.php?id=<?php echo $row['id'];?>' data-role='button' data-theme='e' target='_self' name='download'>Download</a>
下载文件.php
$id = isset($_GET['id']) ? $_GET['id'] : null;
$image = mysql_query('SELECT * FROM image WHERE id="' . $id . '"');
$numrows = mysql_num_rows($image);
if($numrows!=0)
{
$row = mysql_fetch_assoc($image);
if ($row['email'] == $_SESSION['email'])
{
// Header and readfile
}
}