PHP FTP上传错误:警告:ftp_put()[function.ftp-put]:文件名不能为空 [英] PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in

查看:350
本文介绍了PHP FTP上传错误:警告:ftp_put()[function.ftp-put]:文件名不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < ?php 
//开始会话();
session_start();

//检查用户登录
require_once('config.php');

//连接到mysql服务器
$ link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$ link){
die('无法连接到服务器:'。mysql_error());
}

//选择数据库
$ db = mysql_select_db(DB_DATABASE);
if(!$ db){
die(无法选择数据库);
}

$ ftp_server =*;

$ ftp_user_name =*;

$ ftp_user_pass =*;

$ paths =members / userUploads;

$ name = $ _ FILES ['userfile'] ['name'];

$ source_file = $ _ FILES ['userfile'] ['tmp_name'];

//设置基本连接
$ conn_id = ftp_connect($ ftp_server);

//使用用户名和密码登录
$ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass);

检查连接
if((!!conn_id)||(!$ login_result)){
echoFTP连接失败!
echo尝试连接到用户$ ftp_user_name的$ ftp_server;
出口;
} else {
echo连接到$ ftp_server,用户$ ftp_user_name;
}

//上传文件
$ upload = ftp_put($ conn_id,$ paths。'/'。$ name,$ source_file,FTP_BINARY);

//检查上传状态
if(!$ upload){
echoFTP 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 =插入uploads(UploadPath,UploadUser)VALUES('$ file_name','$ CurrentUser');
echo上传$ source_file到$ ftp_server作为$ paths。'/'。$ name;
}

//关闭FTP流
ftp_close($ conn_id);

?>



然而,代码将适用于某些文件但不适用于其他文件。警告:ftp_put()[function.ftp-put]:文件名不能为空。如果它不起作用,则会显示错误信息:


...


解决方案

name 可能不会在发送文件时发生错误。这会导致您尝试上传到 members / userUploads / ,这会导致 ftp_upload 正确地抱怨空文件名。



错误的常见原因是超过了允许的最大文件大小。至少,在尝试FTP上传之前,检查文件的 $ _ FILES 条目中的错误: p>

  if($ _FILES ['userfile'] ['error']!= UPLOAD_ERR_OK){
//处理错误而不是上传,例如给用户一个消息
}

你可以找到 PHP手册中可能的错误代码描述


I'm having some trouble finishing my FTP file uploader using PHP. I'm using the example from php.net found here: http://php.net/manual/en/ftp.examples-basic.php and have modified the code slightly.

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

?>

However, the code will work for some files but not others. When it doesn't work it gives the error:

Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in ... on line 48.

解决方案

The name might not be set if there was an error when sending the file. This would cause you to try to upload to members/userUploads/, which would cause ftp_upload to rightly complain about an empty filename.

A common reason for the error is exceeding the maximum allowed filesize. At the very least, check the error in the $_FILES entry for your file before attempting the FTP upload:

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

You can find the description of the possible error codes in the PHP manual.

这篇关于PHP FTP上传错误:警告:ftp_put()[function.ftp-put]:文件名不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