File Upload
페이지 정보
작성자 서방님 댓글 1건 조회 359회 작성일 16-11-04 11:27본문
1 | file_uploads = On | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form> </body> </html> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } ?> | cs |
1 2 3 4 5 6 7 | <?php // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } ?> | cs |
1 2 3 4 5 6 7 | <?php // Check file size if ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; } ?> | cs |
1 2 3 4 5 6 7 8 9 | <?php // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } ?> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form> <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?> </body> </html> | cs |
댓글목록
서방님님의 댓글
서방님 작성일
<div><span class="Apple-tab-span" style="white-space:pre"> </span><!DOCTYPE html></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><html></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><body></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><form action="test.php" method="post" enctype="multipart/form-data"></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Select image to upload:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span><input type="file" name="fileToUpload" id="fileToUpload"></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><input type="submit" value="Upload Image" name="submit"></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></form></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><?php</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$target_dir = "./test/"; // 업로드 디렉토리 (끝에 슬래쉬 포함)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$target_file = $target_dir.basename($_FILES["fileToUpload"]["name"]);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$uploadOk = 1;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);</div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Debug($target_file);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>exit;</div><div><br /></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if (isset($_POST["submit"])) { // 실제 이미지인지 mime 타입 체크</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if ($check !== false) { // 이미지 파일이라면</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>} else {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "이미지 파일이 아닙니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$uploadOk = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br /></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if (file_exists($target_file)) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "해당 파일이 이미 존재합니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$uploadOk = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br /></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if ($_FILES["fileToUpload"]["size"] > 1048576) { // 1MB 보다 크다면 (Byte 단위 체크)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "해당 파일이 관리자가 설정한 용량(1MB)보다 큽니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$uploadOk = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br /></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "JPG, JPEG, PNG, GIF 확장자만 허용합니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>$uploadOk = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br /></div><div><br /></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if ($uploadOk == 0) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "파일이 업로드 되지 않았습니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>} else {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "파일명 : ". basename( $_FILES["fileToUpload"]["name"]). " 이 정상적으로 업로드 되었습니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>} else {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>echo "파일 업로드중 에러가 발생하였습니다.";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>?></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></body></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></html></div>
<img id="floatBarImgId" src="chrome-extension://dbkmjjclgbiooljcegcddagnddjedmed/pics/save2-32.png" editor_component="image_link" style="display: none; opacity: 0; position: absolute; z-index: 2147483647;"/>