PHPExcel 설치

Jmnote (토론 | 기여)님의 2013년 10월 17일 (목) 00:05 판 (→‎결과물)
PHPExcel 설치
PHPExcel 시작하기

1 다운로드

내부에 Classes, Documentation, Examples 폴더가 있다.

2 설치

3 실행예시 (오류)

브라우저에서 위 주소대로 접속해보니 다음과 같은 오류 메시지가 나온다.

16:00:25 Create new PHPExcel object
16:00:25 Set document properties
16:00:25 Add some data
16:00:25 Rename worksheet
16:00:25 Write to Excel2007 format

Warning: unlink(/var/www/html/PHPExcel/Examples/01simple.xlsx) [function.unlink]: Permission denied in /var/www/html/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php on line 214

Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file /var/www/html/PHPExcel/Examples/01simple.xlsx.' in /var/www/html/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php:348 Stack trace: #0 /var/www/html/PHPExcel/Examples/01simple.php(88): PHPExcel_Writer_Excel2007->save('/var/www/html/P...') #1 {main} thrown in /var/www/html/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php on line 348
→ Permission denied 가 나왔다. Examples 폴더에 엑셀 파일을 저장하려는 데 권한이 없는 것이다.

4 폴더 권한 조정

httpd[1]에게 해당 폴더의 쓰기 권한이 없기 때문에 오류가 발생한다.

[root@jmnote ~]# ps -ef | grep httpd | grep -v grep
root      1750     1  0 Jun13 ?        00:00:00 /usr/sbin/httpd
apache    6874  1750  0 Jun19 ?        00:03:56 /usr/sbin/httpd
apache   18185  1750  0 08:29 ?        00:00:36 /usr/sbin/httpd
apache   18391  1750  0 08:48 ?        00:00:33 /usr/sbin/httpd
apache   18410  1750  0 08:49 ?        00:00:34 /usr/sbin/httpd
apache   18578  1750  0 09:09 ?        00:00:31 /usr/sbin/httpd
apache   18654  1750  0 09:11 ?        00:00:34 /usr/sbin/httpd
apache   20304  1750  0 11:41 ?        00:00:22 /usr/sbin/httpd
apache   20819  1750  0 12:12 ?        00:00:20 /usr/sbin/httpd
apache   22521  1750  0 14:48 ?        00:00:07 /usr/sbin/httpd
apache   24517  1750  0 Jun18 ?        00:04:58 /usr/sbin/httpd
apache   25934  1750  0 Jun20 ?        00:02:20 /usr/sbin/httpd
apache   29616  1750  0 Jun18 ?        00:04:32 /usr/sbin/httpd
→ 최초 구동은 root가 했지만, 실제로 사용자의 요청을 받아 PHP 명령어를 수행하는 계정은 apache이다.
[root@jmnote ~]# cd /var/www/html/PHPExcel
[root@jmnote PHPExcel]# ll
total 8
drwxr-xr-x 3 root root 4096 Jun 22 16:39 Classes
drwxr-xr-x 4 root root 4096 Jun 22 16:40 Examples
→ Examples 폴더의 퍼미션이 755이고, owner가 root이므로 apache 계정을 파일을 쓸 수 없는 상태이다.
[root@jmnote PHPExcel]# chown apache:apache Tests
→ Examples 폴더의 소유자를 apache로 바꾸었다.

5 실행예시 (성공)

이제 브라우저로 다시 접속(또는 새로고침)해보자.

15:58:26 Create new PHPExcel object
15:58:26 Set document properties
15:58:26 Add some data
15:58:26 Rename worksheet
15:58:26 Write to Excel2007 format
15:58:26 File written to 01simple.xlsx
Call time to write Workbook was 0.0275 seconds
15:58:26 Current memory usage: 7.5 MB
15:58:26 Write to Excel5 format
15:58:26 File written to 01simple.xls
Call time to write Workbook was 0.0347 seconds
15:58:26 Current memory usage: 10.5 MB
15:58:26 Peak memory usage: 10.5 MB
15:58:26 Done writing files
Files have been created in /web/jmnote/www/php/PHPExcel/Examples
→ 오류 없이 정상적으로 수행되었다.

다음과 같이 xlsx파일이 생성되었음을 확인할 수 있다.

[root@jmnote PHPExcel]# cd Examples
[root@jmnote Examples]# ll 01simple.*
-rw-r--r-- 1 root   root   4412 Oct 16 23:50 01simple.php
-rw-r--r-- 1 apache apache 4608 Oct 16 23:58 01simple.xls
-rw-r--r-- 1 apache apache 6371 Oct 16 23:58 01simple.xlsx

6 결과물

01simple.xlsx을 내려받아 열어보니 내용은 다음과 같다.

1
2
3
4
5
6
7
A B C D
Hello Hello
world! world!
Miscellaneous glyphs
éàèùâêîôûëïüÿäöüç
Hello World

7 주석

  1. 보다 정확하게는 httpd를 실행한 리눅스 계정
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}