使用PHP和FTP上传文件,我的错误在哪里?

时间:2016-12-14 05:07:35

标签: php ftp

下面的代码从html表单获取文件并将临时文件保存到本地临时文件夹但无法上传到服务器。服务器将从ftp客户端上传,但下面的PHP代码返回"无法上传"。 $ destDir有问题吗?目录存在于服务器上...任何帮助表示赞赏。

<?php
// set up basic connection
$ftp_server = "ftp.rf.gd";
$ftp_user_name = "rfgd_19026557";
$conn_id = ftp_connect($ftp_server);
$pass="fakepass";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $pass);
//test if the connection is successful
echo '<center>';
$conn_id = ftp_connect($ftp_server);
if (!$conn_id)
    echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">
    Couldn\'t connect to <b>' . $ftp_server . '</b></div>';
else
    echo '<div style="background-color:green;padding:10px;color:#fff;font-size:16px;">
    Connected to <b>' . $ftp_server . '</b></div>';
ftp_pasv($conn_id, true);  // turns on passive mode
// upload a file
$destDir = "Business/pics";
$workDir = "C:\Apache24\htdocs\Images"; // define this as per local system
// get temporary file name for the uploaded file
$tmpName = basename($_FILES["fileToUpload"]['tmp_name']);
// copy uploaded file into current directory
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $workDir."/".$tmpName) or die("Cannot move uploaded file to working directory");

//Code above this point works

// perform file upload
$upload = ftp_put($conn_id, $destDir."/".$_FILES['fileToUpload']['name'], $workDir."/".$tmpName, FTP_BINARY);
// check upload status
// display message
if (!$upload) {
    echo "Cannot upload";
} else {
    echo "Upload complete";
}
// close the connection
ftp_close($conn_id);
// delete local copy of uploaded file
//unlink($workDir."/".$tmpName) or die("Cannot delete uploaded file from working directory — manual deletion recommended");
?>

1 个答案:

答案 0 :(得分:2)

$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
if (!@ftp_put($conn_id, $destDir."/".$_FILES['fileToUpload']['name'], $workDir."/".$tmpName, FTP_BINARY)) {
   // error message is now in $php_errormsg
   $msg = $php_errormsg;
   ini_set('track_errors', $trackErrors);
   throw new Exception($msg);
}