【问题标题】:FTP via PHP Error通过 PHP 的 FTP 错误
【发布时间】:2016-09-21 14:55:54
【问题描述】:

我在通过 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);

我收到的信息是:

"连接到ftp.theucl.co.uk,对于用户theucl.co.ukFTP上传有 失败!”

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);

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

【问题讨论】:

  • 那些不是你的真实凭证……是吗?
  • 检查文件夹权限。我猜当前权限设置为755。设置为777
  • 我建议不要将权限设置为 777。这是一个安全风险。虽然检查文件夹权限是否正确是有效的。
  • 您好,请阅读我的回答,如果您需要任何其他信息,请尽快联系我。
  • @JonStirling 不,他们不是。其次,我的主机在控制面板上似乎没有用于文件夹检查的选项。

标签: php html upload ftp web


【解决方案1】:

通过使用 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";
        }
    }

?>

这是一种享受

【讨论】:

    【解决方案2】:

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

    首先在您的 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 port 21。在 ftp_put() 中使用第 4 个变量作为 FTP_ASCII 或 FTP_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);
    

    我看了很多文章,发现username和password应该只包含alpha numeric。我还尝试将 $ftp_server 作为您的 localhost。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多