Upload file to object, then ftp object to server with PHP

Ok, This is going to sound idiotic, but it's how I tend to handle a lot of things.

I want to upload files to an FTP server with my website.  So, the webpage will have the file-upload script, but instead of saving it to that webserver, I want to keep it in memory, open up an FTP connection to my FTP server, then push the object over to that.

Basically... I never ever write anything to any of my web servers.  

Unfortunately, I spend 99.9999% of my time using .Net and storing my files on Amazon S3, and could have had this done in about 20 minutes, but this client is insisting on PHP and FTP.

so... any ideas?
LVL 4
DanielcmorrisAsked:
Who is Participating?
 
DanielcmorrisConnect With a Mentor Author Commented:
In .Net, I generally upload files to a datastream object, then open a connection to Amazon S3, and push the file there.  That way, when I have 20 webservers on a load-balancer, they can all simply pull the file from S3.

It seems that every solution involves writing to the HD, even if temporary.  It's kind of a company policy that all webservers are read-only and any data either goes to FTP storage, S3 Storage, Azure Storage or an Amazon RDS.

So, while I had just hoped to drop the file onto a file-server using the same technique, in this case, I just uploaded the file into the database and then wrote a script to download the file from the database when it was requested.  I generally avoid keeping files in databases, but it isn't a lot of files and they aren't big, so it will be fine.

Thanks for all your suggestions.

-Dan
0
 
Dave BaldwinFixer of ProblemsCommented:
Before you start, make sure that the host supports doing it.  I had one host cancel plain FTP and they don't provide SFTP in their version of PHP.  Another had no idea how to make it work.  Probably still don't and they change passwords every now and then so I have to go back in and get a new one for the client.
0
 
Ray PaseurCommented:
There are a lot of moving parts to this question, so I'll just try to address a few.

1. You would need to have an upload script.  The upload process has security issues, so read the man pages carefully.  You may want to password-protect the script. The file that you upload MUST be saved on the web server.  After the FTP completes, it can be deleted.
// MANUAL REFERENCE PAGES YOU MUST UNDERSTAND TO UPLOAD FILES
// http://php.net/manual/en/features.file-upload.php
// http://php.net/manual/en/features.file-upload.common-pitfalls.php
// http://php.net/manual/en/function.move-uploaded-file.php
// http://php.net/manual/en/reserved.variables.files.php
// MANUAL PAGES THAT ARE IMPORTANT IF YOU ARE DEALING WITH LARGE FILES
// http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
// http://php.net/manual/en/ini.core.php#ini.post-max-size
// http://php.net/manual/en/info.configuration.php#ini.max-input-time

2. You would need an FTP script.  I have not tested any of the user-contributed notes but some of them look fairly well thought-out.
http://php.net/manual/en/ref.ftp.php

After that, it's just integration and, of course, error checking.  But since there are a lot of moving parts and it's an unusual design pattern, you might want to consider hiring a programmer who is familiar with PHP.  It could save you weeks of work.

Best of luck with the project, ~Ray
0
 
Dave BaldwinFixer of ProblemsCommented:
That's certainly an acceptable solution.  But you can't run PHP on a webserver without temporary write-able storage.  I doubt that you can run any .NET without it either.  In addition, any file uploads are received by the web server software and saved in temporary storage before they are passed on to the PHP or .NET software.
0
 
DanielcmorrisAuthor Commented:
I simply went a different route.
/* HOW TO UPLOAD A FILE TO THE DATABASE */

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
      $fileType = $_FILES['userfile']['type'];

      $fp      = fopen($tmpName, 'r');
      $content = fread($fp, filesize($tmpName));
      $content = addslashes($content);
      fclose($fp);

      if(!get_magic_quotes_gpc())
      {
            $fileName = addslashes($fileName);
      }
      $con = mysql_connect($myServer,$myUser,$myPasswd);
      if (!$con){die('Could not connect: ' . mysql_error());}
      mysql_select_db($myDB, $con);

$query = "delete from upload;";
      mysql_query($query) or die('Error, query failed');

      $query = "INSERT INTO upload (name, size, type, content ) ".
            "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

      mysql_query($query) or die('Error, query failed');
      mysql_close($con);

      echo "<br>File $fileName uploaded<br>";
}
?>


<html><body>
 <form method="post" enctype="multipart/form-data">
                                    
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
                                          <tr><td colspan="2"><h4>Upload Membership File</h4></td></tr>
                                          <tr>
                                          <td width="246">
                                          <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
                                          <input name="userfile" type="file" id="userfile">
                                          </td>
                                          <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
                                          </tr>
                                          </table>
</form>
</body>
</html>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.