Hướng dẫn php header save file on server - tiêu đề php lưu tệp trên máy chủ

Đây là một số mã thông thường:

<?php
echo "hey F4LLCON!";
?>

Được thực hiện, nó hoạt động như chúng tôi mong đợi:

% php output.php 
hey F4LLCON!

Bây giờ tôi sẽ sửa đổi nó để thêm bộ đệm đầu ra và lưu vào tệp và ghi vào stdout (sử dụng các cuộc gọi echo thông thường!):

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>

Sau khi thực hiện, đầu ra trong tệp catched.txt bằng với những gì chúng tôi nhận được trước đó (và vẫn nhận được) trên stdout:

hey F4LLCON!

Bây giờ tôi sẽ sửa đổi nó một lần nữa để hiển thị cách các trình tạo từ PHP 5.5 sẽ cung cấp cho bạn một giải pháp thanh lịch không cần phải hy sinh hiệu suất (giải pháp trước đó yêu cầu bạn lưu tất cả nội dung trung gian trong một bộ đệm đầu ra khổng lồ):

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>

Chúng tôi không lưu trữ mọi thứ trong một bộ đệm khổng lồ và chúng tôi vẫn có thể xuất hiện vào tệp và stdout đồng thời.

Nếu bạn không hiểu các trình tạo, đây là giải pháp trong đó chúng tôi chuyển chức năng "in" gọi lại cho main () và chức năng đó được sử dụng mỗi khi chúng tôi muốn xuất (chỉ một lần ở đây).

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>

MJT tại Jpeto Dot Net

13 năm trước

I strongly recommend, that you use

% php output.php 
hey F4LLCON!
0

% php output.php 
hey F4LLCON!
1

% php output.php 
hey F4LLCON!
2

% php output.php 
hey F4LLCON!
3

% php output.php 
hey F4LLCON!
4

% php output.php 
hey F4LLCON!
5

% php output.php 
hey F4LLCON!
6

% php output.php 
hey F4LLCON!
7

% php output.php 
hey F4LLCON!
8

Marcel G ¶

12 năm trước

% php output.php 
hey F4LLCON!
9

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
0

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
1

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
2

% php output.php 
hey F4LLCON!
8

Dylan tại Wedefy Dot Com ¶

15 năm trước

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
4

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
5

% php output.php 
hey F4LLCON!
8

Mandor tại Mandor Dot Net

16 năm trước

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
7

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
8

<?php
ob_start();
echo "hey F4LLCON!";
$output_so_far = ob_get_contents();
ob_clean();
file_put_contents("/tmp/catched.txt", $output_so_far);
echo $output_so_far;
?>
9

% php output.php 
hey F4LLCON!
8

bobertjean tại yahoo dot fr ¶

13 năm trước

hey F4LLCON!
1

hey F4LLCON!
2

hey F4LLCON!
3

hey F4LLCON!
4

hey F4LLCON!
5

% php output.php 
hey F4LLCON!
8

Marcel G ¶

12 năm trước

hey F4LLCON!
7

Dylan tại Wedefy Dot Com ¶

15 năm trước

hey F4LLCON!
8

% php output.php 
hey F4LLCON!
8

Mandor tại Mandor Dot Net

hey F4LLCON!
9

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
0

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
1

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
2

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
3

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
4

% php output.php 
hey F4LLCON!
8

16 năm trước

Mandor tại Mandor Dot Net

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
6

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
7

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
8

16 năm trước

bobertjean tại yahoo dot fr ¶

<?php
$main = function() {
    yield "hey F4LLCON!";
};
$f = fopen("/tmp/catched2.txt", "wb");
foreach ($main() as $chunk) { fwrite($f, $chunk); echo $chunk; }
fclose($f);
?>
9

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
0

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
1

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
2

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
3

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
4

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
5

% php output.php 
hey F4LLCON!
8

PHP tại Ober-Mail Dot de ¶

2 năm trước

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
7

<?php
$main = function($print_func) {
    $print_func("hey F4LLCON!");
};
$f = fopen("/tmp/catched3.txt", "wb");
$main(function($output) use ($f) {
    fwrite($f, $output);
    echo $output;
});
fclose($f);
?>
8

% php output.php 
hey F4LLCON!
8

Tim tại SharpWebDevelopment Dot Com ¶

15 năm trước

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
0

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
1

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
2

% php output.php 
hey F4LLCON!
8

Mandor tại Mandor Dot Net

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
3

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
4

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
5

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
6

16 năm trước

Mandor tại Mandor Dot Net

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
7

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
8

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>
9

echo0

echo1

% php output.php 
hey F4LLCON!
8

16 năm trước

13 năm trước

echo3

echo4

echo5

% php output.php 
hey F4LLCON!
8

Marcel G ¶

12 năm trước

echo7

echo8

echo9

catched.txt0

% php output.php 
hey F4LLCON!
8

Dylan tại Wedefy Dot Com ¶

2 năm trước

catched.txt2

catched.txt3

catched.txt4

% php output.php 
hey F4LLCON!
8

Tim tại SharpWebDevelopment Dot Com ¶

Mandor tại Mandor Dot Net

catched.txt6

catched.txt7

% php output.php 
hey F4LLCON!
8

16 năm trước

bobertjean tại yahoo dot fr ¶

catched.txt9

I strongly recommend, that you use 0

% php output.php 
hey F4LLCON!
8

PHP tại Ober-Mail Dot de ¶

12 năm trước

I strongly recommend, that you use 2

I strongly recommend, that you use 3

