writing a text file on server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
merleore
Forum Newbie
Posts: 4
Joined: Wed Apr 15, 2009 1:08 pm

writing a text file on server

Post by merleore »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have been trying to write a text file to a server.

Code: Select all

$File = "ftp://username:password@the-ace-of-clubs.com//public_html/testwrt/example.txt"; 
$Handle = fopen( $File, "w" ) or die("Couldn't open $file");
 
$Data = "Jane Doe\n"; 
echo " data >$Data<- <br>";
fwrite($Handle, $Data); 
$Data = "Bilbo Jones\n"; 
echo " data >$Data<- <br>";
fwrite($Handle, $Data); 
fclose($Handle);

It does not return and error or say that the file couldn't be opened. Yet it does not appear on the server.

suggestions ?

thanks.

Merleore


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: writing a text file on server

Post by McInfo »

Put this at the top of your script:

Code: Select all

error_reporting(E_ALL);
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 2:35 pm, edited 1 time in total.
merleore
Forum Newbie
Posts: 4
Joined: Wed Apr 15, 2009 1:08 pm

Re: writing a text file on server

Post by merleore »

ok I did that and no error was reported and still no text file.

thanks for your input.

Could use other suggestions on how to get it to work.

Merleore
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: writing a text file on server

Post by McInfo »

It could be the double slashes:
ftp://username:password@the-ace-of-clubs.com//public_html/testwrt/example.txt
You can test that you can read the file with this script:

Code: Select all

<?php
error_reporting(E_ALL);
$file = 'ftp://username:password@the-ace-of-clubs.com//public_html/testwrt/example.txt';
header('Content-Type: text/plain');
echo '{BEGIN}';
echo file_get_contents($file);
echo '{END}';
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 2:37 pm, edited 1 time in total.
merleore
Forum Newbie
Posts: 4
Joined: Wed Apr 15, 2009 1:08 pm

Re: writing a text file on server

Post by merleore »

I have already proved I can read the text file - I just have a problem writing one.

Everything appears to work ok but nothing appears on the server. I have checked to see if I can write to server and
everything I see says yes.

Thanks - still looking for solution

Merleore
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: writing a text file on server

Post by McInfo »

Well, I learned something new today! It is not easy to wade through the documentation on stream contexts to get just what you need.

Try this:

Code: Select all

<?php
error_reporting(E_ALL);
$link = 'the-ace-of-clubs.com//public_html/testwrt/example.txt';
$file = 'ftp://username:password@'.$link;
echo 'Starting insertion... ';
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);
if ($fh = fopen($file, 'w', 0, $stream_context))
{
    fputs($fh, 'Insertion string');
    fclose($fh);
}
else
{
    die('Could not open file.');
}
echo 'Insertion complete: ';
echo '<a href="http://'.$link.'">http://'.$link.'</a>';
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 2:38 pm, edited 1 time in total.
merleore
Forum Newbie
Posts: 4
Joined: Wed Apr 15, 2009 1:08 pm

Re: writing a text file on server

Post by merleore »

thanks for the reply.

I tried the code listed 2 different ways. Using what you had and then removing "public_html" in the call list .

Either way it did not work. I still do not get an error but no text file appears on the website.

Still looking .

Thanks.

Merleore
Post Reply