Solved

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

Posted on 2013-01-14
5
447 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 36
How can I do this in Pyhton? 12 66
get radio button vale in array 7 35
mysql Encryption with PHP 8 45
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
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 look for a specific file type in a local or remote server directory using PHP.

744 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

17 Experts available now in Live!

Get 1:1 Help Now