Page 1 of 1

upload to mysql AND ftp dir

Posted: Sat Oct 21, 2006 6:11 pm
by jfilan
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi,

i am trying to upload to my database and also to a ftp directory, so that when i call up the file dynamically it will grab it from the ftp server. problem is i don't know how to do both on the same upload. i am currently using a simple uploader like so:

Code: Select all

<? 
if(isset($_POST['upload'])) 
{ 
        $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); 
        $content = addslashes($content); 
        fclose($fp); 
         
        if(!get_magic_quotes_gpc()) 
        { 
            $fileName = addslashes($fileName); 
        } 
         

        include 'library/config.php'; 
        include 'library/opendb.php'; 
         
        $query = "INSERT INTO upload (name, size, type, content ) ". 
                 "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; 

        mysql_query($query) or die('Error, query failed');                     
        include 'library/closedb.php'; 
         
        echo "<br>File $fileName uploaded<br>"; 
}         
?>
Any help it much appreciated.

thanks,

josh


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Oct 22, 2006 3:51 pm
by amir
Please try this,

Code: Select all

<?php
//Upload
$conn_id = ftp_connect('ftp.example.com');
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'pass');
// check connection
if ((!$conn_id) || (!$login_result)) {
     echo "FTP connection has failed!";
     echo "Attempted to connect to $ftp_server";
     exit;
} else {
     echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, 'remote_file', 'file', FTP_BINARY);

// check upload status
if (!$upload) {
     echo "FTP upload has failed!";
} else {
     echo "Uploaded";
}

// close the FTP stream
ftp_close($conn_id);
?>

Posted: Sun Oct 22, 2006 6:08 pm
by jfilan
Thank you! How can I determine the directory I want to upload the file to?

Posted: Sun Oct 22, 2006 6:23 pm
by John Cartwright
you specificy the file paths in ftp_put()