2014-05-10 2 views
0

내 웹 사이트에 이미지를 업로드하는 코드가 있으며이를 작성하는 방법을 알고 싶습니다. 사용자가 파일을 선택하지 않으면 'default.png'로 설정할 수 있습니다. . 파일 업로드가 비어있는 경우 기본 파일

양식

<label for="file">Blog Post Image:</label> 
<input type="file" name="file" id="file"> 

의 코드 추출하고이 파일을

$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$temp = explode(".", $_FILES["file"]["name"]); 
$extension = end($temp); 

if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/pjpeg") 
|| ($_FILES["file"]["type"] == "image/x-png") 
|| ($_FILES["file"]["type"] == "image/png")) 
&& ($_FILES["file"]["size"] < 40000) 
&& in_array($extension, $allowedExts)) { 
    if ($_FILES["file"]["error"] > 0) { 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } else { 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 
    if (file_exists("images/blog/" . $_FILES["file"]["name"])) { 
     echo $_FILES["file"]["name"] . " already exists. "; 
    } else { 
     move_uploaded_file($_FILES["file"]["tmp_name"], 
     "../images/blog/" . $_FILES["file"]["name"]); 
     echo "Stored in: " . "../images/blog/" . $_FILES["file"]["name"]; 
    } 
    } 
} else { 
    echo "Invalid file"; 
} 

다음이 그냥 데이터베이스에 추가됩니다 후 업로드 PHP 코드는 ...

답변

0

images/blob/내에 default.png를 유지하십시오. 업로드가 비어있는 경우 is_uploaded_file()으로 체크인하십시오.

if(!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) { 
    echo 'No upload'; 
    // use images/blog/default.png 
    echo "Upload: default.png <br>"; 
    echo "Type: image/png <br>"; 
    echo "Size: " . filesize("images/blog/default.png") . " bytes<br>"; 
    echo "Temp file: images/blog/default.png <br>"; 
} else{ 
    $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $temp = explode(".", $_FILES["file"]["name"]); 
    $extension = end($temp); 

    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/jpg") 
    || ($_FILES["file"]["type"] == "image/pjpeg") 
    || ($_FILES["file"]["type"] == "image/x-png") 
    || ($_FILES["file"]["type"] == "image/png")) 
    && ($_FILES["file"]["size"] < 40000) 
    && in_array($extension, $allowedExts)) { 
     if ($_FILES["file"]["error"] > 0) { 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
     } else { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 
     if (file_exists("images/blog/" . $_FILES["file"]["name"])) { 
      echo $_FILES["file"]["name"] . " already exists. "; 
     } else { 
      move_uploaded_file($_FILES["file"]["tmp_name"], 
      "../images/blog/" . $_FILES["file"]["name"]); 
      echo "Stored in: " . "../images/blog/" . $_FILES["file"]["name"]; 
     } 
     } 
    } else { 
     echo "Invalid file"; 
    } 
}