A basic problem, no problem found, tangled-php Tutorial

Source: Internet
Author: User
A basic problem cannot be found. The following code is entangled. why is the download 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)

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

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


OK, yes, and yes. I tested it locally. Let's see why...

Header ('content-length: '. filesize ($ file ));

Header ('content-length: '. filesize ($ file ));

Or 0 Bytes ..... Depressed ..

The program is not executed to the second if...

If (file_exists (dirname ($ file )))
You determine whether the directory exists, not whether the file exists. Therefore, 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. Therefore, if the file does not exist, the downloaded content may be empty.

If it is changed to: if (file_exists ($ file), no way ....

The second if is not entered, indicating that your uploads/resource/directory does not exist. of course, the file does not exist.

The second if is not entered, indicating that your uploads/resource/directory does not exist. of course, the file does not exist.


$ File_dir = '/webfolder/hz/uploads/resource /';
$ File_name = $ attachment ['attachment'];
$ File = $ file_dir. $ file_name;
This prompt file is not found, but I can directly access the browser, for example, '123.docx ';
127.0.0.1/webfolder/hz/uploads/resource/123.docx.

My php files and folders for saving files are all in the hz folder ..

$ File_dir = '/webfolder/hz/uploads/resource /';

This is the root directory of the system, not the root directory of the website. you have mixed up the two concepts.

$ File_dir = '/webfolder/hz/uploads/resource /';

This is the root directory of the system, not the root directory of the website. you have mixed up the two concepts.


$ File_dir = 'd:/cede/webfolder/hz/uploads/resource /';

Reference the reply from snmr_com on the 10th floor: $ file_dir = '/webfolder/hz/uploads/resource /';

This is the root directory of the system, not the root directory of the website. you have mixed up the two concepts.


$ File_dir = 'd:/cede/webfolder/hz/uploads/resource /';

You have tried all the paths, but you can't do it. you can ask for help !!!!!!!!!!!!!!!!

$ File_dir = '/webfolder/hz/uploads/resource /';
$ File_name = $ attachment ['attachment'];
$ File = $ file_dir. $ file_name;
This prompt file is not found, but I can directly access the browser, for example, '123.docx ';
127.0.0.1/webfolder/hz/uploads/resource/123.docx.

Echo $ file; // try again

$ File_dir = '/webfolder/hz/uploads/resource /';
$ File_name = $ attachment ['attachment'];
$ File = $ file_dir. $ file_name;
This prompt file is not found, but I can directly access the browser, for example, '123.docx ';
127.0.0.1/webfolder /......


Echo $ file to get/webfolder/hz/uploads/resource/123.docx

127.0.0.1/webfolder/hz/uploads/resource/123.docx.
So where is your program? (Echo _ FILE __; take a look)

127.0.0.1/webfolder/hz/uploads/resource/123.docx.
So where is your program? (Echo _ FILE __; take a look)


Echo _ FILE __; get D: \ Code \ webfolder \ hz \ public_attach_down.php. The path is correct.

Program
D: \ Code \ webfolder \ hz \ public_attach_down.php
That is to say, 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 );   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

Program
D: \ Code \ webfolder \ hz \ public_attach_down.php
That is to say, the document is
D:/Code/webfolder/hz/uploads/resource/123.docx
Where D:/Code is the root of the website
PHP code first? 12345678910 $ file = 'd:/Code/webfolder/hz/uploads/re ......

No. I didn't execute the program in if. isn't it in iframe?

Why is the framework?
Can you just write a file and test it?

Why is the framework?
Can you just write a file and test it?


Not a single file...

That is, the path information you provided is incorrect!
You 'd better cut a picture.

That is, the path information you provided is incorrect!
You 'd better cut a picture.




This is the site path

D:/Code/htdocs_33594_v6/hz/uploads/resource/123.docx
Why do you always say webfolder?

D:/Code/htdocs_33594_v6/hz/uploads/resource/123.docx
Why do you always say webfolder?

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

Reply to reference xuzuning on the 23rd floor: D:/Code/htdocs_33594_v6/hz/uploads/resource/123.docx
Why do you always say webfolder?

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




$ File = 'd:/Code/htdocs_33594_v6/uploads/public/20130420071624_1761_upload _ .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 );
}
In this way, the file can be downloaded separately and the file can be downloaded normally, but the file cannot be downloaded in iframe. no wonder !!!!!

The process of solving the problem is the most interesting.

The process of solving the problem is the most interesting.

This fun is too fun ..

Intended

Intended

 header("Content-Transfer-Encoding: binary"); 

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.