【问题标题】:PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty inPHP FTP上传错误:警告:ftp_put()[function.ftp-put]:文件名不能为空
【发布时间】:2012-10-11 13:26:48
【问题描述】:

我在使用 PHP 完成我的 FTP 文件上传器时遇到了一些问题。我正在使用此处找到的 php.net 示例:http://php.net/manual/en/ftp.examples-basic.php 并稍微修改了代码。

<?php
//Start session();
session_start();

// Checking the users logged in
require_once('config.php');

//Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }

    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }

$ftp_server="*";

$ftp_user_name="*";

$ftp_user_pass="*";

$paths="members/userUploads";

$name=$_FILES['userfile']['name'];

$source_file=$_FILES['userfile']['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); 

// 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, $paths.'/'.$name, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
    echo "FTP upload has failed!";
} else {
    $CurrentUser = $_SESSION['CurrentUser'];
    $qry = "SELECT * FROM members WHERE username='$CurrentUser'";
    $result = mysql_query($qry);
    $result = mysql_fetch_array($result);
    $CurrentUser = $result[memberID];
    $qry = "INSERT into uploads (UploadPath, UploadUser) VALUES('$file_name', '$CurrentUser')";
    echo "Uploaded $source_file to $ftp_server as $paths.'/'.$name";
}

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

?>

但是,该代码适用于某些文件,但不适用于其他文件。当它不起作用时,它会给出错误:

警告:ftp_put() [function.ftp-put]: Filename cannot be empty in ... 第 48 行。

【问题讨论】:

  • 对于哪些文件这不起作用?检查$_FILES['userfile']['name']$_FILES['userfile']['tmp_name'] 是否始终有一个值。

标签: php file upload ftp


【解决方案1】:

如果在发送文件时出错,name 可能不会被设置。这会导致您尝试上传到members/userUploads/,这会导致ftp_upload 正确地抱怨文件名为空。

错误的一个常见原因是超出了允许的最大文件大小。至少,在尝试 FTP 上传之前,检查 $_FILES 条目中的 error 以获取您的文件:

if ($_FILES['userfile']['error'] != UPLOAD_ERR_OK) {
   // handle the error instead of uploading, e.g. give a message to the user
}

您可以在 PHP 手册中找到description of the possible error codes

【讨论】:

    【解决方案2】:

    这可能来自 $name 或 $source_file 为空白,也许文件上传有时会失败并导致问题。您可以尝试在某处使用 if 来确保文件已上传,例如:

    if (empty($name)) {
        die('Please upload a file');
    }
    

    请注意,除非您在字符串中使用变量,例如:

    echo "Connected to $ftp_server, for user $ftp_user_name";
    

    最好使用单引号。从技术上讲,它比双引号更快,因为它不扫描字符串中的变量。另外我觉得它看起来更整洁! :)

    【讨论】:

      【解决方案3】:

      第 48 行需要更改,注意双引号和 .'/' 的消除。根据您尝试上传的文件的名称,您可能会转义部分名称。

      $upload = ftp_put($conn_id, "$paths/$name", $source_file, FTP_BINARY); 
      

      【讨论】:

        猜你喜欢
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-21
        • 2014-01-31
        • 1970-01-01
        • 2013-07-12
        相关资源
        最近更新 更多