Solved

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

Posted on 2013-01-14
5
452 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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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 …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

816 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

12 Experts available now in Live!

Get 1:1 Help Now