I'm trying to upload some data to an a server via ftp using the following code:

$output = "data";
$location = "ftp://username:password@location/filename.txt";

$fp = fopen($location, 'w');
fwrite($fp, $output);
fclose($fp); 

it works fine if the file does not exist but if the file is already located on the server the script comes up with an "unable to create stream" error....

    If your trying to overwrite the file try this:

    $output = "data"; 
    $location = "ftp://username:password@location/filename.txt"; 
    
    if(file_exists($location))
    {
      unlink($location);
    }
    // Delete existing file if there is one
    
    $fp = fopen($location, 'w'); 
    fwrite($fp, $output); 
    fclose($fp);
    

    or if your trying to ADD, try this:

    $output = "data"; 
    $location = "ftp://username:password@location/filename.txt"; 
    
    $fp = fopen($location, 'a+');
    / a+ = filepointer at end/create if no file exists
    
    fwrite($fp, $output); 
    fclose($fp);
    

    hope this helps

      Still no joy:

      Warning: File already exists in /..../dataoutput.php on line 80
      Warning: fopen("ftp://...@url/dir/file.txt", "a+") - File exists in /.../dataoutput.php on line 80
      Warning: fwrite(): supplied argument is not a valid File-Handle resource in /.../dataoutput.php on line 87
      Warning: fclose(): supplied argument is not a valid File-Handle resource in /.../dataoutput.php on line

        Try CHMOD'ing the file in question to 777 (all access), then try, (do this with an ftp client)

          Still no luck I've tried chmod the file to no avail still getting the same problem and i've checked the php.ini:
          allow_url_fopen = On

          I can't think what it can be

            20 days later

            OK I'm trying to create a bunch of files on the fly using FWRITE() but before I do that I want to be sure I can use FRWITE() to create a file that isn't yet there, and write the required data too it.

            here's what I'm using right now for example.
            $filename = "matrix.html";
            // open file
            $fh = fopen($filename, 'w') or die("Could not open file!");
            // write to file
            fwrite($fh, "Welcome to The Matrix, Neo") or die("Could not write to file");
            // close file
            fclose($fh);

            But this is doing nothing, it's not failing or kicking me any errors it appears to be completing but the file MATRIX.html is not created.

            I'm assuming it will be created in the same directory as this script since I haven't specified for it to go anywhere else. Any ideas? Once I figure this out I'm sure I can do what else is required. I searched thru the old posts here on this similar problem, but no real resolved soliution.

            I have CHMOD'd the directory the file is in to 777 so it really should work.

            ThanKS

              Write a Reply...