有两个文件,a.php 和 b.php
点击a.php中的链接,开始对文件下载:代码如下
这是a.php
<?php $file_dir = "D:/files";$file_name = $row[1]; //这是从数据库中查找出来的echo "<a href='./b.php?file_dir=" . $file_dir . "&file_name=" . $file_name . "'>" . $row[2] . "</a>"; //点击这个链接,下载的文件名是对的,就是文件里没有内容,如果是图片,图片没有预览?>
这是b.php的全部代码
<?php $file_name = $_GET['file_name']; //文件名 $file_dir = $_GET['file_dir']; //文件路径 if(!file_exists($file_dir.$file_name)){ echo "找不到 [" . $file_dir.$file_name . "] 文件"; exit; }else{ $file=fopen($file_dir.$file_name,"r"); Header("Content-type: application/octet-stream"); Header("Accept-Ranges:bytes"); Header("Accept-Length:".filesize($file_dir.$file_name)); Header("Content-Disposition: attachment; filename=".$file_name); }?>
回复讨论(解决方案)
a.php 中是 $file_dir = "D:/files";
那么在 b.php 中的 $file_dir.$file_name 就缺少了路径符
你在 b.php 中只有 $file=fopen($file_dir.$file_name,"r"); 打开了文件,但并没有输出文件的内容
如果用 $file=fopen($file_dir.$file_name,"r"); 打开的文件是图片的话,将不能正确读出图片数据
应使用二进制方式打开:$file=fopen($file_dir.$file_name,"r b");
a.php 中是 $file_dir = "D:/files";
那么在 b.php 中的 $file_dir.$file_name 就缺少了路径符
你在 b.php 中只有 $file=fopen($file_dir.$file_name,"r"); 打开了文件,但并没有输出文件的内容
如果用 $file=fopen($file_dir.$file_name,"r"); 打开的文件是图片的话,将不能正确读出图片数据
应使用二进制方式打开:$file=fopen($file_dir.$file_name,"r b");
听懂了 但是我改成了这样 还是一样的结果 代码如下
<?php $file_name = $_GET['file_name']; //文件名 $file_dir = $_GET['file_dir']; //文件路径为当前目录 if(!file_exists($file_dir.$file_name)){ echo "找不到 [" . $file_dir.$file_name . "] 文件"; exit; }else{ $file=fopen($file_dir.$file_name,"rb"); readfile($file); Header("Content-type: application/octet-stream"); Header("Accept-Ranges:bytes"); Header("Accept-Length:".filesize($file_dir.$file_name)); Header("Content-Disposition: attachment; filename=".$file_name); ob_clean(); flush(); exit; ?>
哎哟 晕 代码里不能改字体颜色哦 重新
<?php $file_name = $_GET['file_name']; //文件名 $file_dir = $_GET['file_dir']; //文件路径为当前目录 if(!file_exists($file_dir.$file_name)){ echo "找不到 [" . $file_dir.$file_name . "] 文件"; exit; }else{ $file=fopen($file_dir.$file_name,"rb"); readfile($file); Header("Content-type: application/octet-stream"); Header("Accept-Ranges:bytes"); Header("Accept-Length:".filesize($file_dir.$file_name)); Header("Content-Disposition: attachment; filename=".$file_name); ob_clean(); flush(); exit;?>
}else{ $file = $file_dir.$file_name; Header("Content-type: application/octet-stream"); Header("Accept-Ranges:bytes"); Header("Accept-Length:".filesize($file)); Header("Content-Disposition: attachment; filename=".$file_name); readfile($file); }
成功了,首先要谢谢版主,版主说的非常好,然后必须分享一下:
是下载压缩文件的,包括压缩文件其中的文件,代码也不难
//打开文件 $file = fopen($file_dir.$file_name,"r"); //返回的文件类型 Header("Content-type: application/octet-stream"); //按照字节大小返回 Header("Accept-Ranges: bytes"); //返回文件的大小 Header("Accept-Length: ".filesize($file_dir.$file_name)); //这里对客户端的弹出对话框,对应的文件名 Header("Content-Disposition: attachment; filename=".$file_dir.$file_name); //修改之前,一次性将数据传输给客户端 echo fread($file, filesize($file_dir.$file_name)); //修改之后,一次只传输1024个字节的数据给客户端 //向客户端回送数据 $buffer=1024;// //判断文件是否读完 while (!feof($file)) { //将文件读入内存 $file_data=fread($file,$buffer); //每次向客户端回送1024个字节的数据 echo $file_data; } fclose($file);

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
