PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in

拥有回忆 提交于 2020-01-06 15:47:20

问题


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.


回答1:


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.




回答2:


That probably comes from $name or $source_file being blank, perhaps the file upload is sometimes failing and causing the problem. You could try using an if somewhere to make sure a file was uploaded such as:

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

Just to note, unless you are using variables in the string such as:

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

its preferable to use single quotes. Technically its faster that double quotes as it doesn't scan the string for variables. Plus I think it looks neater! :)




回答3:


line 48 needs to be changed, note the double quotes and the elimination of .'/'. Depending on the name of the file you are trying to upload, you may be escaping part of it's name.

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


来源:https://stackoverflow.com/questions/12840636/php-ftp-upload-error-warning-ftp-put-function-ftp-put-filename-cannot-be

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!