FTP 通过 PHP 错误

FTP via PHP Error

我在通过 PHP 从我的网站上传到 FTP 时遇到问题。

连接正常,但上传图片时出现问题。

HTML

<form action="../scripts/php/saveupload.php" method="post">
    <input name="file" type="file" />
    <input name="submit" type="submit" value="Upload File" />
</form>

PHP

$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXX";
$ftp_user_pass = "XXXXX";
$destination_file = "/public_html/img/news/";
$source_file = $_FILE['file']['tmp_name'];

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

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

//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 "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file); 

// check upload status
if (!$upload) { 
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

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

我收到的消息是:

"Connected to ftp.theucl.co.uk, for user theucl.co.ukFTP upload has failed!"

This is now the error I am getting after following Niths advice on the error...

看来错误是围绕以下..

$source_file = $_FILES['file']['tmp_name'];

$upload = ftp_put($conn_id, $destination_file.$_FILES['file']['tmp_name'], $source_file,FTP_ASCII);

显然这之间有一个共同的表现

您的代码中几乎没有更正。检查上传文件的目标文件夹权限。确认该文件夹具有可写权限。 **如果上传失败,请检查错误处理程序并进行必要的更改,例如创建上传目标文件夹、分配文件权限等**

首先在您的 html 表单中添加 enctype 类型。

HTML

<form action="../scripts/php/saveupload.php" method="post" enctype="multipart/form-data">
     <input name="file" type="file" />
     <input name="submit" type="submit" value="Upload File" />
</form>

然后在php文件中一共修改了3处。我在下面打印了这些行。只需将这三个更改与您的代码进行比较即可。在您的程序中, $source_file 表示 $_FILE 未定义。而且,我在 ftp_connect() 函数中使用了 ftp 端口 21。在 ftp_put() 中使用第 4 个变量作为 FTP_ASCIIFTP_BINARY

$source_file = $_FILES['file']['tmp_name'];
$conn_id = ftp_connect($ftp_server,21);
$upload = ftp_put($conn_id, $destination_file.$_FILES['file']['name'], $source_file,FTP_ASCII); 

完整节目

PHP

    ini_set("display_errors", "1");
    error_reporting(E_ALL);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    print_r(error_get_last());

    $ftp_server = "XXXXXX";
    $ftp_user_name = "XXXXX";
    $ftp_user_pass = "XXXXX";
    $destination_file = "/public_html/img/news/";
    $source_file = $_FILES['file']['tmp_name'];

    // set up basic connection
    $conn_id = ftp_connect($ftp_server,21);
    ftp_pasv($conn_id, true); 

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

    // 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 "Connected to $ftp_server, for user $ftp_user_name";
    }

    // upload the file 
    $upload = ftp_put($conn_id, $destination_file.$_FILES['file']['name'], $source_file,FTP_ASCII); 

    // check upload status
    if (!$upload) { 
    echo "FTP upload has failed!";
    } else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

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

看了很多文章,发现usernamepassword应该只包含alpha numeric .我也试过 $ftp_server 作为你的 localhost

因此,要检查该程序,请尝试在任何内部服务器(一个用于 运行 php 文件和同一服务器用于 ftp 文件传输)。我知道没有使用与 FTP 相同的服务器进行文件传输。但是您请尝试在同一台服务器上上传文件。如果 FTP 进程正常,那么我们将尝试更改 ftp 服务器。

通过使用 Thorn Web 在 Youtube 上的教程解决了它...我没有改变我上面的内容,而是重新启动它,现在有以下内容:

<?php

    if($_POST['submit']){

        $name = $_FILES['upload']['name'];
        $temp = $_FILES['upload']['tmp_name'];
        $type = $_FILES['upload']['type'];
        $size = $_FILES['upload']['size'];

        if(($type == "image/jpeg") || ($type == "image/jpg") || ($type == "image/gif")){

                if($size <= 1000000){

                    move_uploaded_file($temp, "../img/news/$name");


                }else{

                    echo "The file: '<b>$name</b>' is too big...<br>
                    The size is <b>$size</b> and needs to be less than 100GB. ";
                }       
        }else{
            echo "This type '<b>$type</b>' is not allowed";
        }
    }

?>

这很管用