如何在另一个php网站上使用外部ftp

时间:2015-11-04 16:00:21

标签: php ftp

如何使用以下php代码实现外部ftp帐户存储上传的数据....?

ps:目前我有无限的带宽但我的网站主机存储容量有限,但我在另一个网站上有无限的ftp帐户,所以请解释我如何在我的下面代码中使用该ftp帐户。

<html>

<div align="center">
<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php

// maximum execution time in seconds
set_time_limit (24 * 60 * 60);

if (!isset($_POST['submit'])) die();


// folder to save downloaded files to. must end with slash
$destination_folder = 'downloads/';

$url = $_POST['url'];
$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
  $newf = fopen ($newfname, "wb");

  if ($newf)
  while(!feof($file)) {
    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
  }
}

if ($file) {
  fclose($file);
}

if ($newf) {
  fclose($newf);
}

?>
</div>

</html>

2 个答案:

答案 0 :(得分:0)

您可以在ftp_fput function上的PHP网站上看到您想要的示例。

答案 1 :(得分:0)

PHP FTP

<html>

<div align="center">
<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php

// maximum execution time in seconds
set_time_limit (24 * 60 * 60);

if (!isset($_POST['submit'])) die();


// folder to save downloaded files to. must end with slash
$destination_folder = 'downloads/';

$url = $_POST['url'];
$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
    $newf = fopen ($newfname, "wb");

    if ($newf)
        while(!feof($file)) {
            fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
        }
}

if ($file) {
    fclose($file);
}

if ($newf) {
    fclose($newf);
}

$ftp_server="ftp.yourserver.com";
$ftp_user_name="username";
$ftp_user_pass="password";

$remote_file = "/path/to/remote_file";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// upload a file
if (ftp_put($conn_id, $remote_file, $newf, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);