Solved

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

Posted on 2013-01-14
5
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

730 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