![]() |
|
Question: Is this possible? - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Design (https://sinister.li/Forum-Design) +--- Forum: Web Design (https://sinister.li/Forum-Web-Design) +--- Thread: Question: Is this possible? (/Thread-Question-Is-this-possible) |
Question: Is this possible? - Mafia - 08-31-2013 I know this might sound a bit stupid but is it possible to edit a .txt file I upload to my site, on the site. So for example. I upload a .txt to mydomain.com/blablabla.txt I want to be able to edit it on the site and not need to reupload it everytime I want to edit it. Is this possible? Thanks Mafia. RE: Question: Is this possible? - Slim - 08-31-2013 Yes, easy using PHP. RE: Question: Is this possible? - Sanctuary - 08-31-2013 // I take no credit for this at all, I just found it online. ![]() Spoiler:PHP Code: <?php
$fn = "file.txt"; // rename this to whatever you want
if (isset($_POST['content']))
{
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="7" cols="80" name="content"><?php readfile($fn); ?></textarea>
<input type="submit" value="Save">
</form>
<!-- Source: http://goo.gl/ipjKng -->
Save this to a .php file and upload it to your site. Go to http://YourSite.com/YourFileName.php and whatever you want to be on the text file, type it in the text area and click "Save". Whenever you want to check what is in the .txt file, you can use the same page, or you can go to http://YourSite.com/file.txt . RE: Question: Is this possible? - void - 08-31-2013 Like the past people have said, all you generally have to do is open it up in write mode and there you go. Though, I do believe everytime you reopen the file in write mode it'll truncate all contents in it. This goes through all programming languages, even Python and C++. You have to append the file, not open it up in write mode or else everytime you open it, nothing will be in it. Instead of Code: $fp = fopen($fn, "w")you need to do: Code: $fp = fopen($fn, "a")I may be wrong though, but take it from experience. When I was learning file input / output, I remember working a project where I'd save contents to a file, and everytime I'd reopen it using 'w', it'd truncate it, so appending the file will allow you to edit it without it deleting all the contents. RE: Question: Is this possible? - Mafia - 08-31-2013 Thank you guys, I will try it out. RE: Question: Is this possible? - iPokemon - 09-04-2013 Since this is in the JS and jQuery section, it could be done asynchronously with a GET/POST request to a PHP file for it to update the text file. But seeing as you've set your sights on PHP at the core, that's even better. RE: Question: Is this possible? - Berzerk - 09-05-2013 Yes this is possible, obviously from all of the above posts. Goodluck, Mafia. |