Solved

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

Posted on 2013-01-14
5
450 Views
Last Modified: 2013-01-20
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?
0
Comment
Question by:Danielcmorris
  • 2
  • 2
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38777331
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38777796
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
 
LVL 4

Accepted Solution

by:
Danielcmorris earned 0 total points
ID: 38779528
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38779553
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
 
LVL 4

Author Closing Comment

by:Danielcmorris
ID: 38798088
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHPStorm debugging issues 1 26
Whether to use true/false, yes/no or 0/1 11 51
Access Object's Property 9 22
phpmailer in WHILE loop - weird results 10 29
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now