Question

I have an FTP server, but don't know the command to upload from a PHP form. I need a command to upload with WinSCP. My code so far is below:

<html>
<body>

<?php


if(isset($_FILES["uploaded"]))
{
print_r($_FILES);
if(move_uploaded_file($_FILES["uploaded"]["tmp_name"],"<root>/domains/sigaindia.com/public_html/reymend/".$_FILES["uploaded"]["name"])) 
echo "FILE UPLOADED!";
}
else
{
 print "<form enctype='multipart/form-data' action='fup1.php' method='POST'>";
 print "File:<input name='uploaded' type='file'/><input type='submit' value='Upload'/>";
 print "</form>";
}

?>

</body>
</html>
Was it helpful?

Solution

$host = "ftp.example.com";
$user = "anonymous";
$pass = "";

// You get this from the form, so you don't need to do move_uploaded_file()
$fname = "/public_html/new_file.txt";
$fcont = "content";

function ftp_writeFile($ftp, $new_file, $content, $debug=false) {
    extract((array)pathinfo($new_file));

    if (!@ftp_chdir($ftp, $dirname)) {
        return false;
    }

    $temp = tmpfile();
    fwrite($temp, $fcont);
    rewind($temp);  

    $res = @ftp_fput($ftp, $basename, $temp, FTP_BINARY);
    if ($debug) echo "a- '$new_file'".(($res)?'':" [error]")."<br/>";
    fclose($temp);

    return $res;
}

$ftp = ftp_connect($host);
if (!$ftp) echo "Could not connect to '$host'<br/>";

if ($ftp && @ftp_login($ftp, $username, $password)) {
    ftp_writeFile($ftp, $fname, $fcont, true);
} else {
    echo "Unable to login as '$username:".str_repeat('*', strlen($password))."'<br/>";
}

ftp_close($ftp);

http://au.php.net/manual/en/book.ftp.php

OTHER TIPS

I have an FTP server, but don't know the command to upload from a PHP form. I need a command to upload with WinSCP. My code so far is below:

If you're talking about PHP and forms than that's HTTP - HTTP is not FTP and vice versa.

WinSCP is an SSH client. SSH is a different protocol from HTTP. SSH is a different protocol from FTP.

If you want your PHP script to transfer files uploaded to the webserver to an FTP server, try something like:

foreach ($_FILES as $f) {
    if (file_exists($f['tmp_name'])) {
       $dest = 'ftp://' . $username . ':' . $password .
               '@' . $ftpserver . $ftp_path;
       file_put_contents($f['tmp_name'], $dest);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top