警告:ftp_put():文件名无效,PHP FTP上传脚本

时间:2014-03-31 20:09:19

标签: php upload ftp

我尝试将文件上传到ftp服务器时出错了。我可以连接到我的FTP服务器,但是ftp_put();不断返回错误信息

这是我的html表单提交

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

这是我的upload_file.php脚本

<?php
$ftp_server = "xxx";
$ftp_user_name = "xxx";
$ftp_user_pass = "xxx";
$destination_file = "/Iklan_Output/". $_FILES["file"]["name"]; // returns /Iklan_Output/file.test
$source_file = $_FILES["file"]["tmp_name"]; //decoy
$new_src_file = "E:\\ta\\temp\\" . $_FILES["file"]["name"]; // directory to the file

// set up basic connection
$conn_id = ftp_connect($ftp_server,20,900);


// 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";
}

ftp_pasv($conn_id, true);


// moves file from temporary to specific folder, which is used for new source_file destination

if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"]. "<br>";

    if (file_exists("E:\\ta\\temp\\" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }

    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "E:\\ta\\temp\\" . $_FILES["file"]["name"]);
    echo "Temporarily Stored in: " . "E:\\ta\\temp\\" . $_FILES["file"]["name"]. "<br>";
    }

    }


// upload the file

$upload = ftp_put($conn_id, $destination_file, $new_src_file, FTP_BINARY); 


// 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_put():文件名无效错误,

谢谢!

更新已解决,代码正在运行,谢谢大家!

1 个答案:

答案 0 :(得分:0)

您的目标文件是"/Iklan_Output/",它不是有效的文件名,而是目录名(因为如果是斜杠)。尝试使用有效的文件名。