A foundation of fundamental problems, finding no problems, tangled

Source: Internet
Author: User
Tags ranges
The following code, download the question why is the size only 0 bytes?

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);
}
}


Reply to discussion (solution)

Verify that the $attachment [' attachment '] has a value and that the file exists.

Verify that the $attachment [' attachment '] has a value and that the file exists.


OK, sure, and sure, I tested it locally. Just know why.

Header (' Content-length: '. FileSize ($file));

Header (' Content-length: '. FileSize ($file));

or 0 bytes ..... Depressed..

The program did not execute to the second if ...

if (File_exists (DirName ($file)))
You determine whether the directory exists, not whether the file exists, so if the file does not exist, the downloaded content may be empty

if (File_exists (DirName ($file)))
You determine whether the directory exists, not whether the file exists, so if the file does not exist, the downloaded content may be empty

If you change to: if (file_exists ($file)), also not ....

Without entering the second if, the description of your uploads/resource/directory does not exist, and of course the file does not exist.

Without entering the second if, the description of your uploads/resource/directory does not exist, and of course the file does not exist.


$file _dir = '/webfolder/hz/uploads/resource/';
$file _name = $attachment [' attachment '];
$file = $file _dir. $file _name;
This hint file is not found, but I directly hit the browser and can, such as $file= ' 123.docx ';
127.0.0.1/webfolder/hz/uploads/resource/123.docx This download is no problem.

My php file and the folder where the files are saved are in the Hz folder.

$file _dir = '/webfolder/hz/uploads/resource/';

This is the root directory of the system, not the root directory of the site, you two concepts confused

$file _dir = '/webfolder/hz/uploads/resource/';

This is the root directory of the system, not the root directory of the site, you two concepts confused


$file _dir = ' d:/cede/webfolder/hz/uploads/resource/'; That's not going to work.

Reference 10 Floor snmr_com reply: $file _dir = '/webfolder/hz/uploads/resource/';

This is the root directory of the system, not the root directory of the site, you two concepts confused


$file _dir = ' d:/cede/webfolder/hz/uploads/resource/'; That's not going to work.

All kinds of paths have been tried, is it not, heroes, help!!!!!!!!!!!!!!!!

$file _dir = '/webfolder/hz/uploads/resource/';
$file _name = $attachment [' attachment '];
$file = $file _dir. $file _name;
This hint file is not found, but I directly hit the browser and can, such as $file= ' 123.docx ';
127.0.0.1/webfolder/hz/uploads/resource/123.docx This download is no problem.

echo $file;//Try It

$file _dir = '/webfolder/hz/uploads/resource/';
$file _name = $attachment [' attachment '];
$file = $file _dir. $file _name;
This hint file is not found, but I directly hit the browser and can, such as $file= ' 123.docx ';
127.0.0.1/webfolder/...


echo $file Get/webfolder/hz/uploads/resource/123.docx

127.0.0.1/webfolder/hz/uploads/resource/123.docx This download is no problem.
So where's your program? (Echo __file__; Check it out)

127.0.0.1/webfolder/hz/uploads/resource/123.docx This download is no problem.
So where's your program? (Echo __file__; Check it out)


echo __file__; get D:\Code\webfolder\hz\public_attach_down.php, correct path

Program
D:\Code\webfolder\hz\public_attach_down.php
This means that the document is
D:/code/webfolder/hz/uploads/resource/123.docx
Where D:/code is the root of the website
You first

$file = ' D:/code/webfolder/hz/uploads/resource/123.docx ', if (file_exists ($file)) {  $fileName = basename ($file); C1/>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);}
Test it.

Program
D:\Code\webfolder\hz\public_attach_down.php
This means that the document is
D:/code/webfolder/hz/uploads/resource/123.docx
Where D:/code is the root of the website
You first PHP code?12345678910$file = ' D:/code/webfolder/hz/uploads/re ...

No, do not execute if inside of the program, is not in the IFRAME not?

Why the frame?
Can you just write a file and test it on your own?

Why the frame?
Can you just write a file and test it on your own?


Not even a single file ...

That is the path information you provide is wrong!
You better cut a picture.

That is the path information you provide is wrong!
You better cut a picture.




This is the site path

D:/code/htdocs_33594_v6/hz/uploads/resource/123.docx
Why do you always say it's webfolder?

D:/code/htdocs_33594_v6/hz/uploads/resource/123.docx
Why do you always say it's webfolder?

The red part is just an example, I wrote htdocs_33594_v6 this in the code.

Reference 23 Floor Xuzuning's reply: D:/code/htdocs_33594_v6/hz/uploads/resource/123.docx
Why do you always say it's webfolder?

The red part is just an example, I wrote htdocs_33594_v6 this in the code.




$file = ' D:/code/htdocs_33594_v6/uploads/public/20130420071624_1761_ upload _ case. 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);
}
This can be downloaded in a separate file and normal, but not in the IFRAME, strange!!!!!

The process of solving the problem is the most interesting.

The process of solving the problem is the most interesting.

This fun is too frustrating for people.

What Satan intends to do.

What Satan intends to do.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.