允许登录用户以PHP下载文件,否则没人可以 [英] Allow logged in user to Download File in PHP else nobody can't

查看:80
本文介绍了允许登录用户以PHP下载文件,否则没人可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有.mp3文件,我想设置网站,以便用户登录后可以下载文件.如果用户未登录,则将无法下载文件.我不希望任何人都能找到文件的路径.

I have .mp3 files in my website and I want to set my site so that after my users have logged in they can download files. If users are not logged in they won't be able to download files. I do not want anyone to be able to find the path of the files.

推荐答案

我将使文件无法单独通过HTTP请求访问,而对于PHP,只需将其打印出来即可:

I'd make the file impossible to access via an HTTP request alone, and with PHP, just print it out:

<?php
session_start();
if (isset($_SESSION['logged_in'])) {
  $file = '/this/is/the/path/file.mp3';

  header('Content-type: audio/mpeg');
  header('Content-length: ' . filesize($file));
  readfile($file);
}
?>

这篇关于允许登录用户以PHP下载文件,否则没人可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