dongtuoji5396 2010-06-09 21:50
浏览 84
已采纳

php大多数内存有效的方式返回文件

so i have a bunch of files, some can be up to 30-40mb and i want to use php to handle security of the files, so i can control who has access to them

that means i have a script sort of like this rough example

$has_permission = check_database_for_permission($user, filename);

if ($has_permission) {
   header('Content-Type: image/jpeg'); 
   readfile ($filename);    
   exit; 
} else {
  // return 401 error
}

i would hate for every request to load the full file into memory, as it would soon chew up all the memory on my server with a few simultaneous requests

so a couple of questions

  1. is readfile the most memory efficient way of doing this?
  2. is there some better method of achieving the same outcome, that i am overlooking?

server: apache/php5

thanks

  • 写回答

2条回答 默认 最新

  • duangang3832 2010-06-09 21:58
    关注

    readfile is the correct way to do this. By all means don't try to read the file yourself and print it to output--that will consume excessive memory. With the readfile function the contents of the file are buffered directly to output, taking up a trivial amount of transitory memory.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据