Jump to content

FTP upload instead of HTTP


monarlte

Recommended Posts

Hello.  I am having trouble using the regular php form-based (<input type="file" ... />) upload.  This uses the HTTP protocol to transfer the file right?  So, I was wondering if I could use FTP instead.

I created a script with the following:

<?php

$conn_id = ftp_connect('ftp.server.com');

$login_result = ftp_login($conn_id, 'userid', 'password');

$upload = ftp_put($conn_id, '/home/user/public_html/dir/filename.txt', 'D:\file.txt', FTP_BINARY);

?>

This is not working.  I am wondering if it is possible to upload a file from the user's hard drive using te ftp_put function or is this function only used for transfering files from the current web server to another server?

This is the error message I am getting:

Warning: ftp_put(D:\file.txt) [function.ftp-put]: failed to open stream: No such file or directory in /home/user/public_html/dir/ftp.php on line 7

thank you so much,
monarlte
Link to comment
https://forums.phpfreaks.com/topic/35873-ftp-upload-instead-of-http/
Share on other sites

Second post at: http://www.php.net/ftp, It does work, I tested it.

[code]<?php
if(isset($_POST['start_upload']) && $_FILES['txt_file']['name'] != ""){
   
  $local_file = $_FILES['txt_file']['tmp_name']; // Defines Name of Local File to be Uploaded

  $destination_file = "/".basename($_FILES['txt_file']['name']);  // Path for File Upload (relative to your login dir)

  // Global Connection Settings
  $ftp_server = "127.0.0.1";      // FTP Server Address (exlucde ftp://)
  $ftp_user_name = "username";    // FTP Server Username
  $ftp_user_pass = "password";      // Password

  // Connect to FTP Server
  $conn_id = ftp_connect($ftp_server);
  // Login to FTP Server
  $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
 
  // Verify Log In Status
  if ((!$conn_id) || (!$login_result)) {
      echo "FTP connection has failed! <br />";
      echo "Attempted to connect to $ftp_server for user $ftp_user_name";
      exit;
  } else {
      echo "Connected to $ftp_server, for user $ftp_user_name <br />";
  }

  $upload = ftp_put($conn_id, $destination_file, $local_file, FTP_BINARY);  // Upload the File
 
  // Verify Upload Status
  if (!$upload) {
      echo "<h2>FTP upload of ".$_FILES['txt_file']['name']." has failed!</h2><br /><br />";
  } else {
      echo "Success!<br />" . $_FILES['txt_file']['name'] . " has been uploaded to " . $ftp_server . $destination_file . "!<br /><br />";
  }

  ftp_close($conn_id); // Close the FTP Connection
}
?>

<html>
  <head>
      <script type="text/javascript">
          window.onload = function() {
              document.getElementById("progress").style.visibility = "hidden";
              document.getElementById("prog_text").style.visibility = "hidden";
          }
         
          function dispProgress() {
              document.getElementById("progress").style.visibility = "visible";
              document.getElementById("prog_text").style.visibility = "visible";
          }
         
      </script>
     
  </head>
  <body>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
          Please choose a file: <input name="txt_file" type="file" size="35" />
          <input type="submit" name="start_upload" value="Upload File" onClick="dispProgress()" />
      </form>
     
      <!-- Link to progress file: see http://www.ajaxload.info/ for animated gifs -->
      <img id="progress" src="http://www.your.site/images/progress.gif" />
      <p id="prog_text" style="display:inline;"> Upload Started!</p>
     
  </body>
<html>[/code]
spfoonenwb:  I don't think you are doing it the same way I am.

You first used a form of type enctype="multipart/form-data" to upload the file onto the web server and then you used ftp to transfer that file that is already on the web server.  The local file ($local_file) you are using is already on the web server as a temporary file in the php temporary upload directory.  That is not what I am looking to do.  Instead, I would like the local file to be on the user's computer (on the user's hardrive) and upload that file using ftp rather than the enctype="multipart/form-data" form.  I am still going to use a <input type="file" ... /> to get the file name from the user but it will not be part of the form that is submitted to the second or recursive php script.  Instead  I will use javascript to get that file name and submit it with the form that is submitted to the php script that handles it.  So, if you try to set your local file variable like so:

$local_file = 'D:\FileThatExists.txt';

and use that with the ftp_put(....) function will it still work for you?

thank you so much,
monarlte

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.