2012-11-24 2 views
0

오케이 그래서 브라우저에서 실행될 때 스크립트가 실행되어 내 데이터베이스에서 csv 파일을 만듭니다.대신 데이터베이스에서 csv 내보내기를 다운로드하고 ftp를 통해 서버에 파일을 작성하십시오.

하지만 내 서버에서 다른 서버로 파일을 게시하고 싶습니다.

나는이 코드로 해보려고했지만 작동하지 않는 것으로 기록 된 파일이 없습니다. FTP 계정 세부 정보가 잘못되어도 OK가 다시 돌아옵니다.

// header("Content-type: application/octet-stream"); 
// header("Content-Disposition: attachment; filename=sailings.txt"); 
// header("Pragma: no-cache"); 
// header("Expires: 0"); 
// print "$header\n$data"; 

//Connect to the FTP server 
$ftpstream = ftp_connect('ftp server address'); 

//Login to the FTP server 
$login = ftp_login($ftpstream, 'user', 'password'); 
if($login) { 
echo "logged in ok"; 
//We are now connected to FTP server. 
//Create a temporary file 
$temp = tmpfile(); 
fwrite($temp, $header."\n"); 
fwrite($temp, $data); 
fseek($temp, 0); 
echo fread($temp, 0); 

//Upload the temporary file to server 
ftp_fput($ftpstream, '/sailings.txt', $temp, FTP_ASCII); 

//Make the file writable only to owner 
ftp_site($ftpstream,"CHMOD 0644 /sailings.txt"); 
} 

//Ftp session end 
fclose($temp); 
ftp_close($ftpstream); 

제발 아무도 알려 줄 수 없나요?

감사

리치 :

+0

실제로 연결되어 있습니까? 계속하기 전에 $ ftpstream을 false로 테스트 해보십시오. –

답변

0

귀하의 문제를 TMPFILE 기능은 파일의 핸들을 반환하고 ftp_put 기능이 업로드 될 것입니다 파일의 경로 + 이름을 수신 할 필요가 있다는 것입니다 . 이러한 의미에서이 2 가지 명령어의 작동에는 불일치가 있습니다.

는이 문제를 해결하려면 다음과 같이 의견을 펠리페하는 감사 :

$f = tempnam("/tmp", "FOO"); 
$temp = = fopen($f, "w"); 
fwrite($temp, $header."\n"); 
fwrite($temp, $data); 
fseek($temp, 0); 
echo fread($temp, 0); 

//Upload the temporary file to server 
ftp_put($ftpstream, '/sailings.txt', $f, FTP_ASCII); 

// The rest is the same as you have 

이 솔루션 행운

편집을보십시오. ftp_fput을 올바르게 사용하고 있습니다. 그럼에도 불구하고 문제가 지속되고 여전히 갇혀 있다면 주어진 전략을 사용하여 어떤 일이 발생하는지 볼 수 있습니다.

+1

'ftp_fput (리소스 $ ftp_stream, 문자열 $ remote_file, 리소스 $ 핸들 **, int $ 모드 [, int $ startpos = 0])'PHP 5 매뉴얼 –

+0

@FelipeAlamedaA 소스는 무엇입니까? http://php.net/manual/en/function.ftp-put.php에서 읽었습니다 : bool ftp_put (리소스 $ ftp_stream, 문자열 $ remote_file, 문자열 $ local_file, int $ mode [, int $ startpos = 0]) 여기서 $ local_file은 "로컬 파일 경로"입니다. –

+1

우리는 ftp_put과 ftp_fput에 대해 서로 다른 기능을 이야기하고 있습니다. OP 코드는 마지막 코드를 포함합니다. –

관련 문제