我开发了一个用于WordPress的小插件(用于培训)。该插件允许获取表单数据,将其保存到CSV文件中,然后管理员可以下载该文件。
我的问题是我无法下载这个文件。当我点击下载按钮时,它会打开一个download.php页面但没有任何进一步的操作。
我尝试了不同的解决方案,但都没有起作用。这是主文件的代码:
但是什么都没起作用。问题是来自头文件吗?还是来自WordPress,需要特定的编写方式?
以下是开发者工具中响应头的截图:
我的问题是我无法下载这个文件。当我点击下载按钮时,它会打开一个download.php页面但没有任何进一步的操作。
我尝试了不同的解决方案,但都没有起作用。这是主文件的代码:
<?php
/*
Plugin Name: Form to CSV
Version: 1.0
Author: Grégory Huyghe
*/
// 1. Shortcode
function ftc_shortcode() {
readfile("form-to-csv.html", 1);
}
add_shortcode( 'form_csv', 'ftc_shortcode' );
// 1.1 CSS
function ftc_style() {
wp_register_style('stylesheet', plugins_url('form-to-csv.css', __FILE__));
wp_enqueue_style('stylesheet');
}
add_action('admin_init', 'ftc_style');
// 2. Onglet plugin dans panneau admin pour voir et télécharger les données collectées
function ftc_menu_item() {
add_menu_page(
__( 'Form to CSV', 'textdomain' ),
'Form to CSV',
'manage_options',
'form-to-csv',
'ftc_menu_plugin',
'dashicons-portfolio',
21
);
}
add_action('admin_menu', 'ftc_menu_item');
// 3. Ecrire les données dans un fichier
// 3.1 Variables
$error = '';
$fname = sanitize_text_field($_POST['prenom']);
$lname = sanitize_text_field($_POST['nom']);
$email = sanitize_email($_POST['email']);
$checkbox = implode(" / ", (array)$_POST['films']);
// 3.2 Clean_text
function clean_text($clean) {
$trimmed = trim($clean);
$stripped = stripslashes($clean);
$special = htmlspecialchars($clean);
return $clean;
}
// 3.3 Submission form
if(isset($_POST['submit'])) {
$success = true;
if(empty($_POST['prenom']) OR empty($_POST['nom']) OR empty($_POST['email'])) {
$error = '<p>Veuillez réessayer</p>';
} else {
$fname = clean_text($_POST['prenom']);
$lname = clean_text($_POST['nom']);
$email = clean_text($_POST['email']);
}
if($error == '' && $success = true) {
// Ecriture dans fichier csv
$file_open = fopen('C:\Users\huygh\Desktop\form-to-csv.csv', 'a');
$index = count(file('C:\Users\huygh\Desktop\form-to-csv.csv'));
if ($index == 0) {
$index = $index +1;
} else if ($index > 0) {
$index = $index +1;
}
$form_data = array(
'id' => $index,
'prenom' => $fname,
'nom' => $lname,
'email' => $email,
'films' => $checkbox
);
fputcsv($file_open, $form_data);
header( 'Location: index.php' );
exit();
}
}
// 4. Récupérer ses infos dans un custom post accessible depuis le panneau admin. Pas d'envoi de mail.
// 4.1 Récupérer et Afficher les données dans l'onglet du plugin
function ftc_menu_plugin() {
// if (isset($_GET['action']) && $_GET['action'] == 'download') {
// header('Location: C:\Users\huygh\Desktop\form-to-csv.csv');
// header('Content-Disposition: attachment; filename="form-to-csv.csv"');
// header("Content-Type: application/force-download");
// header("Content-Transfer-Encoding: Binary");
// header("Pragma: no-cache");
// header("Expires: 0");
//
// readfile('form-to-csv.csv');
//
// echo "toto";
// }
$counter = 0;
echo "<html><body><table>\n\n";
// Titres du tableau
echo "<thead>";
echo "<tr class=\"titles\">";
echo "<th>ID</th>";
echo "<th>Prénom</th>";
echo "<th>Nom</th>";
echo "<th>Email</th>";
echo "<th>Sélection</th>";
echo "</tr>\n";
echo "</thead>";
if (($file_read = fopen('C:\Users\huygh\Desktop\form-to-csv.csv', 'r')) !== FALSE) {
while (($data = fgetcsv($file_read)) !== FALSE && $counter < 20) {
echo "<tr>";
$counter++;
foreach ($data as $cell) {
echo "<td>" . $cell . "</td>";
}
echo "</tr>\n";
}
}
fclose($file_read);
echo "\n</table></body></html>";
// 4.2 Télécharger ce fichier .csv depuis l'onglet du plugin
?>
<a href="download.php" target="_blank">
<button class="button__csv">Télécharger fichier CSV</button>
</a>
<a href="delete.php" target="blank">
<button class="button__csv button__csv--delete">Supprimer données</button>
</a>
<?php
下载.php的代码如下:
<?php
header('Content-Disposition: attachment; filename="form-to-csv.csv"');
header('Content-Type: text/csv');
readfile('C:\\Users\\huygh\\Desktop\\form-to-csv.csv');
从主文件代码中可以看到,我还尝试了一种不使用download.php页面的解决方案,将a标签编写为以下形式:
<a href="?action=download" target="blank">
但是什么都没起作用。问题是来自头文件吗?还是来自WordPress,需要特定的编写方式?
以下是开发者工具中响应头的截图:
![enter image description here](https://istack.dev59.com/1Ifl4.webp)
download.php
时打开Web开发人员工具(F12),刷新页面,在工具中导航到“网络”,过滤“Doc”节点,然后发布响应头和响应本身的屏幕截图? - SaschaM78