【问题标题】:PHP File Upload to FTP (Here Using Uploadify as FTP) -- Unable to perform uploadPHP 文件上传到 FTP(此处使用 Uploadify 作为 FTP)- 无法执行上传
【发布时间】:2022-06-17 22:17:07
【问题描述】:
Here is my HTML Code :

<html>
<head>
<title>Welcome</title>
</head>

<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile_1" type="file" /><br />
Choose a file to upload: <input name="uploadedfile_2" type="file" /><br />
<input type="submit" value="Upload Files" />
</form>
</body>
</html>

下面是PHP:

<?php
$ftp_server = "94.xx.1.xxx";
$ftp_username   = "anxxxxxx";
$ftp_password   =  "xxxxxxxxx";

$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");

if(@ftp_login($conn_id, $ftp_username, $ftp_password))
{
  echo "connected as $ftp_username@$ftp_server\n";
  }
else {
  echo "could not connect as $ftp_username\n";
}

$file = $_FILES["uploadedfile_1"]["name"];
$file2 = $_FILES["uploadedfile_2"]["name"];

$remote_file_path = "ansxxxx@94.xx.1.xxx/JustForTest".$file; // This is the Folder which I've created inside the FTP 
$remote_file_path2 = "ansxxxx@94.xx.1.xxx/JustForTest".$file2; // This is the Folder which I've created inside the FTP 

ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile_1"]["tmp_name"],FTP_ASCII);
ftp_put($conn_id, $remote_file_path2, $_FILES["uploadedfile_2"]["tmp_name"],FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>

错误:

连接为 anshxxx@94.xx.1.xxx 致命错误:未捕获的 ValueError:C:\xampp\htdocs\upload.php:22 中的路径不能为空 堆栈跟踪:#0 C:\xampp\htdocs\upload.php(22): ftp_put(Object(FTP\Connection) , 'anshxxx@94.xx...', '', 1) #1 {main} 在第 22 行的 C:\xampp\htdocs\upload.php 中抛出

它连接完美...但没有文件上传,抛出上述错误。我是 php 新手。请帮忙...!

如果有人做过这种要求,我更愿意分享代码。

非常感谢...!`

【问题讨论】:

  • 做一些调试,比如检查$_FILES["uploadedfile_1"]["tmp_name"] 和$_FILES["uploadedfile_2"]["tmp_name"] 实际包含的内容。做一个var_dump($_FILES) 并检查。如果它们为空,请检查error 是否为0 以外的任何内容(这表示上传错误,例如文件太大或其他问题)。永远不要假设两个文件都已成功上传,始终验证和验证您获得的数据。
  • 为什么在上传文件时使用FTP_ASCII而不是默认的FTP_BINARY?会一直是文本文件吗?
  • 试过没用。
  • 我相信您的远程文件路径有问题。您不应该在路径中使用伪目录名称,例如 username@server.ip 等。您必须确定 FTP 服务器上是否允许使用此类目录名称。还有一个问题,你确定你测试的时候上传2个文件吗?如果您只上传一个文件,则出现此错误是正常的。因为第二个文件的上传路径为空。
  • 我发布了一个解决方案,如果是这种情况,请尝试并发布任何错误消息。

标签: php html xampp connection uploadify


【解决方案1】:

试试下面的代码。请在代码中重新定义您的上传目录。我对您的问题写了评论,您应该使用安全的目录名称。我不会使用 @ 和 .在目录名称中。尤其是没有点。

<?php

// Build FTP connection and login
function getFtpConnection()
{
    $ftp_server = "94.xx.1.xxx";
    $ftp_username   = "anxxxxxx";
    $ftp_password   =  "xxxxxxxxx";
    
    $conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
    
    if(@ftp_login($conn_id, $ftp_username, $ftp_password))
    {
      echo "connected as $ftp_username@$ftp_server\n";
      return $conn_id;
    }
    else {
      echo "could not connect as $ftp_username\n";
      return false;
    }
}

$messages = [];
if(gettype($conn) == "resource")
{
    $remoteDirectory = "/html/myuploadDir/";//Absoulte path
    foreach($_FILES as $k => $upload_file)
    {
        if(!empty($upload_file["name"]) && filesize($upload_file["tmp_name"]) > 0)
        {
            $fname = $upload_file["name"];
            $remote_filepath = $remoteDirectory . $fname;
            if(ftp_put($conn, $remote_filepath, $upload_file["tmp_name"]))
                $messages[] = "File $fname uploaded successfully";
            else
                $messages[] = "Upload for file $fname failed";
        }
    }
    ftp_close($conn);
    $messages[] = "Connection closed";
}
echo implode("<br>", $messages);

【讨论】:

  • 嗨,非常感谢上面的代码。我已将远程目录定义为“/JustForTest
  • 另外我一次上传 2 个文件@Selim Acar
  • @DivyanshSharma $remoteDirectory = "/JustForTest" 可能是错误的。因为在这种情况下,JustForTest 目录必须位于 FTP 服务器的 ROOT 目录中。请尝试不使用 remoteDirectory:$remote_filepath = $upload_file["name"]。如果上传成功,则在保存文件的 FTP 服务器上搜索文件。根据路径,您可以将目标目录定义为绝对路径或相对路径。
  • 我已更改以下代码。 //$remote_filepath = "$remoteDirectory/" . $upload_file["名称"]; $remote_filepath = $upload_file["name"]; ftp_put($conn, $remote_filepath, $upload_file["tmp_name"]); } } ftp_close($conn); echo "\n\n连接关闭"; } ?> 结果:连接为 axxxxxx@91.xx.x.xxx 连接关闭。 *没有上传发生。
  • @DivyanshSharma 我在上面更新了我的解决方案。它不包含巨大的变化。请定义您的远程目录路径。注意远程上传目录路径应该是绝对路径,并且该目录应该有适当的访问权限或权限。应该允许对该文件夹进行写访问。我用我自己的 FTP 帐户(远程目录/html)测试了这段代码,它工作正常。请确保您的防火墙也不会阻止您的连接。
【解决方案2】:

我是 PHP 新手,我需要以下代码的帮助。下面的代码工作正常,但我需要在下面的代码中添加一个“要添加的功能,这样如果上传了任何新文件,它应该在一系列中自动递增。例如:00004001、00004002、00004003 ....等等。

请帮助...!以下是现有代码。目前它会将确切的文件名上传到远程服务器。

<?php
if ( empty( $_FILES['file'] ) ) {
    return;
}
$ftp_server = "xx.xx.x.xxxx";
$ftp_user_name = "xxxxxxx";
$ftp_user_pass = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
$destination_file = "";
$source_file = $_FILES['file']['tmp_name'];

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


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

//ftp_pasv($conn_id, true); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
}   else {
    
    
    echo "<p align=center> Your Image has been Uploaded if you dont't see any Error Msg Below</p> ";
}

// upload the file

$destination_folder = "/";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
//ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 


echo "<p align=center> **File Upload Has Failed &#128558</p> ";

} else {
echo "";
}

// close the FTP stream 
ftp_close($conn_id);
?>

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多