Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Question: Is this possible? filter_list
Author
Message
Question: Is this possible? #1
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.

Reply

RE: Question: Is this possible? #2
Yes, easy using PHP.

Reply

RE: Question: Is this possible? #3
// I take no credit for this at all, I just found it online. Tongue

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 .

Reply

RE: Question: Is this possible? #4
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.

Reply

RE: Question: Is this possible? #5
Thank you guys, I will try it out.

Reply

RE: Question: Is this possible? #6
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.
[Image: 6tbE9Nw.png]

Reply

RE: Question: Is this possible? #7
Yes this is possible, obviously from all of the above posts.

Goodluck, Mafia.
[Image: b360081.gif]

Reply







Users browsing this thread: 1 Guest(s)