Web EditorProf. Joerg Vollrath, 5.1.2022JavaScript, NodeJS, php |
<div contenteditable="true" class="over">
This text can be edited by the user.
</div>
<?php
$filename = $_GET['Typ'];
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
if ( file_exists(__DIR__ . DIRECTORY_SEPARATOR . "txt" . DIRECTORY_SEPARATOR . $filename) ) {
unlink( __DIR__ . DIRECTORY_SEPARATOR . "txt" . DIRECTORY_SEPARATOR . $filename);
echo "File $filename deleted $res";
} else {
echo "File $filename does not exist";
}
?>
Dir.php
<?php
// Folder where you want to get all files names from
$dir = "txt/";
/* Hide this */
$hideName = array('.','..','.DS_Store');
// Sort in ascending order - this is default
$files = scandir($dir);
/* While this to there no more files are */
foreach($files as $filename) {
if(!in_array($filename, $hideName)){
/* echo the name of the files */
echo "$filename
";
}
}
?>
Read.php
<?php
$filename = $_GET['Typ'];
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($filename.".txt");
// echo "x".$filename.".txtx\n";
echo $contents."\n";
?>
SavePOST.php
var xmlHttpObjectLog = new XMLHttpRequest();
function handleStateChangeLogX()
{
if (xmlHttpObjectLog.readyState >= 3) {
stText = xmlHttpObjectLog.responseText;
document.getElementById("result").innerHTML = stText;
}
}
trequest = "Typ=" + typ + "&Time=" + tstring
+ "&TextX=JSON" + JSON.stringify(editor.value);
xmlHttpObjectLog.open("POST","../php/SavePOST.php",false); // send to server
xmlHttpObjectLog.onreadystatechange = handleStateChangeLogX;
xmlHttpObjectLog.send(trequest);
PHP code in subdirectory /php
<?php
// $somecontent=$_POST['TextX'];
$post = file_get_contents('php://input'); // Gesamter String
$t1 = strpos($post,"Typ",0); // Typ
$t2 = strpos($post,"Time",0); // Typ
$t3 = strpos($post,"TextX",0); // Typ
$filename = substr($post, $t1 + 4, $t2 - $t1 - 5);
$fileX = substr($post, $t2 + 5, $t3 - $t2 - 6);
$somecontent = substr($post, $t3 + 6);
// unlink( __DIR__ . DIRECTORY_SEPARATOR . "videos" . DIRECTORY_SEPARATOR . "Test.txt");
// (C) UPLOAD DESTINATION
// ! CHANGE FOLDER IF REQUIRED !
$filePath = __DIR__ . DIRECTORY_SEPARATOR . "txt";
if (!file_exists($filePath)) {
if (!mkdir($filePath, 0777, true)) {
verbose(0, "Failed to create $filePath");
}
}
$filePath = $filePath . DIRECTORY_SEPARATOR . $filename;
rename($filePath . ".txt", $filePath . $fileX . ".txt");
$out = fopen($filePath . ".txt","w");
fwrite($out, $somecontent);
fclose($out);
echo "Filepath $filePath.txt Content $somecontent";
?>