Notice
Recent Posts
Recent Comments
Link
Daniel's Knowledge Storage
php FILES 관련 본문
파일 업로드 관련소스를 보면 대부분 복잡합니다.
고로 귀찮아서 책안보시고 공부하는분들은 접근하기가 어렵는데, 사실 한번 해보면 쉽습니다.
아래는 기본적인 업로드에 관련된 소스입니다. 폼입니다.
- <form method="post" enctype="multipart/form-data" action="upload_ok.php">
- <input type="file" name="userfile" style="width: 100%;" /><br>
- <input type="submit" value="파일 업로드" />
- </form>
이렇게 보내면 이렇게 변수를 받습니다.
- $_FILES['userfile']['userfile']['name'] = "업로드한파일명.jpg";
- $_FILES['userfile']['userfile']['type'] = "image/jpeg"; // 파일의 포맷
- $_FILES['userfile']['userfile']['tmp_name'] = "/tmp/phpKAI9e1"; // 파일이 서버의 임시 폴더로 들어가고, 그 파일명입니다. (사실 실제로 서버에 업로드되는건 아닙니다.)
- $_FILES['userfile']['userfile']['error'] = 0; // 오류가 발생했을때 0 이 아닌 다른 숫자가 나옵니다.
- $_FILES['userfile']['userfile']['size'] = 77563; // 업로드한 파일의 사이즈입니다. (바이트)
아주 간단하게는 이렇게 하면 끝납니다.
- <?
- move_uploaded_file($_FILES['userfile']['userfile']['tmp_name'], "img/".$_FILES['userfile']['userfile']['name']);
- echo "<img src='img/".$_FILES['userfile']['userfile']['tmp_name']."' />";
- ?>
move_uploaded_file 함수 말고 cp라는 함수도 있는데, 이 함수가 더 낫다고 하네요.
이 코드는 서버의 임시 폴더로 들어간 파일을 현재 php파일이 존재하는 곳의 img 폴더에 업로드한 파일명으로 파일을 옮기는 과정입니다. 간단하게 이렇게 하면 업로드가 끝납니다.
확장자 제한, 오류발생시 무시등은 여러분의 몫입니다.
출처 : http://awhile.us/blog/99
'Develop > PHP' 카테고리의 다른 글
Warning: main(): php_network_getaddresses 에러 발생시 (0) | 2010.01.07 |
---|---|
PHP세션 서브도메인에서 로그인 풀리지 않도록 (0) | 2009.12.25 |
PHP 코드 최적화 가이드 (0) | 2009.10.24 |
DB의 내용을 PHP로 파싱을 해서 xml파일로 저장 (0) | 2009.09.02 |
php 파일 크기 단위 변환(kb,mb...) (0) | 2009.09.01 |
Comments