Download, edit and reupload FTP file with PHP

  • Mitchell_
  • Newbie
  • Newbie
  • Mitchell_
  • Posts: 6

Post 3+ Months Ago

I am trying to get a server.cfg file from a FTP in a form on my website, using PHP. After this I want to edit the server.cfg file on the website, click 'submit' and reupload the edited file on the FTP. I did some research, but I still get some errors. I've managed to get the file in the form. But if I edit and save the form, I get errors. My PHP Code:

Code: [ Select ]
<?php

$config = array
(
'user' => 'Hunterr',
'pass' => '[PASSWORD]',
'domain'  => '[HOST]',
'file'   => '213.108.31.167-27030/etpro/server.cfg',    # relative to 'domain'
);

if(isset($_POST['submit']))
{
$fp = fopen($config['file'],'w');
fwrite($fp,stripslashes($_POST['newd']));
fclose($fp);

$ftp = ftp_connect($config['domain']);
ftp_login($ftp,$config['user'],$config['pass']);
ftp_pasv($ftp,TRUE);
ftp_put($ftp,$config['file'],$config['file'],FTP_BINARY);
ftp_close($ftp);

}
?>

<form width="440" height="440" method="post" action="<?=( $_SERVER['PHP_SELF'] )?>">
<textarea name="newd"><?=(    file_get_contents('ftp://'.$config['user'].':'.$config['pass'].'@'.$config['domain'].'/'.$co  nfig['file']) )?></textarea>
<input type="submit" name="submit" value="Save">
</form>
  1. <?php
  2. $config = array
  3. (
  4. 'user' => 'Hunterr',
  5. 'pass' => '[PASSWORD]',
  6. 'domain'  => '[HOST]',
  7. 'file'   => '213.108.31.167-27030/etpro/server.cfg',    # relative to 'domain'
  8. );
  9. if(isset($_POST['submit']))
  10. {
  11. $fp = fopen($config['file'],'w');
  12. fwrite($fp,stripslashes($_POST['newd']));
  13. fclose($fp);
  14. $ftp = ftp_connect($config['domain']);
  15. ftp_login($ftp,$config['user'],$config['pass']);
  16. ftp_pasv($ftp,TRUE);
  17. ftp_put($ftp,$config['file'],$config['file'],FTP_BINARY);
  18. ftp_close($ftp);
  19. }
  20. ?>
  21. <form width="440" height="440" method="post" action="<?=( $_SERVER['PHP_SELF'] )?>">
  22. <textarea name="newd"><?=(    file_get_contents('ftp://'.$config['user'].':'.$config['pass'].'@'.$config['domain'].'/'.$co  nfig['file']) )?></textarea>
  23. <input type="submit" name="submit" value="Save">
  24. </form>


Any idea's? Line 13 starts at: $fp = fopen($config['file'],'w'); so you know where the errors come from.

Errors:
Warning: fopen(213.108.31.167-27030/etpro/server.cfg) [function.fopen]: failed to open stream: No such file or directory in [LINK] on line 13

Warning: fwrite(): supplied argument is not a valid stream resource in [LINK] on line 14

Warning: fclose(): supplied argument is not a valid stream resource in [LINK] on line 15

Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: Name or service not known in [LINK] on line 17

Warning: ftp_login() expects parameter 1 to be resource, boolean given in [LINK] on line 18

Warning: ftp_pasv() expects parameter 1 to be resource, boolean given in [LINK] on line 19

Warning: ftp_put() expects parameter 1 to be resource, boolean given in [LINK] on line 20

Warning: ftp_close() expects parameter 1 to be resource, boolean given in [LINK] on line 21


Thanks in advance
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

Is the file on the same server as the form? If it is why don't you just use fopen and the path to the file for example fopen("../etpro/server.cfg");. I'm not sure where the file it in relation to your web root.

Also look into seeing if your server allows fopen a lot of hosting providers block the fopen due to security issues.
  • Mitchell_
  • Newbie
  • Newbie
  • Mitchell_
  • Posts: 6

Post 3+ Months Ago

Hello, thanks for the reply.

No, it is not on the same server. I have 2 FTP's 1 for the website and 1 for the server.cfg. The FTP from the server.cfg is not on a dedicated server of myself but I just hired a gameserver from YCN Hosting. So how will the path look like? $fp = fopen($config['ftp://ycn-hosting.[CANT-PUT-EXTENTION]/213.108.31.167-27030/etpro/server.cfg'],'w'); ?
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

Since it is on a different server fopen will not work so the FTP would be your option.

also fopen will not work in this case
PHP Code: [ Select ]
$fp = fopen($config['file'],'w');
fwrite($fp,stripslashes($_POST['newd']));
fclose($fp);
 
  1. $fp = fopen($config['file'],'w');
  2. fwrite($fp,stripslashes($_POST['newd']));
  3. fclose($fp);
  4.  


What you can do instead is make a server.cfg and hold it on the server with the form until the file gets FTPed ... So something like This. you might have to adjust the paths.

PHP Code: [ Select ]
$fp = fopen('temp_server.cfg','w');
fwrite($fp,stripslashes($_POST['newd']));
fclose($fp);
 
$ftp = ftp_connect($config['domain']);
ftp_login($ftp,$config['user'],$config['pass']);
ftp_pasv($ftp,TRUE);
ftp_put($ftp,'etpro/server.cfg','temp_server.cfg',FTP_BINARY);
ftp_close($ftp);
 
  1. $fp = fopen('temp_server.cfg','w');
  2. fwrite($fp,stripslashes($_POST['newd']));
  3. fclose($fp);
  4.  
  5. $ftp = ftp_connect($config['domain']);
  6. ftp_login($ftp,$config['user'],$config['pass']);
  7. ftp_pasv($ftp,TRUE);
  8. ftp_put($ftp,'etpro/server.cfg','temp_server.cfg',FTP_BINARY);
  9. ftp_close($ftp);
  10.  



Are you passing a directory with the Host when trying to connect to the ftp? If so that could be causing your ftp connection issue
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

also is 213.108.31.167-27030 a folder?
  • Mitchell_
  • Newbie
  • Newbie
  • Mitchell_
  • Posts: 6

Post 3+ Months Ago

Hello, thank you very much I am starting to understand it now :D

Yes, 213.108.31.167-27030 is a folder.
  • Mitchell_
  • Newbie
  • Newbie
  • Mitchell_
  • Posts: 6

Post 3+ Months Ago

What do you mean with 'Are you passing a directory with the Host when trying to connect to the ftp?' (sorry I dont know much about this).

I tried your code but I am getting the same errors only the first one is different:

Warning: fopen(temp_server.cfg) [function.fopen]: failed to open stream: Permission denied in [LINK] on line 13
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

OK the first thing what I mean is are you trying to connect with the host being like this ftp://ycn-hosting.yoursite.com/ or is it like ftp://ycn-hosting.com/yoursite if you could make a generic link that would help

Second the folder that contains your form doesn't have write permissions set.
  • Mitchell_
  • Newbie
  • Newbie
  • Mitchell_
  • Posts: 6

Post 3+ Months Ago

Hi, it's the second one. ftp://ycn-hosting.[extention]/213.108.31.167-27030/etpro/server.cfg is there complete path of the file. (Extention=com but I am not allowed to post links)
  • Mitchell_
  • Newbie
  • Newbie
  • Mitchell_
  • Posts: 6

Post 3+ Months Ago

I have this atm:

Code: [ Select ]
<?php

$config = array
  (
  'user' => 'Hunterr',
  'pass' => '[PASS]',
  'domain'  => 'ycn-hosting.[EXTENSION]:21',
  'file'   => '213.108.31.167-27030/etpro/server.cfg',    # relative to 'domain'
  );

if(isset($_POST['submit']))
  {
    $fp = fopen('temp_server.cfg','w');
fwrite($fp,stripslashes($_POST['newd']));
fclose($fp);

$ftp = ftp_connect($config['domain']);
ftp_login($ftp,$config['user'],$config['pass']);
ftp_pasv($ftp,TRUE);
ftp_put($ftp,'213.108.31.167-27030/etpro/server.cfg','temp_server.cfg',FTP_BINARY);
ftp_close($ftp);
  

  }
?>
  1. <?php
  2. $config = array
  3.   (
  4.   'user' => 'Hunterr',
  5.   'pass' => '[PASS]',
  6.   'domain'  => 'ycn-hosting.[EXTENSION]:21',
  7.   'file'   => '213.108.31.167-27030/etpro/server.cfg',    # relative to 'domain'
  8.   );
  9. if(isset($_POST['submit']))
  10.   {
  11.     $fp = fopen('temp_server.cfg','w');
  12. fwrite($fp,stripslashes($_POST['newd']));
  13. fclose($fp);
  14. $ftp = ftp_connect($config['domain']);
  15. ftp_login($ftp,$config['user'],$config['pass']);
  16. ftp_pasv($ftp,TRUE);
  17. ftp_put($ftp,'213.108.31.167-27030/etpro/server.cfg','temp_server.cfg',FTP_BINARY);
  18. ftp_close($ftp);
  19.   
  20.   }
  21. ?>


