Backend/PHP
파일 업로드 및 다운로드
Jeffrey Oh
2019. 6. 26. 15:34
파일 업로드 및 다운로드
/** 파일 업로드 처리 */// <input type="file" name="file[]"/>$files = $_FILES['file'];$cpt = count($_FILES['file']['name']); for($i=0; $i<$cpt; $i++) { $_FILES['file']['name']= $files['name'][$i]; $_FILES['file']['type']= $files['type'][$i]; $_FILES['file']['tmp_name']= $files['tmp_name'][$i]; $_FILES['file']['error']= $files['error'][$i]; $_FILES['file']['size']= $files['size'][$i]; $config = $this->config->item('upload'); // 파일이 업로드 될 폴더가 존재하지 않는다면 생성한다. // --> './files/upload/' 경로에 대한 처리가 수행된다. if (!is_dir($config['upload_path'])) { // 경로, 퍼미션, 하위폴더 생성 여부 설정 mkdir($config['upload_path'], 0766, TRUE); } $this->upload->initialize($config); $upload = $this->upload->do_upload('file'); $data = $this->upload->data(); if (!empty($upload)) { // 원본 파일명 $this->file_dao->orginName = $data['client_name']; // 파일이 저장된 경로 $filename = $data["file_name"]; $this->file_dao->fileDir = "./files/upload/".$filename; $this->file_dao->fileName = $filename; // 파일 형식 $this->file_dao->contentType = $data['file_type']; $this->file_dao->fileSize = $data["file_size"]; // 등록일시 $this->file_dao->regDate = '{now()}'; // 변경일시 $this->file_dao->editDate = '{now()}'; // 파일이 속한 게시글의 일련번호 $this->file_dao->boardId = $boardId; // 작성자의 일련번호 $this->file_dao->memberId = $this->session->userdata("user_info")->memberId; // 데이터 저장하기 $result = $this->file_dao->insert(); if (!$result) { echo json_encode(array("rt" => "첨부파일 저장 실패")); // 업로드 된거 다시 삭제 exit; } }}
/** 파일 업로드 처리 */ // <input type="file" name="file[]"/> $files = $_FILES['file']; $cpt = count($_FILES['file']['name']); for($i=0; $i<$cpt; $i++) { $_FILES['file']['name']= $files['name'][$i]; $_FILES['file']['type']= $files['type'][$i]; $_FILES['file']['tmp_name']= $files['tmp_name'][$i]; $_FILES['file']['error']= $files['error'][$i]; $_FILES['file']['size']= $files['size'][$i]; $config = $this->config->item('upload'); // 파일이 업로드 될 폴더가 존재하지 않는다면 생성한다. // --> './files/upload/' 경로에 대한 처리가 수행된다. if (!is_dir($config['upload_path'])) { // 경로, 퍼미션, 하위폴더 생성 여부 설정 mkdir($config['upload_path'], 0766, TRUE); } $this->upload->initialize($config); $upload = $this->upload->do_upload('file'); $data = $this->upload->data(); if (!empty($upload)) { // 원본 파일명 $this->file_dao->orginName = $data['client_name']; // 파일이 저장된 경로 $filename = $data["file_name"]; $this->file_dao->fileDir = "./files/upload/".$filename; $this->file_dao->fileName = $filename; // 파일 형식 $this->file_dao->contentType = $data['file_type']; $this->file_dao->fileSize = $data["file_size"]; // 등록일시 $this->file_dao->regDate = '{now()}'; // 변경일시 $this->file_dao->editDate = '{now()}'; // 파일이 속한 게시글의 일련번호 $this->file_dao->boardId = $boardId; // 작성자의 일련번호 $this->file_dao->memberId = $this->session->userdata("user_info")->memberId; // 데이터 저장하기 $result = $this->file_dao->insert(); if (!$result) { echo json_encode(array("rt" => "첨부파일 저장 실패")); // 업로드 된거 다시 삭제 exit; } } } |
이미지 다운로드 헤더 설정
$item = $this->file_dao->where("orginName", $fileName)->select_one(); if (!empty($item)) { $filePath = $item->fileDir;
$size = filesize("./files/upload/".$item->fileName);
if (is_file("./files/upload/".$item->fileName)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $fileName); header('Content-Transfer-Encoding: binary'); header('Connection: Keep-Alive'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $size); $fp = fopen($filePath, "rb"); fpassthru($fp); fclose($fp); } } |