文件上传代码给出了此错误 - 警告:ftp_put():文件名不能为空

时间:2014-08-22 15:49:19

标签: php html ftp

我正在尝试编写允许我将图像上传到ftp服务器的代码,但由于某种原因,它会抛出错误“警告:ftp_put():文件名不能为空”。我环顾四周,而且找不到任何解决方案,请帮忙!

这是我的表单代码:

<form id="edit-cd" method="post" enctype="multipart/form-data">
    <input type="file" name="headliner-logo" />
    <input type="submit" name="submit-headlogo" class="submit-btn" value="Submit" />
</form>
<?php
if (isset($_POST['submit-headlogo'])) {
    if ($_FILES['headliner-logo']['size'] <= 0) {
        error("No file was chosen to upload");
    } else if ($_FILES['headliner-logo']['size'] > 1572864) {
        error("File too large, max. filesize is 1.5mb");
    } else if ($_FILES['headliner-logo']['type'] != "image/png") {
        error("Invalid filetype, .png only");
    } else {
        setHeadlinerLogo($_FILES['headliner-logo']['tmp-name'], $_FILES['headliner-logo']['name']);
    }
}
?>

以下是执行上传的代码:

function setHeadlinerLogo($source, $name) {
    $server = "ftp.server.com";
    $user = "username";
    $pass = "password";
    $dest = "/public_html/directory/img/";

    // Set up connection
    $conn = ftp_connect($server);
    if ($conn) {
        // Login
        $login = ftp_login($conn, $user, $pass);
        if($login) {
            // Upload File
            $upload = ftp_put($conn, $dest.$name, $source, FTP_ASCII);
            if ($upload) {
                success("File successfully uploaded");
            } else {
                error("Something went wrong, contact the admin");
            }
        } else {
        error("Could not log in to server");
        }
    } else {
        error("Could not connect to file server");
    }

    ftp_close($conn);
}

1 个答案:

答案 0 :(得分:0)

对不起大家,这是一个菜鸟的错误。这应该是$ _FILES [&#39; headliner-logo&#39;] [&#39; tmp_name&#39;]而不是$ _FILES [&#39; headliner-logo&#39;] [&# 39; TMP-名称&#39;!]