2014-12-05 2 views
-1

PHP를 사용하여 데이터베이스에 사진 만 업로드하고 싶습니다. 나는이 시도 무엇 ,데이터베이스에있는 이미지 만 업로드하는 방법

<?php 

if (isset($_POST['Upload'])) { 
    $con = mysql_connect("localhost", "root", ""); 
    if (!$con) { 
     die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("iis", $con); 
    $image = $_FILES["product_image"]["name"]; 
    $imageType = mysql_real_escape_string($_FILES["product_image"]["type"]); 
    if (substr($imageType, 0, 5) == "image") { 
     if (!file_exists("product_images")) { 
      mkdir("product_images"); 
     } 

     if ($_FILES["product_image"]["error"] > 0) { 
      $error = "ERROR Return Code :" . $_FILES["product_image"]["error"] . "<br />"; 
     } else { 
      move_uploaded_file($_FILES["product_image"]["tmp_name"], "product_images/" . $_FILES["product_image"]["name"]); 
     } 
    } 

    $UserName = $_SESSION['id']; 
    $product_image = ("product_images/" . $_FILES["product_image"]["name"]); 
    mysql_query("INSERT INTO `feedbackzxc` VALUES ('', '$UserName', '$product_image')"); 
    echo "Image Uploaded!"; 
} else { 
    echo "Only images are allowed"; 
} 

?> 

하지만 이미지 이외의 파일을 업로드 할 때 오류 메시지가 표시되지 않습니다. 이미지가 아닌 다른 파일을 업로드하면 오류 메시지가 표시되게하려면 어떻게해야합니까? 메시지 이미지 만이 확인 if 블록 다음에 위치해야 도시을 허용

+1

가능한 중복 : //stackoverflow.com/questions/6755192/uploaded-file-type-check-by-php) – JasonCG

답변

1

else 블록 : substr($imageType,0,5) == "image"

if(substr($imageType,0,5) == "image"){ 
    if(!file_exists("product_images")) 
    { 
     mkdir("product_images"); 
    } 
    if($_FILES["product_image"]["error"] > 0) 
    { 
     $error = "ERROR Return Code :" . $_FILES["product_image"]["error"] . "<br />"; 
    } 
    else 
    { 

     move_uploaded_file($_FILES["product_image"]["tmp_name"], "product_images/". 
     $_FILES["product_image"]["name"]); 
    } 
} 
else 
{ 
    echo "Only images are allowed"; 
} 
[PHP 업로드 한 파일 타입 체크 (HTTP의
관련 문제