% php output.php 
hey F4LLCON!
8

Dylan tại Wedefy Dot Com ¶

12 năm trước

I strongly recommend, that you use 5

I strongly recommend, that you use 6

I strongly recommend, that you use 7

I strongly recommend, that you use 8

I strongly recommend, that you use 9

% php output.php 
hey F4LLCON!
00

% php output.php 
hey F4LLCON!
8

Mandor tại Mandor Dot Net

13 năm trước

% php output.php 
hey F4LLCON!
02

% php output.php 
hey F4LLCON!
03

% php output.php 
hey F4LLCON!
04

% php output.php 
hey F4LLCON!
05

% php output.php 
hey F4LLCON!
06

% php output.php 
hey F4LLCON!
07

% php output.php 
hey F4LLCON!
08

% php output.php 
hey F4LLCON!
09

% php output.php 
hey F4LLCON!
10

Marcel G ¶

13 năm trước

% php output.php 
hey F4LLCON!
11

% php output.php 
hey F4LLCON!
12

% php output.php 
hey F4LLCON!
13

% php output.php 
hey F4LLCON!
14

% php output.php 
hey F4LLCON!
15

% php output.php 
hey F4LLCON!
16

% php output.php 
hey F4LLCON!
17

% php output.php 
hey F4LLCON!
18

% php output.php 
hey F4LLCON!
19

% php output.php 
hey F4LLCON!
20

% php output.php 
hey F4LLCON!
8

Marcel G ¶

12 năm trước

% php output.php 
hey F4LLCON!
22

% php output.php 
hey F4LLCON!
23

% php output.php 
hey F4LLCON!
24

% php output.php 
hey F4LLCON!
25

% php output.php 
hey F4LLCON!
26

% php output.php 
hey F4LLCON!
8

Dylan tại Wedefy Dot Com ¶

15 năm trước

% php output.php 
hey F4LLCON!
28

% php output.php 
hey F4LLCON!
29

% php output.php 
hey F4LLCON!
8

% php output.php 
hey F4LLCON!
8

15 năm trước

% php output.php 
hey F4LLCON!
31

% php output.php 
hey F4LLCON!
32

% php output.php 
hey F4LLCON!
8

% php output.php 
hey F4LLCON!
8

bobertjean tại yahoo dot fr ¶

% php output.php 
hey F4LLCON!
34

% php output.php 
hey F4LLCON!
35

% php output.php 
hey F4LLCON!
8

PHP tại Ober-Mail Dot de ¶

13 năm trước

% php output.php 
hey F4LLCON!
37

% php output.php 
hey F4LLCON!
38

% php output.php 
hey F4LLCON!
8

Marcel G ¶

12 năm trước

% php output.php 
hey F4LLCON!
40

% php output.php 
hey F4LLCON!
41

% php output.php 
hey F4LLCON!
42

% php output.php 
hey F4LLCON!
8

Dylan tại Wedefy Dot Com ¶

15 năm trước

% php output.php 
hey F4LLCON!
44

% php output.php 
hey F4LLCON!
45

% php output.php 
hey F4LLCON!
46

% php output.php 
hey F4LLCON!
8

% php output.php 
hey F4LLCON!
8

15 năm trước

% php output.php 
hey F4LLCON!
48

% php output.php 
hey F4LLCON!
49

% php output.php 
hey F4LLCON!
50

% php output.php 
hey F4LLCON!
51

% php output.php 
hey F4LLCON!
8

% php output.php 
hey F4LLCON!
8

Mandor tại Mandor Dot Net

% php output.php 
hey F4LLCON!
53

Làm cách nào để lưu vị trí tệp trong PHP?

PHP $ target_path = "hình ảnh/"; $ target_path = $ target_path.basename ($ _files ['userFile'] ['name']); Move_upLoaded_File ($ _Files ['userFile'] ['tmp_name'], $ target_path); ?> Khi tệp (hình ảnh) được lưu tại đường dẫn được chỉ định ... điều gì sẽ xảy ra nếu tôi muốn lưu tệp với một số tên mong muốn ....$target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

Làm cách nào để lưu một tệp trong PHP?

Mã PHP: Lưu một tệp với fopen / fwrite 'myFile1.txt ';$ fp = fopen ($ file, "w") hoặc die ("Không thể mở tệp $ để viết!");fwrite ($ fp, $ data) hoặc die ("không thể ghi các giá trị vào tệp!");fclose ($ fp);echo "Đã lưu vào tệp $ thành công!";?>fopen / fwrite 'myfile1. txt'; $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); fwrite($fp, $data) or die("Couldn't write values to file!"); fclose($fp); echo "Saved to $file successfully!"; ?>

Làm thế nào để gửi tiêu đề trong PHP?

Hàm tiêu đề () là hàm sẵn trong PHP được sử dụng để gửi tiêu đề HTTP thô ...
Tiêu đề $: Tham số này giữ chuỗi tiêu đề.....
$ Thay thế: Đó là tham số tùy chọn ..

Làm thế nào để buộc tải xuống tệp từ máy chủ từ xa trong PHP?

Sử dụng hàm readfile () với tiêu đề loại nội dung ứng dụng/file-to-save, để tải xuống tệp zip từ URL từ xa bằng PHP.Tiêu đề ("Loại nội dung: Ứng dụng/X-File-to-Save");Tiêu đề ("Xác định nội dung: tệp đính kèm; fileName =". basEname ($ remoteUrl));. header("Content-type: application/x-file-to-save"); header("Content-Disposition: attachment; filename=". basename($remoteURL));