Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,630 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,048 people online right now. Registration is fast and FREE... Join Now!




Remote File Append

 
Reply to this topicStart new topic

Remote File Append

tel0r
post 11 May, 2005 - 09:43 AM
Post #1


New D.I.C Head

Group Icon
Joined: 22 Feb, 2005
Posts: 27



Dream Kudos: 200
My Contributions


Im trying to work out a good way to append a file sitting on one one webserver, from another webserver.

Basically, the webserver hosting the file allows no php,cgi,asp scripting. its just some free 10mb webspace that comes with a chat account.

The space is owned by some people (including my gf) who run some backgammon room in a chat, and they want an 'easily updateable' ie; "no ftp involved", news script.

So now i need the web based interface to connect to the ftp, and append a file with the text they want to add. This interface will be on some of my webspace, which allows php etc.

I've been reading about php + ftp, which seems easy enough, i just can't work out how to just add data to one specific file.

Maybe theres another way, not using the php ftp

any help or ideas would be great, thanks
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 11 May, 2005 - 09:45 AM
Post #2


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


It sounds like you're going to have to download the file, append to the file, then reupload... I don't think there is an easy way using JUST php to append to a file.

So maybe download to a temp file on the 1 server, do watcha need to do to it using some more PHP, then ftp back up when the script is done appending?
User is offlineProfile CardPM

Go to the top of the page

tel0r
post 11 May, 2005 - 11:38 AM
Post #3


New D.I.C Head

Group Icon
Joined: 22 Feb, 2005
Posts: 27



Dream Kudos: 200
My Contributions


any ideas on how to do it that way?
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 11 May, 2005 - 11:46 AM
Post #4


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


I would do something like this:

CODE

<?php

// open some file for reading
$remote_file = 'filetoappend.txt';
$handle = fopen('filetoappend.txt', 'w');

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $file
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 1)) {
echo "successfully written to $file\n";
} else {
echo "There was a problem with $file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);




$filename = 'filetoappend.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

  // In our example we're opening $filename in append mode.
  // The file pointer is at the bottom of the file hence
  // that's where $somecontent will go when we fwrite() it.
  if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
  }

  // Write $somecontent to our opened file.
  if (fwrite($handle, $somecontent) === FALSE) {
      echo "Cannot write to file ($filename)";
      exit;
  }
 
  echo "Success, wrote ($somecontent) to file ($filename)";
 
  fclose($handle);

} else {
  echo "The file $filename is not writable";
}





// open some file for reading
$file = 'filetoappend.txt';
$fp = fopen($file, 'r');

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
  echo "Successfully uploaded $file\n";
} else {
  echo "There was a problem while uploading $file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
?>



This is pieced together from the PHP.net documentation but it looks like it will work fine with a couple small tweaks.
User is offlineProfile CardPM

Go to the top of the page

tel0r
post 11 May, 2005 - 01:59 PM
Post #5


New D.I.C Head

Group Icon
Joined: 22 Feb, 2005
Posts: 27



Dream Kudos: 200
My Contributions


Changed it around a bit, seems to work great.. thanks for that

Just one more thing.

I need to find, and change a value in the page that gets downloaded.

i need to basically find the current amount (x), then add 1 to that amount, and add the new amount as the next entries array

'file ex:

array(1)

example(0) = "hello"
example(1) = "Hi"

i need to find the (1), change it to (2), and then add example(2) = "lalala" to the file.

all i really need to know is how to put the filecontents in a string, or if it already is in a string from that code you posted.. what string is

thanks
User is offlineProfile CardPM

Go to the top of the page

tel0r
post 11 May, 2005 - 07:09 PM
Post #6


New D.I.C Head

Group Icon
Joined: 22 Feb, 2005
Posts: 27



Dream Kudos: 200
My Contributions


I worked it out, not using that idea..

did it using a web based interface on the php server, and referenced the news file using <script src="">

works pretty well for a basic news updating script.

eg: http://www.mysite.com/Post.php?post=hey

CODE

<?php
$news    = $_REQUEST['post'];
if ($news != "")
{
$newsfile = '/full_path_to/news.txt';
$countFile = '/full_path_to/count.txt';
$newsmodule = '/full_path_to/news.bas';

//get count
$cc = file($countFile);
$c = $cc[0];
    
//add new post
$today=date("F j, Y, g:i a");
$addnews = "MakeNews($c) = \"$today|$news\"";
$fp = fopen($newsfile , "a");
fwrite($fp, $addnews);
fwrite($fp, "\n");
fclose($fp);

//rewrite news.bas
 //clear file
$fn = fopen($newsmodule, "w");
fwrite($fn, "");
fclose($fn);

 //re-write array declare/dimension
$fn = fopen($newsmodule, "w");
$basheader = "Dim Makenews($c) \n";
fwrite($fn, $basheader);
fclose($fn);

 //add news.txt
$fn = fopen($newsmodule, "a");
$fp = fopen($newsfile, "r");
$text = fread($fp,filesize($newsfile));
fwrite($fn, $text);
fclose($fn);
fclose($fp);

//amend count
$c++;
$fr = fopen($countFile, "w");
fwrite($fr, $c);
fclose($fr);
echo "News Updated";
}
?>


This post has been edited by tel0r: 11 May, 2005 - 07:57 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 03:52AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month