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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Files and PHP

  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Files and PHP

    I was wondering how I might create a file using a PHP form action? I searched PHP.net but I didn't see any function to create a file. Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Searching with your eyes fopen usually works better

    http://us3.php.net/manual/en/function.fopen.php

    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, fopen wouldn't seem to create, but thanks anyways
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, so my meager quest to create a file creation system So I don't have to be at home to add files. My error is this

    Warning: fopen("", "x") - No error in c:\ibserver\www\fcreate\filecreater.php on line 4

    Warning: fwrite(): supplied argument is not a valid File-Handle resource in c:\ibserver\www\fcreate\filecreater.php on line 7

    and then my form action
    PHP Code:
    <?php
    $filename 
    $_POST['filename'];
    $filecontents $_POST['filecontent'];
    $open fopen($filename,"x");
    $chmod chmod($filename0777);
    if (
    is_writable($filename)) {
    fwrite($filename,$filecontents);
    echo 
    "$filename sucessfully created";
    }
    else {
    echo 
    "<B>Error:</b> Cannot write to file";
    }
    ?>
    I know there are probably many things wrong. Any help anyone?

    Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Warning: fopen("", "x") - No error in c:\ibserver\www\fcreate\filecreater.php on line 4

    That says it all.

    $filename = $_POST['filename'];

    Isn't being set. Read the errors.

  • #6
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I uploaded it to the web http://www.net-riches.com/includes/fcreate/

    Try for yourself, I get different errors now. Still using same code as above.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #7
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Do you have multipart set in your form attribute???

    Can you show us the html?

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Have you chmodded the directory the file's to be created in?

    bcarl314, the html is on the page he showed ( http://www.net-riches.com/includes/fcreate/ )

  • #9
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't believe I had, but where are the files going to be created in? The folder that the script is in?

    Thanks for your continued help
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by SDP2006
    Well, fopen wouldn't seem to create, but thanks anyways
    Are you blind or just South Carolinian?


    'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
    'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #11
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Originally posted by Nightfire
    Have you chmodded the directory the file's to be created in?

    bcarl314, the html is on the page he showed ( http://www.net-riches.com/includes/fcreate/ )
    D'OH <slaps what="head">

    Ok, in that case, change your <form> tag as below

    Code:
    <form method="POST" enctype="multipart/form-data" action="filecreater.php">

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is PHP 4.3.2 installed on your host? That's needed as a minimum requirement if you want to use the 'x' flags. Why don't you go with the standard 'w' argument?

    Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
    De gustibus non est disputandum.

  • #13
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I have 4.2.3 on my server. I changed it to "w". Now it is creating the file, but nothing is being written to the file. I still get one error, which I think is the reason the file is not being written -

    Warning: fwrite(): supplied argument is not a valid File-Handle resource in /usr/local/psa/home/vhosts/net-riches.com/httpdocs/includes/fcreate/filecreater.php on line 7

    If you wanna test it, http://www.net-riches.com/includes/fcreate/


    Thanks again for the continued help
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Assuming you use a similar code as shown above, you need to pass the file handle as an argument to frwrite(), not the file name itself. Basically this example illustrates the usage:

    PHP Code:
    $fp fopen('foo.bar''w');
    fwrite($fp"hello foo");
    fclose($fp); 
    De gustibus non est disputandum.

  • #15
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wonderful guys! It creates and writes to the file now. Step 1 complete. Now, when I acess the file I created, example http://www.net-riches.com/includes/fcreate/testone.php i get the errors as you can see. I typed in testone.php for the file name and
    PHP Code:
    <?php
    echo "TEST";
    ?>
    for the file contents. It is adding slashes to the " "'s. How do I stop it from doing that?
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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