我在functions.php的wordpress admin中添加了一个自定义页面。并在同一页面上添加表单并尝试执行操作。
在这里你可以看到实际发生的事情,这里是我的网站goo.gl/jLfsDo的后端网址。用户名是admin,密码是testwooc ++。在左侧菜单栏上,标题为“上载COD文件”的管理菜单栏。我在当前主题的functions.php上添加了它,需要在同一页面添加动作结果。但它不起作用。
/*******************/
/*Form starts here*/
/*****************/
<div style="width:750px;">
<h1><span style="position:relative;top:-7px">Upload COD file</span></h1>
<form action="<?php the_permalink(); ?>" method="post" enctype="multipart/form-data">
Please choose a file: <input type="file" name="uploadFile"><br>
<input type="submit" value="Upload File">
</form>
/*Form action starts here */
<?php
if(isset($_POST['submit'])) {
$target_dir = basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;
$contents2 = "";
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
$CSVfp = fopen($target_dir, "r");
if($CSVfp !== FALSE)
while(!feof($CSVfp)) {
$data = fgetcsv($CSVfp, 3000, ",");
$contents2 .= $data[3];
$var_str = var_export($contents2, true);
$file = 'new_file.txt';
$var_str = str_replace("'",",",$var_str);
// $var_str2 = str_replace(" ",",",$var_str);
// $var_str3 = str_replace(",,",",",$var_str2);
$var_str3 = ltrim($var_str,"Pin,");
$var_str3 = implode(",", str_split($var_str3, 6))." ";
$var_str4 = str_replace(",,","",$var_str3);
file_put_contents($file, $var_str4);
}
}
else {
echo "Sorry, there was an error uploading your file.";
}
?>
<form method="get" action="new_file.txt">
<button type="submit">Download!</button>
</form>
<?php
}
?>