Code: [ Select ]
            <form width="440" height="440" method="post" action="<?=( $_SERVER['PHP_SELF'] )?>">
<textarea name="newd"><?=( file_get_contents('ftp://'.$config['user'].':'.$config['pass'].'@'.$config['domain'].'/'.$config['file']) )?></textarea>
<input type="submit" name="submit" value="Save">
</form>
  1.             <form width="440" height="440" method="post" action="<?=( $_SERVER['PHP_SELF'] )?>">
  2. <textarea name="newd"><?=( file_get_contents('ftp://'.$config['user'].':'.$config['pass'].'@'.$config['domain'].'/'.$config['file']) )?></textarea>
  3. <input type="submit" name="submit" value="Save">
  4. </form>


I dont get errors on line 13, but at 17 ($ftp = ftp_connect($config['domain']);)

Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: Name or service not known in [LINK] on line 17

Warning: ftp_login() expects parameter 1 to be resource, boolean given in [LINK] on line 18

Warning: ftp_pasv() expects parameter 1 to be resource, boolean given in [LINK] on line 19

Warning: ftp_put() expects parameter 1 to be resource, boolean given in [LINK] on line 20

Warning: ftp_close() expects parameter 1 to be resource, boolean given in [LINK] on line 21

I have set 777 on both ftp's for the files.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

the error means it can't find the FTP server. Domain name doesn't exist, name resolution failed. which triggers all the other errors below.

In your domain i see your passing the port as well
'domain' => 'ycn-hosting.[EXTENSION]:21',

Try somthing like this
PHP Code: [ Select ]
    <?php
    $config = array
      (
      'user' => 'Hunterr',
      'pass' => '[PASS]',
      'domain'  => 'ycn-hosting.[EXTENSION]',
      'port'  => '21',
      'file'   => '213.108.31.167-27030/etpro/server.cfg',    # relative to 'domain'
     );
    if(isset($_POST['submit']))
      {
        $fp = fopen('temp_server.cfg','w');
    fwrite($fp,stripslashes($_POST['newd']));
    fclose($fp);
 
    // Add the port here
    $ftp = ftp_connect($config['domain'], $config['port']);
   
    ftp_login($ftp,$config['user'],$config['pass']);
    ftp_pasv($ftp,TRUE);
    ftp_put($ftp,'213.108.31.167-27030/etpro/server.cfg','temp_server.cfg',FTP_BINARY);
    ftp_close($ftp);
     
      }
    ?>
 
  1.     <?php
  2.     $config = array
  3.       (
  4.       'user' => 'Hunterr',
  5.       'pass' => '[PASS]',
  6.       'domain'  => 'ycn-hosting.[EXTENSION]',
  7.       'port'  => '21',
  8.       'file'   => '213.108.31.167-27030/etpro/server.cfg',    # relative to 'domain'
  9.      );
  10.     if(isset($_POST['submit']))
  11.       {
  12.         $fp = fopen('temp_server.cfg','w');
  13.     fwrite($fp,stripslashes($_POST['newd']));
  14.     fclose($fp);
  15.  
  16.     // Add the port here
  17.     $ftp = ftp_connect($config['domain'], $config['port']);
  18.    
  19.     ftp_login($ftp,$config['user'],$config['pass']);
  20.     ftp_pasv($ftp,TRUE);
  21.     ftp_put($ftp,'213.108.31.167-27030/etpro/server.cfg','temp_server.cfg',FTP_BINARY);
  22.     ftp_close($ftp);
  23.      
  24.       }
  25.     ?>
  26.  
  • demonmaestro
  • Gold Member
  • Gold Member
  • User avatar
  • Posts: 679
  • Loc: Conroe, Texas

Post 3+ Months Ago

im suprised i havent seen this sooner. BUT why are you trying to change the server.cfg

As a long time wolf player. I know that there are some hosts that are really picky over the server.cfg file due to you able to change the max players.

I thought et died along time ago.. heh

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 92 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.