View previous topic :: View next topic |
Author |
Message |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sat Jul 24, 2004 4:10 am Post subject: Open txt file & Delete content. |
|
|
This may sound stupid but im having a bit of trouble with one of my newest
PHP scripts called ::Lock Down:: v1 and I need someone to tell me how to
open a text file in PHP and delete any item I select using a form.
My PHP Book doesn't teach this to me and I tried so many times
to at least try this using a for loop , can someone help me :(
About ::Lock Down:: v1
This is a script im working on that will be similar to my last script which
is ::File Limit:: but this time it won't ban a user after a certain amount
of hits , it will just e-mail you a msg if someone hotlinks your file and
will log downloads to the admin section. Have A Nice Day ! |
|
Back to top |
|
 |
masa -
Joined: 05 Apr 2004 Posts: 182 Location: Hong Kong
|
|
Back to top |
|
 |
iNaNimAtE -
Joined: 05 Nov 2003 Posts: 2381 Location: Everywhere you're not.
|
Posted: Sat Jul 24, 2004 8:22 am Post subject: |
|
|
...mind showing us the code? _________________ Bienvenidos! |
|
Back to top |
 |
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sat Jul 24, 2004 9:16 am Post subject: |
|
|
I don't have any code thats why im posting this , I need to know how to
open a txt file in PHP and delete what ever element I want inside that
file by using a form , masa that is very stupid what you said lol. |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sun Jul 25, 2004 5:08 am Post subject: |
|
|
I waited over a day and no one seems to really care about this topic , can
some one please for the love of god help me get this stupid thing working.
All I ask is that someone tell me how to open a text file
with php using fopen() or something else and allow me to
delete certain parts of that file using an html form. :( |
|
Back to top |
|
 |
iNaNimAtE -
Joined: 05 Nov 2003 Posts: 2381 Location: Everywhere you're not.
|
Posted: Sun Jul 25, 2004 7:46 am Post subject: |
|
|
TRUSTpunk wrote: | I waited over a day and no one seems to really care about this topic , can
some one please for the love of god help me get this stupid thing working. |
No TRUSTpunk, it's not that we don't want to help you, it's that no one knows!
Believe me, I would really love to help you out, but I have no idea how the content would end up in the HTML form, and then somehow be seperated into deletable strings...? _________________ Bienvenidos! |
|
Back to top |
 |
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sun Jul 25, 2004 8:32 am Post subject: |
|
|
Only Eznetlinks knows this type of stuff and I haven't seen him on the
forum lately so I guess I will post on another forum and hope to get an
answer from someone because this is something I always wanted to know. |
|
Back to top |
|
 |
iNaNimAtE -
Joined: 05 Nov 2003 Posts: 2381 Location: Everywhere you're not.
|
Posted: Sun Jul 25, 2004 8:40 am Post subject: |
|
|
Yes, it seems the forum has been sort of "dead" lately (maybe it's just in contrast to others I've seen).
Anyway, if you get an answer, please tell us and post it here, because I would sure like to know. _________________ Bienvenidos! |
|
Back to top |
 |
 |
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Sun Jul 25, 2004 11:01 am Post subject: |
|
|
Hi, ive just uploaded a pdf file that might answer your question.
you can find it here:
"410 Gone"
It is the PHP4 tutorial.pdf file
pay particular attention to "Hour 10: Working with Files" on pg 175
iNaNimAtE wrote: | Yes, it seems the forum has been sort of "dead" lately |
I wonder how many regular users there are
Edit: removed the link _________________ Anthony R
Roganty | Links-Links.co.uk
Last edited by roganty on Wed Nov 01, 2006 11:19 pm; edited 1 time in total |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sun Jul 25, 2004 4:28 pm Post subject: |
|
|
Im sorry but that did not help me , it does not explain how to open a text
file and give me an option to delete lines from that file , I will keep looking |
|
Back to top |
|
 |
Stone-D -
Joined: 09 Jan 2004 Posts: 90
|
Posted: Sun Jul 25, 2004 7:56 pm Post subject: |
|
|
Just for the record, I'm not helping out because I reckon I can do it in perl, but I have no clue with php. :P _________________ --
Look, no SIG! |
|
Back to top |
|
 |
needforspeed -
Joined: 20 Oct 2003 Posts: 69 Location: Kansas
|
Posted: Sun Jul 25, 2004 10:28 pm Post subject: |
|
|
ok, I'm just learning php myself, so I don't know exactly how to do it. If you want to send me some details, preliminary code, or something I can try to get it working.
A thought though, you could open the file, and read in all of the contents, then, search for the string from the html form. If you find it, replace it or whatever you want while it's in memory, then, you can write all of the information you read in and changed back into the file, overwriting the old info.
That isn't the best method of doing it, but I'm a newbie to php programming and I think that would be all that I could code right now. I think the proper way is to open the file in read write mode, read until you find what you are looking for, replace only that info, then close the file, but like I said, I haven't learned how to do all of that yet. _________________ My site and forums |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Mon Jul 26, 2004 1:40 pm Post subject: |
|
|
I tried to replace it like your talking about but the only way I can really
do this is if I delete the file after reading the contents and then write to
the new file with my new settings , the only way is to over write it. :)
I will work on this later today ! |
|
Back to top |
|
 |
needforspeed -
Joined: 20 Oct 2003 Posts: 69 Location: Kansas
|
Posted: Mon Jul 26, 2004 6:30 pm Post subject: |
|
|
you can have the file automatically truncated. Look at http://us2.php.net/manual/en/function.fopen.php
If you close the file after reading it into memory, then reopen it in mode "w", it will truncate the file to size 0, allowing you to write everything back to it without having to delete the file. _________________ My site and forums |
|
Back to top |
|
 |
mtbiking -
Joined: 18 Mar 2004 Posts: 66
|
Posted: Mon Jul 26, 2004 8:30 pm Post subject: |
|
|
Code: | <?
$path = 'C:\Program Files\Abyss Web Server\htdocs';
$textfile = $_SERVER["QUERY_STRING"];
if(is_dir($path))
{
$data = fopen("$path/$textfile.txt","r");
$content = fgets($data,1000000);
fclose($data);
$content = $_SERVER["content"] ;
echo "$content" ;
$data = fopen("$path/$textfile.txt","w");
fwrite($data, $content);
fclose($data);
}
?> |
that may work but it may need a bit of>>> tinkering <<< _________________ Check It Out... Nochex Classifieds |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Tue Jul 27, 2004 2:28 am Post subject: |
|
|
Quote: | you can have the file automatically truncated. Look at http://us2.php.net/manual/en/function.fopen.php
If you close the file after reading it into memory, then reopen it in mode "w", it will truncate the file to size 0, allowing you to write everything back to it without having to delete the file. |
Wow , Thanks "needforspeed" , I finally have something to work with. |
|
Back to top |
|
 |
needforspeed -
Joined: 20 Oct 2003 Posts: 69 Location: Kansas
|
Posted: Wed Jul 28, 2004 12:17 am Post subject: |
|
|
TRUSTpunk wrote: | Quote: | you can have the file automatically truncated. Look at http://us2.php.net/manual/en/function.fopen.php
If you close the file after reading it into memory, then reopen it in mode "w", it will truncate the file to size 0, allowing you to write everything back to it without having to delete the file. |
Wow , Thanks "needforspeed" , I finally have something to work with. |
the code that mtbiking posted looks exactly like what I was talking about. I'm not very experienced with php or html, but it follows the process I had in my mind. I've done similar things in c++ programs, which I have a couple semesters under my belt. _________________ My site and forums |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Wed Jul 28, 2004 10:59 am Post subject: |
|
|
I need more help , that didn't really work out to well , can someone tell
me how I can read files in a directory and record the results in a txt file.
What im trying to do is record all domain files like localhost.txt in a
directory to a txt file that way its easier but when I tried this using
a loop , only the last domain in the directory gets recorded. Help!
Here's some code !
Code: | <?php
$path = "dir";
$open = opendir ($path);
while ($file = readdir ($open)) {
if (eregi("..." . ".txt$" , "$file")) {
$total = str_replace ('.txt','',$file);
}
echo "$total<br>";
$data = "host.txt";
$record = fopen ($data, "w");
fwrite ($record, "$total\r\n");
fclose ($record);
}
?> |
As you can see the writing process is within the loop so why do I get only
one domain recorded from that directory to the text file , is their a bug ?
Edit: I will keep working on this and post my progress , im sure I will
figure something out , untill than , does any one have some code ? |
|
Back to top |
|
 |
Anonymoose -
Joined: 09 Sep 2003 Posts: 2192
|
Posted: Wed Jul 28, 2004 4:11 pm Post subject: |
|
|
If I'm reading it right, you're using
$record = fopen ($data, "w");
to open the file you're outputting the domains to, and doing it within the while loop.
Using w to open the file deletes the existing content. Each time you execute the loop, and the fopen, you're deleting the content, adding the single line and closing the file - this is why you only end up with the final domain; when the loop finishes, you're only left with the last thing written in the file.
Use "a" to open the file for appending data, rather than clearing the existing content - it's explained in more detail in the php manual page needforspeed linked to. |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Wed Jul 28, 2004 4:41 pm Post subject: |
|
|
I can't believe how stupid I was , it works !
Thanks Anonymoose , You know your PHP !!!!! |
|
Back to top |
|
 |
Anonymoose -
Joined: 09 Sep 2003 Posts: 2192
|
Posted: Wed Jul 28, 2004 5:14 pm Post subject: |
|
|
Glad it worked - but the only PHP I've ever written was that log file splitter, and I've never touched it before or after that, although it is pretty similar to C++ which I spent two years developing in - just a lot more forgiving :D |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Wed Jul 28, 2004 8:06 pm Post subject: |
|
|
I could use some help on one last thing , when you list directory content
you see 3 dots and I don't want that to be shown in the written file , if I
use str_replace() , it removes the 3 dots in the text file but leaves 2 gaps.
How do I keep my written file from looking like
\r\n
\r\n
host1.com
host2.com
host3.com
The \r\n characters are the gaps , their not shown in the file
their just line spaces , basicly it skips 2 lines before writting. |
|
Back to top |
|
 |
Anonymoose -
Joined: 09 Sep 2003 Posts: 2192
|
Posted: Wed Jul 28, 2004 9:54 pm Post subject: |
|
|
I'd add an if to the fwrite line so it doesn't write if $total is empty as a result of replacing your ... with nothing. |
|
Back to top |
|
 |
needforspeed -
Joined: 20 Oct 2003 Posts: 69 Location: Kansas
|
Posted: Thu Jul 29, 2004 12:54 am Post subject: |
|
|
umm...when you read in the contents of the directory, you need to check each item to make sure that that item is not a directory (assuming you don't want to put those into the txt file).
I use an image gallery script that does this. I think it would probably stop your problem of having the blank lines in there. I'll see if I can get you a code sample. _________________ My site and forums |
|
Back to top |
|
 |
needforspeed -
Joined: 20 Oct 2003 Posts: 69 Location: Kansas
|
Posted: Thu Jul 29, 2004 1:34 am Post subject: |
|
|
Code: | $i =1;
$files = array ();
$myDirectory = opendir("images/thumbs");
echo "<table width='$table_width' bgcolor = '$table_bg_color' border ='$table_border' cellpadding='0' cellspacing='10'><tr>";
while ($file = readdir($myDirectory)) {
if (($file != ".") && ($file != "..") && ($file != "index.php") && !(is_dir("images/$file")) )
{
$files[] = $file;
if (is_int($i / $cols)) {
list($width, $height) = getimagesize("images/$file");
echo "<td align='center'>";
?> |
_________________ My site and forums |
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Thu Jul 29, 2004 3:32 am Post subject: |
|
|
Ok , I know how to do this , I just compare the files to a directory and the
script will decide which one is shown , thank you so much for your help :)
Code: | if(is_file($file_name)) { |
Thats what I will be using ! |
|
Back to top |
|
 |
|