How do I create a PHP file in WordPress using dashboard - php

I want to create a PHP file through WordPress dashboard without using ftp/c-panel, as I have checked and I found that you can create a PHP file through adding code in header.php But I don't have header.php in child theme as well and no access to cPanel. Any suggestion how I can create php file from WordPress dashboard appearance->editor by putting some code in functions.php?
Thank you in advance.

Put this code in your function.php file and run the site
add_action( 'init', 'createnewfile' );
function createnewfile()
{
$myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
}

I have modified the answer to be able to:
The function only runs when the theme is "activated".
And, if for some reason the file exists, don't overwrite the existing one.
function createnewfile() {
$filename = dirname(__FILE__)."/newfile.php";
if (file_exists($filename)){
//file exists, then it does nothing.
} else{
$myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
$txt = "Tienes que editar este archivo2\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
add_action( 'after_switch_theme', 'createnewfile' );

Related

Session data to text file showing nothing

I have created something that once text is put inside of a text box and submit is pressed it sends this to a server and it is then stored in a session. On the press of another button you can retrieve this text and it is then transferred from the session to a txt file.
Yesterday I had this working and it was printing to my txt file but now all of a sudden it wont do it at all, can anyone spot any issues that would cause this?
This is ran when the retrieve button is pressed:
<?php session_start(); ?>
<?php
print_r($_SESSION ["input_data"]);
$myfile = fopen("TestingOut.txt", "a+") or die("Unable to open file!");
$txt = $_SESSION["input_data"];
fwrite($myfile, $txt);
fclose($myfile);
?>
I have narrowed it down to the $_SESSION["input_data"] part as it will print some random text in place for he session as seen above.
This is the creation of the session array:
<?php session_start(); ?>
<?php
$_SESSION["input_data"][] = $_POST["input"];
echo $_POST["input"];
?>
Many thanks to #marekful , I was tryign to print an array into a text file, I used implode() to fix this:
$strings = implode(" ", $_SESSION["input_data"]);
$myfile = fopen("TestingOut.txt", "a+") or die("Unable to open file!");
$txt = $strings;
fwrite($myfile, $txt);
fclose($myfile);

creating a custom redirecting page

I want to make a page like
www.example.com/redirect.php
above page must redirect to another page like
www.google.com
But whenever I open www.example.com/redirect.php?changeurl=www.yahoo.com
from now onwards www.example.com/redirect.php
page should start redirecting to yahoo.com
if changeurl=youtube.com
from now onwards
the page www.example.com/redirect.php must star redirecting to youtube.com
How can I do that without using SQL database, only by using a single file "redirect.php"
Please ask if you need more information about this question
First off you should at least have some code already written.
But try using this:
redirect.php:
<?php
if(!file_exists("redirect_url.txt")) {
$createfile= fopen("redirect_url.txt", "w") or die("Unable to open file!");
$txt = "https://www.google.com";
fwrite($createfile, $txt);
fclose($createfile);
}
if(isset($_GET['changeurl'])) {
$editfile= fopen("redirect_url.txt", "w") or die("Unable to open file!");
$txt = $_GET['changeurl'];
fwrite($editfile, $txt);
fclose($editfile);
header("Location: " . $_GET['changeurl']);
} else {
$myfile = fopen("redirect_url.txt", "r") or die("Unable to open file!");
$url = fgets($myfile);
fclose($myfile);
header("Location: " . $url);
}
?>
Also make sure in the ?changeurl You always use http:// or https://, Since you are not using a database you can use a .txt file.

How to write a html file using PHP?

I used below HTML code and php code to Write new files.
<html>
<body>
<form action="write.php" method="get">
ID : <input type="text" name="ID"><br>
<input type="submit">
</form>
</body>
</html>
<?php
$myfile = fopen($_GET["ID"] , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
?>
If I submit TEST to html form , php code writes a file named "test". it hasn't a file extension.
How to write "TEST.html" with above code.?
Add the extension to the filename before opening the file:-
$filename = $_GET["ID"] . '.html';
$myfile = fopen($filename, "w") or die("Unable to open file!");
Note You are opening up yourself to all kinds of security issues here.
just use this code:-
fopen($_GET["ID"].'.html' , "w")
You need to add .html with a !empty() check there, like below:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html" , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
}
?>
To open and read this file:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html", "r") or die("Unable to open file!");
echo fread($myfile,filesize($_GET["ID"]));
fclose($myfile);
}
?>
OR:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$file = fopen($_GET["ID"]."html","r") or die("Unable to open file!"); ;
while(! feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
?>
Note:-
In my opinion you need .txt rather than .html.An HTML file with text like test really have no mean, or server any useful purpose. BTW it's up-to-you what extension you want. I just gave my opinion.

Trying to build a file in PHP and use fwrite to file

trying to figure out how i can make a and save a file while including default info from another file. i tried include but it did not work. any suggestion on how to build this file?
<?php
$wrtID = $_POST["fileID"];
SQL statement here to get relevant info
mkdir("CNC/$wrtID", 0770, true);
?>
<?php
$batfile = fopen("CNC/$wrtID/$wrtID.bat", "w") or die("Unable to open file!");
$txt = "
#ECHO OFF
#ECHO **** Run NC-Generator WOODWOP 4.0 ****
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sl.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sr.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-dc.mpr
#ECHO **** Done ****
";
fwrite($batfile, $txt);
fclose($batfile);
?>
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
$txt = "
include("defaultcnc.php");
if ( additional file pats needed ) {
include("component-1.php");
}
";
fwrite($slfile, $txt);
fclose($slfile);
?>
I don't see a problem in the first block of code.
On the second block, the interpreter will consider the second double quotes to mean the end of the string $txt = " include(". So everything after that would produce a PHP error.
But even if you escape those the mpr file will have the string include("defaultcnc,php"); but not the actual contents of that file. For that you should do file_get_contents("defaultcnc.php").
something like:
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
// set params to pass to defaultcnc.php
$value1 = 1;
$value2 = "I'm a text string";
$file = urlencode("defaultcnc.php?key1=".$value1."&key2=".$value2);
$txt = file_get_contents($file);
if ( additional file pats needed ) {
$txt .= file_get_contents("component-1.php");
}
fwrite($slfile, $txt);
fclose($slfile);
?>
I assume additional file pats needed means something to you. It should be a condition evaluating either true or false.

Use fwrite to create an .md file

I was wondering if there is any way to use fwrite() to create an .md file. I am creating a blog for a team that needs to upload content without me typing it out. The content is in markdown via an .md file for each post. I am trying to make a web page that will take content and create an .md file with it. Right now I just have a proof test running and am trying to save this to a .md file. However, when I try to change the extension to .md I keep getting errors. I'm kinda a PHP noob so your help is apricated.
TLDR
I'm wondering why this (fwrite("newfile.md", "w") or die("Unable to create file!") would throw an error while using .txt won't.
CODE
<?php
$myfile = fwrite("newfile.md", "w") or die("Unable to create file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Your issue would be that you aren't opening/creating a file, you're just attempting to write to it. You need to use fopen() before you write to it:
$myfile = fopen("newfile.md", "w") or die("Unable to create file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

Categories