因此,我们的系统通过将用户图像存储在用户文件夹中来工作,用户文件夹存储在主用户文件夹中,该文件夹存储在名为client_folders的一个文件夹中。
每次我尝试构建我们的下载脚本(使用php force download)下载的文件都是整个文件路径的名称
示例:client_folder / user_30 / client_130 / image.jpg
我只想让文件说出image.jpg
我尝试过几个东西,使用数组pop,basename()进行爆炸但每次尝试其中一个选项时,readfile()函数读取的变量都是空的。
现在在mysql中我将整个文件路径(包含文件名)存储在一列中,这是正确的方法吗?以及如何在没有完整路径的情况下下载文件
如果它有助于这是成功下载的代码....只有完整的路径名:(
ob_clean();
if(isset($_POST['file_name'])){
$file = $_POST['file_name'];
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file.'"');
readfile($file);
exit();
}
答案 0 :(得分:5)
在readfile中,您必须传递完整路径,但是在文件名文件名的头文件中为用户:
ob_clean();
if(isset($_POST['file_name'])){
$file_for_user = $_POST['file_name'];
$full_path_file = "STORAGE_PATH".$file_for_user;
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_for_user.'"');
readfile($full_path_file);
exit();
}
答案 1 :(得分:0)
如果您使用mysql中引用的文件路径动态提取文件以供下载,并且您希望下载文件具有名称而不是此处的路径
ob_clean();
if(isset($_POST['file_name'])){
$file = $_POST['file_name'];
$filename = basename($file);
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($file);
exit();
}
以下是评论版
//This cleans the cache (just a precaution)
ob_clean();
//Grab the file path from the POST global
if(isset($_POST['file_name'])){
//Put the global variable into a regular variable
$file = $_POST['file_name'];
//Use basename() to separate the name of the file from the reference path and put it in a variable
$filename = basename($file);
//I have no idea if this is needed but everyone uses it
header("Content-type: application/octet-stream");
//Use this header to name your file that is being downloaded, use the variable that is storing the file name you grabbed using basename()
header('Content-Disposition: attachment; filename="'.$filename.'"');
//useread file function to send info to buffer and start download, use the variable that contains the full path
readfile($file);
exit();
}