Created
October 17, 2020 12:51
-
-
Save tinshade/c51dc6ef84975ffdc806c483656a2ea1 to your computer and use it in GitHub Desktop.
I just wanted to write a file to the directory and read it via JS for a very particular use-case. Here's the script if you know what you're doing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$conn = mysqli_connect('localhost', 'root', '', 'db_name'); | |
$row = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM table_name")); | |
if(!$conn){ | |
echo 'Nope, dead.'; | |
}else{ | |
$myfile = fopen("filename.txt", "w+"); | |
$txt = strval($row['index']); | |
fwrite($myfile, $txt); | |
fclose($myfile); | |
echo "Write complete."; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Writing a File with PHP and Reading the file with JS</title> | |
</head> | |
<body> | |
<h1>Here's what I found from your file!</h1> | |
<br> | |
<h5 id="result"></h5> | |
<script type="text/javascript"> | |
// As with JSON, use the Fetch API & ES6 | |
fetch('instagram_id.txt') | |
.then(response => response.text()) | |
.then(data => { | |
// Do something with your data | |
console.log(data); | |
document.getElementById('result').innerHTML = data; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment