Create file for download without saving on server with PHP(使用 PHP 创建文件以供下载而不保存在服务器上)
问题描述
假设我的 PHP 代码是
Say my PHP code was
<?php
// Get form variables using POST method
$MarriagePostMortem=stripslashes($_POST['MarriagePostMortem']);
$AutoSavesToKeep=stripslashes($_POST['AutoSavesToKeep']);
// Open and write file
$myFile="S3S.cfg";
$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, "The Sims 3 Starter Configuration
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
MarriagePostMortem $MarriagePostMortem
AutoSavesToKeep $AutoSavesToKeep
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-");
fclose($fh);
//echo "file written
";
?>
我想将文件直接发送给用户下载,而不将其保存到服务器.我真的不太擅长 PHP,那么我到底需要添加/更改什么?
I want to send the file directly to the user for download without saving it to the server. I'm really not all that great with PHP, so what exactly would I need to add/change to do so?
推荐答案
你应该怎么做,你的 html 标签应该是小写 &除非您输出值表,否则不要使用表格,将 div 与 css 一起使用.您也可以发回脚本,因为从表单接收到的变量是特定的,您只需检查正确的值,然后将数据发送回用户.
Heres how you should do it, your html tags should be lower case & stay clear of tables unless your outputting a table of values, use divs with css. You can also post back to the script, as variables received from the form are specific you just check there the right value and then send the data back to the user.
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$mpm = (!empty($_POST['mpm']) && ($_POST['mpm']=='true' || $_POST['mpm']=='false'))?$_POST['mpm']:die('Wrong parameter type');
$astk = (!empty($_POST['astk']) && is_numeric($_POST['astk']))?$_POST['astk']:die('Wrong parameter type');
$content = "The Sims 3 Starter Configuration".PHP_EOL;
$content .= "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-".PHP_EOL;
$content .= "MarriagePostMortem $mpm".PHP_EOL;
$content .= "AutoSavesToKeep $astk".PHP_EOL;
$content .= "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-".PHP_EOL;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename=S3S.cfg');
header('Content-Length: '.strlen($content));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
echo $content;
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sims 3 Starter Configuration</title>
</head>
<body>
<h1>Sims 3 Starter Configuration</h1>
<form action="" method="post">
<input type="reset" value="Clear changes">
<br />
<p>marriagepostmortem
<select name="mpm">
<option value="true">enabled</option>
<option value="false" selected="selected">disabled</option>
</select>
Keep marriage lines on family tree entries after death. experimental!</p>
<p>autosavestokeep
<select name="astk">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
Number of autosaves to keep.</p>
<p><input type="submit" value="Generate"></p>
</form>
<p>Drag the file onto the sims 3 starter to add configurations.</p>
</body>
</html>
这篇关于使用 PHP 创建文件以供下载而不保存在服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 创建文件以供下载而不保存在服务器上
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01