5 Replies - 3416 Views - Last Post: 11 May 2005 - 08:09 PM Rate Topic: -----

#1 tel0r  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 22-February 05

Remote File Append

Posted 11 May 2005 - 10:43 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Remote File Append

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Remote File Append

Posted 11 May 2005 - 10:45 AM

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?
Was This Post Helpful? 0
  • +
  • -

#3 tel0r  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 22-February 05

Re: Remote File Append

Posted 11 May 2005 - 12:38 PM

any ideas on how to do it that way?
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Remote File Append

Posted 11 May 2005 - 12:46 PM

I would do something like this:

<?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.
Was This Post Helpful? 0
  • +
  • -

#5 tel0r  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 22-February 05

Re: Remote File Append

Posted 11 May 2005 - 02:59 PM

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
Was This Post Helpful? 0
  • +
  • -

#6 tel0r  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 22-February 05

Re: Remote File Append

Posted 11 May 2005 - 08:09 PM

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.co...st.php?post=hey

<?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 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1