【问题标题】:Upload file using PHP and FTP, where is my error?使用 PHP 和 FTP 上传文件,我的错误在哪里?
【发布时间】: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) ,这实际上断开了连接。很抱歉打扰并感谢您寻求帮助。杰夫

标签: php ftp


【解决方案1】:
$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);
}

【讨论】:

  • 连接到ftp.rf.gd

    警告:ftp_put() [function.ftp-put]:您没有登录 C:\Apache24\htdocs\newupload.php29

    致命错误:未捕获的异常:ftp_put() [<a href='/phpmanual/function.ftp-put'>function.ftp-put</a>]:您没有登录 C:\Apache24\htdocs \newupload.php:35 堆栈跟踪:#0 {main} 在 35
    行的 C:\Apache24\htdocs\newupload.php 中抛出
  • 基本错误:“您未登录” 显然我已经登录到 FTP 服务器,您可以在输出中看到“已连接到 ftp.rf.gd。我还需要登录到我的本地 apache 服务器以运行调用我的 php 脚本的 html 文件?在网上找不到任何关于此的内容。谢谢,Jeff
猜你喜欢
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多