본문 바로가기

WEB/PHP

PHP 파일 업로드 & 다운로드

728x90
반응형

1. PHP File up & download

  • upload
<html>
<head>
<title> File Upload </title>
</head>
 
<body>
<form method="POST" action="" enctype="multipart/form-data">
        <input type="file" name="uploadfile"/>
        <input type="submit" value="upload" name="upload"/>
</form>
</body>
</html>
<?php
$file = $_FILES['uploadfile'];
echo "파일이름: ".$file['name']."<br>";
echo "파일크기: ".$file['size']."<br>";
echo "임시저장: ".$file['tmp_name']."<br>";
$path="./upload/";
 
 
 
 
if($_POST['upload']=="upload"){
        if(move_uploaded_file($file['tmp_name'],$path.$file['name'])){
                echo "Upload success!";
        }
        else {
                echo "Upload fail..";
        }
}
?>

 

  • download
<?php
$file_name = "a.txt";
$path = "./upload/$file_name";
$file_size = filesize($path);
echo $path;
 
header("Pragma: public");
header("expires: 0");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$file_size);
header("Content-Disposition: attachment; filename=\"$file_name\"");
 
ob_clean();
flush();
readfile($path);
?>

 
+

enctype="multipart/form-data

multipart/form-data는 파일 업로드가 있는 양식요소에 사용되는 enctype 속성의 값중 하나
multipart는 폼데이터가 여러 부분으로 나뉘어 서버로 전송되는 것을 의미

 

<문법>

<form enctype="속성값">
속성값
설명
application/x-www-form-urlencoded
기본값으로, 모든 문자들은 서버로 보내기 전에 인코딩됨을 명시함.
multipart/form-data
모든 문자를 인코딩하지 않음을 명시함.
이 방식은 <form> 요소가 파일이나 이미지를 서버로 전송할 때 주로 사용함.
text/plain
공백 문자(space)는 "+" 기호로 변환하지만, 나머지 문자는 모두 인코딩되지 않음을 명시함.

728x90
반응형

'WEB > PHP' 카테고리의 다른 글

끝말잇기 게임 구현 (php)  (0) 2023.01.07
mysql과 mysqli 의 차이점  (0) 2023.01.07
PHP와 MYSQL 연동하기  (0) 2023.01.07
PHP cookie & session  (1) 2022.12.29
Php functions  (0) 2022.12.29