0

I want to open and write a txt file from another server, but I don't know how to do it? Can anyone help me?

<?PHP
   $fname=$_POST["fname"];

   $groupid=$_POST["groupid"];
   $myfile = fopen("Ji.txt", "a+") or die("Unable to open file!");
   fwrite($myfile, $fname."|".$groupid."\r\n");
   fclose($myfile)
?>

I want to replace Ji.txt with http://xxx.xxx.xx.x/ddd.txt

1
  • You'll have to use FTP or something similar to achieve this... Commented Dec 11, 2014 at 13:19

5 Answers 5

2

You can read the files over HTTP by using fopen or cURL.

You can't write to files over HTTP unless the server you are writing to is set up to understand an appropriate request. You could configure it to support PUT requests (make sure you have some kind of authn/authz system in place!) and make one using cURL.

Alternatively, you could use some other protocol to make the file available between servers (such as NFS).

Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively you can do it with the FTP functions

<?php
// Connect to remote FTP server
$conn = ftp_connect("ftp.example.com")or die("Cant connect to ftp server");
$login = ftp_login($conn, "username", "password");

// Open local (temporary) file handle
$fh = fopen("Ji.txt", "a+");

// Get remote file and save it to the previous file handle
if(ftp_fget($conn, $fh, "Ji.txt", FTP_ASCII))
{
    // Local file has now been updated with the content of the remote Ji.txt
    $fname = $_POST['fname'];
    $groupid = $_POST['groupid'];
    fwrite($fh, $fname.'|'.$groupid.'\r\n');
    if(ftp_fput($conn, "Ji.txt", $fh, FTP_ASCII))
        echo 'File saved to remote server';
    else
        echo 'Error saving to remote server';
}
else
    echo 'Error downloading remote file';

ftp_close($conn);
fclose($fh);
?>

Read more about ftp_fput etc here: http://php.net/manual/en/function.ftp-fput.php

1 Comment

I've updated the code as the original code only did a write operation to the remote server. The updated code first retrieves remote file, then appends the $_POST variables and then upload it to remote server.
0

As long as allow_url_fopen is set to True you can pass an URL to fopen() and read the data. Writing however, is impossible. However, you can write a wrapper around the file that accepts both GET & POSTS requests. A GET returns the contents of the file and a POST (or PUT) saves content to the files. The downside of this is that you must add security and it's a lot more complex to save the data.

Comments

0

Assuming you have access to the other server and the text file is available at a web address (ie you can see it in a browser) then you can use fopen to grab it, make your edits or whatever and then post back to a script that will handle the post data and save the file using curl. Example code below copied from this answer

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/path/to/file.txt'));
 curl_setopt($ch, CURLOPT_URL, 'http://server2/upload.php');
 curl_exec($ch);
 curl_close($ch);

Comments

0

Posting server should be something like this

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, array('ourfilename' =>'yourpath/test.txt'));
 curl_setopt($ch, CURLOPT_URL, 'http://urlreceivepost/getfile.php');
 curl_exec($ch);
 curl_close($ch);

Then getfile.php at your above url

if ($_POST) {
$file= $_POST['ourfilename'];
$data = file_get_contents($file);
$new = 'test.txt';
file_put_contents($new, $data);
var_dump($new);}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.