Results 1 to 8 of 8

Thread: create a file

  1. #1

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236

    create a file

    if a file doesnt exists, how do I create on in php? simple one but I never really worked with files before.

  2. #2
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    well if your writing to a file, and it does not exist it will create it.

  3. #3

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    ok, this goes with my other question. I'm creating a file upload for where someone submits their zip file and I upload it to the file server and I want to add some kind of bar so they know how much is uploaded (I know thats going to be a pain). But for right now I'm trying to just throw text into a file on the ftp server... here's what my code looks like..

    <?php
    $file = fopen ("ftp://username[email protected]/uploads/text", "w");
    if (!$file) {
    echo "<p>Unable to open remote file for writing.\n";
    exit;
    }
    /* Write the data here. */
    fputs ($file, "This is my test" . "\n");
    echo("File Created?");
    fclose ($file);
    ?>

    now, when are no files in that directory it does nothing. When I create a text document with the same name text.txt it creates the text file with no extension. when the text file with no extension exists, it gives me an error that it cant over write existing file...


  4. #4
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    You have to give the file you are writing to a name, even if it does not exist yet

    $file = fopen ("ftp://[email protected]/uploads/text/sometextfile.txt", "w");

  5. #5

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    cool, I got that working. Now its typing a line of information into the file. But the new problem is that its looking to write to the file, the information from the remote user file and I keep getting this...

    Warning: file("C:\Programming\test.txt") - No such file or directory in /home/sites/home/web/member/uploadthefile.php on line 16

    Any ideas?

  6. #6
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    with fopen use 'a' instead of 'w'

  7. #7

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    alright, this thing has me tapping out... I get to a point where it works, then it doesnt work. here's my code.

    <?
    echo($thisfile); //just to see the tmp filename
    $fd = fopen ($thisfile, "r"); //$thisfile is from a file input type on the previous page
    while (!feof ($fd)) {
    $buffer = $buffer.fgets($fd, 4096);
    }
    fclose ($fd);

    mail("[email protected]", "Subject", $buffer, "FROM:[email protected]");
    //mail myself the $buffer string to make sure its got stuff in it (which is always does)

    $XXfileX = fopen ("ftp://username[email protected]/uploads/test.txtxt", "a+");
    fputs($XXfileX, $buffer."\n");
    fclose($XXfileX);
    ?>

    The thing is, it works sometimes and not others. Like if I post my file and refresh this php page around 3 times, it'll show up on the ftpserver. But not the first or second time. There seems to be no consistancy to when it goes through and I cant figure it out. Can anybody help?



  8. #8
    Lively Member scoutt's Avatar
    Join Date
    Aug 2002
    Posts
    84
    If filename begins with "ftp://" (not case sensitive), an ftp connection to the specified server is opened and a pointer to the requested file is returned. If the server does not support passive mode ftp, this will fail. You can open files for either reading or writing via ftp (but not both simultaneously).
    are you sure it supports passive mode?
    Just Imagine what you can do with the power of php.

    257 Tutorials and counting.
    529 Snippets and growing.

    Add yours today @
    SnippetLibrary.com

Posting Permissions

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



Click Here to Expand Forum to Full Width