一个基础的基础问题,找不出问题,纠结
下面代码,下载下来的问题为什么大小只有0字节?
if( $attachment['attachment'] )
{
$file = 'uploads/resource/'.$attachment['attachment'];
if( file_exists( dirname($file) ) )
{
$fileName = basename( $file );
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize( $file ));
header("Content-Disposition: attachment; filename=" . $fileName);
readfile( $file );
}
}
回复讨论(解决方案)
确认 $attachment['attachment'] 有值,且文件存在。
确认 $attachment['attachment'] 有值,且文件存在。
确定、肯定、以及一定,我在本地测试的。就吧知道为什么。。。
header('Content-length: '.filesize($file));
header('Content-length: '.filesize($file));
还是0字节。。。。。郁闷。。
程序没执行到第二个 if 。。。
if( file_exists( dirname($file) ) )
你判断的是目录是否存在,而不是文件是否存在,因此如果文件确实不存在,下载的内容就可能是空
if( file_exists( dirname($file) ) )
你判断的是目录是否存在,而不是文件是否存在,因此如果文件确实不存在,下载的内容就可能是空
如果改为:if( file_exists( $file) ),也不行。。。。
没有进入第二个if, 说明的你 uploads/resource/ 目录不存在,当然文件也不存在。
没有进入第二个if, 说明的你 uploads/resource/ 目录不存在,当然文件也不存在。
$file_dir = '/webfolder/hz/uploads/resource/';
$file_name = $attachment['attachment'];
$file = $file_dir . $file_name;
这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。
我的PHP文件和保存文件的文件夹都在hz文件夹里。。
$file_dir = '/webfolder/hz/uploads/resource/';
这个是系统的根目录,不是网站的根目录,你两个概念搞混了
$file_dir = '/webfolder/hz/uploads/resource/';
这个是系统的根目录,不是网站的根目录,你两个概念搞混了
$file_dir = 'd:/cede/webfolder/hz/uploads/resource/'; 这样也不行呢
引用 10 楼 snmr_com 的回复:$file_dir = '/webfolder/hz/uploads/resource/';
这个是系统的根目录,不是网站的根目录,你两个概念搞混了
$file_dir = 'd:/cede/webfolder/hz/uploads/resource/'; 这样也不行呢
各种路径都试过了,就是吧不行,大侠们,求救!!!!!!!!!!!!!!!!
$file_dir = '/webfolder/hz/uploads/resource/';
$file_name = $attachment['attachment'];
$file = $file_dir . $file_name;
这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。
echo $file;//试试看
$file_dir = '/webfolder/hz/uploads/resource/';
$file_name = $attachment['attachment'];
$file = $file_dir . $file_name;
这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
127.0.0.1/webfolder/……
echo $file得到/webfolder/hz/uploads/resource/123.docx
127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。
那么你的程序在哪里?(echo __FILE__; 看一下)
127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。
那么你的程序在哪里?(echo __FILE__; 看一下)
echo __FILE__;得到D:\Code\webfolder\hz\public_attach_down.php,路径正确
程序
D:\Code\webfolder\hz\public_attach_down.php
也就是说文档为
D:/Code/webfolder/hz/uploads/resource/123.docx
其中 D:/Code 为网站的根
你先
$file = 'D:/Code/webfolder/hz/uploads/resource/123.docx';if( file_exists( $file ) ) { $fileName = basename( $file ); header("Content-type: application/force-download"); header("Content-type: application/octet-stream"); header("Accept-Ranges: bytes"); header("Accept-Length: ".filesize( $file )); header("Content-Disposition: attachment; filename=" . $fileName); readfile( $file );}
程序
D:\Code\webfolder\hz\public_attach_down.php
也就是说文档为
D:/Code/webfolder/hz/uploads/resource/123.docx
其中 D:/Code 为网站的根
你先PHP code?12345678910$file = 'D:/Code/webfolder/hz/uploads/re……
不行,没执行if里面的程序,难道是在iframe里不行?
干嘛是框架?
你单独写个文件测试一下不就可以了吗
干嘛是框架?
你单独写个文件测试一下不就可以了吗
单独文件也不行。。。
那就是你提供的路径信息不对!
你最好截个图
那就是你提供的路径信息不对!
你最好截个图
这是站点路径
D:/Code/ htdocs_33594_v6/hz/uploads/resource/123.docx
你怎么总是说是 webfolder 呢?
D:/Code/htdocs_33594_v6/hz/uploads/resource/123.docx
你怎么总是说是 webfolder 呢?
红色部分只是个例子,代码中我是写htdocs_33594_v6这个的
引用 23 楼 xuzuning 的回复:D:/Code/htdocs_33594_v6/hz/uploads/resource/123.docx
你怎么总是说是 webfolder 呢?
红色部分只是个例子,代码中我是写htdocs_33594_v6这个的
$file = 'D:/Code/htdocs_33594_v6/uploads/public/20130420071624_1761_上传_案.docx';
if( file_exists( $file ) ) {
$fileName = basename( $file );
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize( $file ));
header("Content-Disposition: attachment; filename=" . $fileName);
readfile( $file );
}
这样在单独文件可以下载并正常,但是在iframe里就不行了,怪哉!!!!!
解决问题的过程是最有趣的。
解决问题的过程是最有趣的。
这个趣味也太折腾人了。。
撒打算打算的
撒打算打算的
header("Content-Transfer-Encoding: binary");

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 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
