Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting, clearing, and creating files

    Is it possible to delete, clear, and create files with php?

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Yes..you can also create folders.
    Leonard Whistler

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    http://php.net/filesystem is the filesystem function reference, though you'll probably be only interested in fopen for now, as it has a list of the basic functions at the bottom (before the comments)
    Last edited by CraigC; 04-17-2004 at 09:40 PM.

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    WOW! Php is very powerful. That is amazing

    New question:

    Is it possible to tell php to delete something in the middle of a file, or start the cursor there?

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    see fseek()
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I didn't see that.

    I took one of there example scripts and get errors
    script:
    PHP Code:
    <?php

    $fp 
    fopen('test_file.txt');

    // read some data
    $data fgets($fp4096);

    // move back to the beginning of the file
    // same as rewind($fp);
    fseek($fp0);

    ?>
    errors:

    Warning: fopen() expects at least 2 parameters, 1 given in /data/hosted/stophon4/ftell.php on line 3

    Warning: fgets(): supplied argument is not a valid stream resource in /data/hosted/stophon4/ftell.php on line 6

    Warning: fseek(): supplied argument is not a valid stream resource in /data/hosted/stophon4/ftell.php on line 10


    Is it just my servers problem or am I not understanding this and missing something?

    EDIT: I got a better idea. What code would I need to delete a line or insert a line somewhere (and type on that linee)?

    I forgot to say earlier: All help is greatly appreicated
    Last edited by stophon4; 04-18-2004 at 06:15 PM.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    you still need to open with a mode , here r+ if you want to seek and write

    PHP Code:
    <?
    $fp 
    fopen'test_file.txt' 'r+' ) ;
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't really get that, what code would I need to delete line 3 of a document ?

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    just a line ? easist is along the lines of , note that '2' here is 'line 3' as the $file array counts from 0;

    PHP Code:
    <?
    $filename 
    'text.txt' ;
    /*read file into memory as an array*/
    $arrayfile$filename ) ;

    /*clear the contents of line 3*/
    unset($array[2]);

    /*save it*/
    $fp fopen$filename 'w' ) ;
    fputs$fp implode'' $array ) ) ;
    fclose$fp );
    ?>
    note this is only efficient for small files , when dealing with big files the above method gets resource intensive & fseek etc is a beter option
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am sending a deletion command for a line, but I am using a for loop variable. How do I retreive which line they chose to delete from the for loop?

    PHP Code:
    $lines count(file('guestbook.php')) + 1;
            
    $filename "guestbook.php";
            
    $arrayfile$filename ) ; 
            for (
    $i 1$i $lines; ++$i)
            {
             echo 
    "<form method='post' action='gb.php'><INPUT type='Submit' Name='delete' Value='Delete Post $i'></form>";
            } 
    Last edited by stophon4; 04-19-2004 at 09:39 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •