📖
그누보드 스마트에디터 에서 이미지 업로드시 다른 서버로 ftp 전송하는 루틴
페이지 정보
본문
스마트에이터에 이미지호스팅으로 연결되도록 설정해봤습니다.
/plugin/editor/smarteditor2/photo_uploader/popup/php/UploadHandler.php
약 1126번째줄[code]protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
$file = new \stdClass();
$file->oriname = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);
$filename_ext = pathinfo($name, PATHINFO_EXTENSION);
$file->name = $this->get_file_passname().'_'.str_replace(".", "_", $this->get_microtime()).".".$filename_ext;
$file->size = $this->fix_integer_overflow(intval($size));
$file->type = $type;
if (SMARTEDITOR_UPLOAD_IMG_CHECK && !$this->reprocessImage($uploaded_file, null)) {
$file->error = $this->get_error_message('accept_file_types');
return $file;
}
if ($this->validate($uploaded_file, $file, $error, $index)) {
$this->handle_form_data($file, $index);
// === FTP 설정 ===
$ftp_server = "#FTP주소#";
$ftp_user_name = "#유저#";
$ftp_user_pass = "#패스워드#";
$ftp_base_path = "www/data/editor"; // 이미지호스팅에 맞춰서 설정해주세요.(/data/editor은 하단에서 자동 폴더생성됨)
$ftp_full_path = $ftp_base_path . "/" . $file->name;
// 로컬 파일 존재 확인
if (!file_exists($uploaded_file)) {
$file->error = "업로드 파일 없음";
return $file;
}
// FTP 연결
$conn_id = ftp_connect($ftp_server);
if (!$conn_id || !ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
$file->error = "FTP 연결 실패";
return $file;
}
ftp_pasv($conn_id, true);
// 디렉토리 생성
$ftp_dir_parts = explode('/', $ftp_base_path);
$cwd = '';
foreach ($ftp_dir_parts as $part) {
if ($part == '') continue;
$cwd .= '/' . $part;
if (!@ftp_chdir($conn_id, $cwd)) {
if (!ftp_mkdir($conn_id, $cwd)) {
ftp_close($conn_id);
$file->error = "FTP 디렉토리 생성 실패: $cwd";
return $file;
}
}
}
ftp_chdir($conn_id, '/');
// FTP 업로드
if (!ftp_put($conn_id, $ftp_full_path, $uploaded_file, FTP_BINARY)) {
ftp_close($conn_id);
$file->error = "FTP 업로드 실패";
return $file;
}
// 권한 설정 (Cafe24는 644 필요)
if (function_exists('ftp_chmod')) {
ftp_chmod($conn_id, 0644, $ftp_full_path);
}
// URL 지정 (Cafe24 CDN)
$file->url = "#제공받는 CDN주소#/data/editor/" . $file->name;
// === 원격 파일을 임시로 다운로드해서 이미지 정보 추출 ===
$tmp_local_file = sys_get_temp_dir() . "/" . $file->name;
if (ftp_get($conn_id, $tmp_local_file, $ftp_full_path, FTP_BINARY)) {
// 사이즈 갱신
$file_size = filesize($tmp_local_file);
$file->size = $file_size;
// 이미지 검사
if ($this->is_valid_image_file($tmp_local_file)) {
$this->handle_image_file($tmp_local_file, $file);
if ($this->options['is_resize']) {
$resize_options = [
'max_width' => $this->options['resize_max_width'],
'max_height' => $this->options['resize_max_height'],
'jpeg_quality' => $this->options['resize_jpeg_compress'],
'auto_orient' => true,
];
if ($this->create_scaled_image($file->name, '', $resize_options)) {
$file->size = filesize($tmp_local_file);
}
}
$image_info = getimagesize($tmp_local_file);
$file->width = $image_info[0];
$file->height = $image_info[1];
$file->type = $image_info['mime']; // image/png 등
if (function_exists('run_replace')) {
$file->url = run_replace('get_editor_upload_url', $file->url, $ftp_full_path, $file);
}
$this->files[] = $file->name;
} else {
$file->error = $this->get_error_message('accept_file_types');
}
unlink($tmp_local_file);
} else {
$file->error = "FTP 원격 파일 다운로드 실패";
}
ftp_close($conn_id);
$this->set_additional_file_properties($file);
}
return $file;
}[/code]
/plugin/editor/smarteditor2/photo_uploader/popup/php/UploadHandler.php
약 1126번째줄[code]protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
$file = new \stdClass();
$file->oriname = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);
$filename_ext = pathinfo($name, PATHINFO_EXTENSION);
$file->name = $this->get_file_passname().'_'.str_replace(".", "_", $this->get_microtime()).".".$filename_ext;
$file->size = $this->fix_integer_overflow(intval($size));
$file->type = $type;
if (SMARTEDITOR_UPLOAD_IMG_CHECK && !$this->reprocessImage($uploaded_file, null)) {
$file->error = $this->get_error_message('accept_file_types');
return $file;
}
if ($this->validate($uploaded_file, $file, $error, $index)) {
$this->handle_form_data($file, $index);
// === FTP 설정 ===
$ftp_server = "#FTP주소#";
$ftp_user_name = "#유저#";
$ftp_user_pass = "#패스워드#";
$ftp_base_path = "www/data/editor"; // 이미지호스팅에 맞춰서 설정해주세요.(/data/editor은 하단에서 자동 폴더생성됨)
$ftp_full_path = $ftp_base_path . "/" . $file->name;
// 로컬 파일 존재 확인
if (!file_exists($uploaded_file)) {
$file->error = "업로드 파일 없음";
return $file;
}
// FTP 연결
$conn_id = ftp_connect($ftp_server);
if (!$conn_id || !ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
$file->error = "FTP 연결 실패";
return $file;
}
ftp_pasv($conn_id, true);
// 디렉토리 생성
$ftp_dir_parts = explode('/', $ftp_base_path);
$cwd = '';
foreach ($ftp_dir_parts as $part) {
if ($part == '') continue;
$cwd .= '/' . $part;
if (!@ftp_chdir($conn_id, $cwd)) {
if (!ftp_mkdir($conn_id, $cwd)) {
ftp_close($conn_id);
$file->error = "FTP 디렉토리 생성 실패: $cwd";
return $file;
}
}
}
ftp_chdir($conn_id, '/');
// FTP 업로드
if (!ftp_put($conn_id, $ftp_full_path, $uploaded_file, FTP_BINARY)) {
ftp_close($conn_id);
$file->error = "FTP 업로드 실패";
return $file;
}
// 권한 설정 (Cafe24는 644 필요)
if (function_exists('ftp_chmod')) {
ftp_chmod($conn_id, 0644, $ftp_full_path);
}
// URL 지정 (Cafe24 CDN)
$file->url = "#제공받는 CDN주소#/data/editor/" . $file->name;
// === 원격 파일을 임시로 다운로드해서 이미지 정보 추출 ===
$tmp_local_file = sys_get_temp_dir() . "/" . $file->name;
if (ftp_get($conn_id, $tmp_local_file, $ftp_full_path, FTP_BINARY)) {
// 사이즈 갱신
$file_size = filesize($tmp_local_file);
$file->size = $file_size;
// 이미지 검사
if ($this->is_valid_image_file($tmp_local_file)) {
$this->handle_image_file($tmp_local_file, $file);
if ($this->options['is_resize']) {
$resize_options = [
'max_width' => $this->options['resize_max_width'],
'max_height' => $this->options['resize_max_height'],
'jpeg_quality' => $this->options['resize_jpeg_compress'],
'auto_orient' => true,
];
if ($this->create_scaled_image($file->name, '', $resize_options)) {
$file->size = filesize($tmp_local_file);
}
}
$image_info = getimagesize($tmp_local_file);
$file->width = $image_info[0];
$file->height = $image_info[1];
$file->type = $image_info['mime']; // image/png 등
if (function_exists('run_replace')) {
$file->url = run_replace('get_editor_upload_url', $file->url, $ftp_full_path, $file);
}
$this->files[] = $file->name;
} else {
$file->error = $this->get_error_message('accept_file_types');
}
unlink($tmp_local_file);
} else {
$file->error = "FTP 원격 파일 다운로드 실패";
}
ftp_close($conn_id);
$this->set_additional_file_properties($file);
}
return $file;
}[/code]
댓글목록
등록된 댓글이 없습니다.
![]() ![]() |