Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 一个基础的基础问题,找不出问题,纠结

一个基础的基础问题,找不出问题,纠结

Jun 23, 2016 pm 02:06 PM

下面代码,下载下来的问题为什么大小只有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 );}
Copy after login
测试一下

程序
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"); 
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

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: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

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.

See all articles