繁体   English   中英

php中的ftp文件上传问题

[英]ftp file upload problems in php

我一直在谷歌上搜索我能找到的所有东西,但没有任何效果。 我开始变得绝望了。

我正在尝试创建一个可以将文件上传到我的ftp帐户的脚本,但到目前为止我还没能让它运行起来。

我收到这些错误:

警告:ftp_put():打开文件传输的数据通道。 在第49行的C:\\ xampp \\ htdocs \\ AA \\ dwsite \\ ftptest \\ up.php中

致命错误:第49行的C:\\ xampp \\ htdocs \\ AA \\ dwsite \\ ftptest \\ up.php超出了30秒的最大执行时间

这是我正在使用的代码:

<?php
if (isset ( $_FILES ['be_file'] )) {

    $file_size = $_FILES ['be_file'] ['be_file'];
    $file_type = $_FILES ['be_file'] ['type'];
    $source_file = $_FILES ['be_file'] ['tmp_name'];
    $destination_file = $_FILES ['be_file'] ['name'];
//ftp details   
    $ftp_server = 'ip of ftp';
    $ftp_port = 'port number';
    $ftp_user_name = 'username';
    $ftp_user_pass = 'pass';

    // set up basic connection
    $conn_id = ftp_connect ( $ftp_server, $ftp_port );
    ftp_pasv ( $conn_id, true );
    // login with username and password

    $login_result = ftp_login ( $conn_id, $ftp_user_name, $ftp_user_pass );

    // upload a file
    if (ftp_put ( $conn_id, $destination_file, $source_file, FTP_BINARY )) {
        echo "successfully uploaded $source_file\n";
        exit ();
    } else {
        echo "There was a problem while uploading $source_file\n";
        exit ();
    }
    // close the connection
    ftp_close ( $conn_id );
    echo "Success";
}

?>
<html>
<body>

    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="file" /> <input type="submit" />

        <ul>
            <li>Sent file: <?php echo $_FILES['be_file']['name'];  ?>

            <li>File size: <?php echo $_FILES['be_file']['size'];  ?>

            <li>File type: <?php echo $_FILES['be_file']['type']?>

        </ul>

    </form>

</body>
</html>

max_execution_time整数

  • max_execution_time = 300添加到php.ini文件服务器中。 然后重新启动php-fpm服务并重新启动服务器。
  • 或者添加set_time_limit(0); 在你的代码中。
  • 或者,如果您无法访问php.ini文件,则可以延长最长执行时间,如下所示: ini_set('max_execution_time', 300); //300 seconds = 5 minutes ini_set('max_execution_time', 300); //300 seconds = 5 minutes

这将设置允许脚本在解析器终止之前运行的最长时间(以秒为单位)。 这有助于防止编写不良的脚本占用服务器。 默认设置为30.从命令行运行PHP时,默认设置为0。

最大执行时间不受系统调用,流操作等的影响。有关更多详细信息,请参阅set_time_limit()函数。

在安全模式下运行时,无法使用ini_set()更改此设置。 唯一的解决方法是关闭安全模式或更改php.ini中的时间限制。

Php doc - > Max_execution_time

我认为你有这个错误,因为你在登录前调用了ftp_pasv函数:

ftp_pasv ( $conn_id, true );
$login_result = ftp_login ( $conn_id, $ftp_user_name, $ftp_user_pass );

应该:

$login_result = ftp_login ( $conn_id, $ftp_user_name, $ftp_user_pass );
ftp_pasv ( $conn_id, true );

如果它可以帮助某人。 ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM