BLOB, SQL, and FTP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Melarlo
Forum Newbie
Posts: 3
Joined: Sun Apr 09, 2006 1:59 pm

BLOB, SQL, and FTP

Post by Melarlo »

Hello,

I am new here and this may be an easy question... not sure. I did search but didn't find anything on this. Basically here is the situation:

We are currently working on a program in mainly PHP and MySQL. We currently have a working section of the program that allows for Uploading of .jpg, .eps, and .swf files into BLOB storage in a MySQL database. We also have working preview pages of these files.

The problem lies in this:
We have a need to be able to pull multiple files directly from the BLOB status and FTP them to another site for conventional storage (I.E. in folders on that site). This will all be done with no updating or previewing of the data, just sending (up to 100 files at a time potentially).

Has anyone out there ever tried this, and if so, could you please point me in the right direction?

Any knowledge or help is greatly appreciated.

Thank you! :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you can create a stream compatible interface to the file(s), you may be able to use ftp_fput(), otherwise you'll need to write the blob to a real filesystem file in order to use ftp_put()
Melarlo
Forum Newbie
Posts: 3
Joined: Sun Apr 09, 2006 1:59 pm

Post by Melarlo »

Thanks for the quick reply!:)

So, i could write the affected BLOB(s) to a conventional file location, ftp_put() them, and then delete the files. Interesting idea, i'll have to give that a shot.

Thanks again!
Melarlo
Forum Newbie
Posts: 3
Joined: Sun Apr 09, 2006 1:59 pm

Post by Melarlo »

Thanks again for the info there. Just in case anyone runs into this later, here is some sloppy code (that will be refined) that works for me:

Code: Select all

if(isset($_GET['id'])) 
{ 
include 'config.php'; 
include 'opendb.php'; 
$id = $_GET['id']; 
$query = "SELECT name, type, size, content FROM files WHERE id = '$id'"; 
$result = mysql_query($query) or die('Error, query failed'); 
list($name, $type, $size, $content) = mysql_fetch_array($result);
$myDir = "tempfile/";
$myFile = $name;
$fh = fopen($myDir.$myFile,'w') or die("can't open file");
$data = $content;
fwrite($fh,$data);
fclose($fh);
$ftp_server = "ADDRESS";
$ftp_user_name = "USERNAME";
$ftp_user_pass = "PASSWORD";
$conn_id = ftp_connect($ftp_server);
$destination_file = $name;
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
if ((!$conn_id) || (!$login_result)) { 
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
       exit; 
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name";
   }
// upload the file
$upload = ftp_put($conn_id, $myFile, $myDir.$myFile, FTP_BINARY);
// check upload status
if (!$upload) { 
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $source_file to $ftp_server as $destination_file";
       unlink($myFile);
   }
// close the FTP stream 
ftp_close($conn_id);
include 'closedb.php'; 
exit;
Hope that helps someone later.

:P
Post Reply