【发布时间】:2016-12-14 05:07:35
【问题描述】:
以下代码从 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");
?>
【问题讨论】:
-
你有权限发起传出流量吗?
-
我从浏览器地址 localhost/upload.html 运行,它要求输入文件并调用下面的 php 文件。相同的文件夹(htdocs)。我该怎么做才能检查是否允许外出?
-
糟糕,发现我的错误。连接后,我再次重新定义 $conn_id = ftp_connect($ftp_server) ,这实际上断开了连接。很抱歉打扰并感谢您寻求帮助。杰夫