본문 바로가기
Linux&Ubuntu/WEB

php 파일업로드 소스 간단

by Vittorio_Lee 2024. 7. 1.
728x90
반응형
SMALL

# vi upfile.html

 

# vi upfile.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload</title>
</head>
<body>
    <h2>File Upload Form</h2>
    <form action="upfile.php" method="post" enctype="multipart/form-data">
        Select file to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload File" name="submit">
    </form>
</body>
</html>

~                                                                                              
~                             

 

 

# vi upfile.php
<?php
$uploadDir = "uploads/"; // 업로드 디렉토리 경로

// 업로드 디렉토리가 존재하지 않으면 생성합니다.
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

$maxFileSize = 5 * 1024 * 1024; // 5MB

// 업로드된 파일의 정보를 가져옵니다.
$fileName = $_FILES["fileToUpload"]["name"];
$fileTmpName = $_FILES["fileToUpload"]["tmp_name"];
$fileSize = $_FILES["fileToUpload"]["size"];

// 파일 확장자를 체크하고 허용되는 확장자를 지정합니다.
$allowedExtensions = ["jpg", "jpeg", "png", "gif"];
$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

if (in_array($fileExtension, $allowedExtensions)) {
    // 파일 크기를 체크하고 원하는 크기로 제한합니다.
    if ($fileSize <= $maxFileSize) {
        // 새로운 파일 이름을 생성합니다.
        $newFileName = uniqid() . "." . $fileExtension;
        $uploadPath = $uploadDir . $newFileName;

        // 파일을 이동시킵니다.
        if (move_uploaded_file($fileTmpName, $uploadPath)) {
            echo "파일 업로드 성공: " . $newFileName;
        } else {
            echo "파일 업로드 실패.";
        }
    } else {
        echo "파일 크기가 너무 큽니다. 최대 파일 크기는 " . ($maxFileSize / (1024 * 1024)) . "MB입니다.";
    }
} else {
    echo "지원하지 않는 파일 형식입니다. jpg, jpeg, png, gif 파일만 허용됩니다.";
}
?>

 

 

도메인/upfile.html 파일선택 -> 업로드 -> 해당 파일 표출 

 

http://vittorio.블라 블라.com/uploads/6682370de4db2.png

 

 

728x90
반응형
LIST

'Linux&Ubuntu > WEB' 카테고리의 다른 글

검색봇 차단  (0) 2024.05.24
ubuntu 22.04apache2 URL 에서 .php 확장자 제거  (0) 2023.11.03
apache mpm 수정  (0) 2023.10.24
[linux] apache nginx tomcat 비교 및 차이  (0) 2022.02.03
[linux] 웹페이지 현재 접속자수 확인!  (0) 2022.02.03