2012-10-11 1 views
0

PHP를 사용하여 FTP 파일 업 로더를 마무리하는 데 문제가 있습니다. 나는 여기에서 찾아낸 php.net에서 예제를 사용하고있다 : http://php.net/manual/en/ftp.examples-basic.php 그리고 약간 코드를 수정했다.PHP FTP 업로드 오류 : 경고 : ftp_put() [function.ftp-put] : 파일 이름을 비워 둘 수 없습니다.

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

?>

그러나 코드가 일부 파일이 아닌 다른 사람을 위해 작동합니다. 그것이 작동하지 않는 경우 그 오류를 제공합니다

에러가 발생했을 경우 파일을 보낼 때 name가 설정되지 않을 수 있습니다

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

+0

어떤 파일이 작동하지 않습니까? '$ _FILES [ 'userfile'] [ 'name']','$ _FILES [ 'userfile'] [ 'tmp_name']'항상 값을 가지고 있는지 확인하십시오. – pankar

답변

1

. 이로 인해 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을 찾을 수 있습니다.

0

$ name 또는 $ source_file이 비어있을 가능성이 높습니다. 아마도 파일 업로드가 실패하여 문제를 일으키는 경우 일 수 있습니다. 다음을 사용하여 시도 할 수있는 곳하다면 만들기 위해 반드시 파일 같은 업로드 : 당신 같은 문자열에 변수를 사용하지 않는 그냥주의하는 것이

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

:

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

의 사용하는 것이 바람직 작은 따옴표. 기술적으로는 문자열을 변수로 검색하지 않기 때문에 큰 따옴표가 빠릅니다. 게다가 나는 그것이 더 깔끔하게 보인다라고 생각한다! :)

0

라인 48을 변경해야합니다. 큰 따옴표와 '/'를 제거하십시오. 업로드하려는 파일의 이름에 따라 파일 이름의 일부를 이스케이프 처리 중일 수 있습니다.

$upload = ftp_put($conn_id, "$paths/$name", $source_file, FTP_BINARY); 
관련 문제