Jump to content

get_file_contents, fopen & fwrite via ftp_connect


dameunmate

Recommended Posts

I'm trying to create an online web editor. It works fine for opening and writing files on the same server where the web editor is hosted, but I want it to be able to get_file_contents, fopen and fwrite files on a remote server, logging in with ftp_connect and ftp_login. I've noticed the functions ftp_get and ftp_fget but I'd rather not create another local file, if possible I want to open the remote file directly into a <texarea> then save it directly back to the remote server. Is this possible? So far I just get errors...  :shrug:

 

Here's an example of the code I'm trying to combine, to retrieve the contents of a file on a remote server and put them in a <textarea>:

 

<?php

    $conn = ftp_connect("ftp.domain.com");
    ftp_login($conn, "username", "password");
    ftp_pasv($conn, true);

  $filename='example.php';

// open file 
  $fh = fopen($filename, "r") or die("Could not open file!"); 

// read file contents
  $datax = fread($fh, filesize($filename)) or die("Could not read file!");
  $data=htmlspecialchars($datax);

// close file
  fclose($fh);

// close this connection
  ftp_close($conn);

echo '<textarea cols="200%" rows="50">'.$data.'</textarea>';

?>

 

And another failed example:

 

<?php

  $filename='editor.php';

  $fh = fopen("ftp://username:password@domain.com/example.php", "r") or die("Could not open file!"); 

  $datax = fread($fh, filesize($filename)) or die("Could not read file!");
  $data=htmlspecialchars($datax);

  fclose($fh);

echo '<textarea cols="200%" rows="50">'.$data.'</textarea>';

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.