将图像上传到FTP

时间:2016-01-31 09:21:15

标签: php image ftp

从我的Swift应用程序中,我将图像文件发送到我的PHP脚本,并从我的PHP脚本中将其上传到我的FTP。

这是我的剧本

<?php

$ftp_server = "My FTP";
$ftp_user = "Username";
$ftp_pass = "Password";
$file = $_FILES["file"]["tmp_name"];
$remote_file = "test.png"; 

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";
} else {
    echo "Couldn't connect as $ftp_user\n";
}

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

// close the connection
ftp_close($conn_id);  
?>

我正在连接到服务器并且我能够上传文件但是我的问题是我上传的文件已损坏。我认为这与我的$remote_file = "test.png";有关。这是使用ftp_put上传文件的正确方法吗?是否有人知道它为什么会损坏?

1 个答案:

答案 0 :(得分:0)

<?php

$ftp_server = "My FTP";
$ftp_user = "Username";
$ftp_pass = "Password";
$file = $_FILES["file"]["tmp_name"];
$remote_file = "test.png"; 

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";
} else {
    echo "Couldn't connect as $ftp_user\n";
}

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

// close the connection
ftp_close($conn_id);  
?>