upload to mysql AND ftp dir

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
jfilan
Forum Newbie
Posts: 3
Joined: Sat Oct 21, 2006 6:07 pm

upload to mysql AND ftp dir

Post 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]
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post 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);
?>
jfilan
Forum Newbie
Posts: 3
Joined: Sat Oct 21, 2006 6:07 pm

Post by jfilan »

Thank you! How can I determine the directory I want to upload the file to?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you specificy the file paths in ftp_put()
Post Reply