Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
181 views
Welcome To Ask or Share your Answers For Others

1 Answer

$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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...