2014-10-16 2 views
0
이 코드를 사용하고

는 :PHP 파일 업로드 누락 된 파일 확장

$target_dir = "../uploads/"; 
$uploadOk=1; 

$newname = $target_dir .'file_' . rand(0, 1000000) . '.' . end(explode(".", $_FILES["vimage1"]["name"])); 

if (move_uploaded_file($_FILES["vimage1"]["tmp_name"], $newname)) { 
    echo "The file ". basename($_FILES["vimage1"]["name"]). " has been uploaded."; 
} 

else { 
    echo "Sorry, there was an error uploading your file."; 
} 

신비 나는 하나 개의 기능이 동일한 코드를 사용하고 있는데 모두가 잘 작동한다는 것입니다.

두 번째 함수에 동일한 코드를 복사했는데 어떤 이상한 이유로 인해 파일 확장자가 $newname에서 누락되어 계속 업로드 오류가 발생합니다.

누구에게 이런 일이 일어날 수있는 아이디어가 있습니까?

+0

전 기능을 보여주고 – Etixpp

+1

을 변경 한 후 차라리'의 PathInfo을 사용하십시오 ($ _ FILES [ "vimage1"] [ "이름"], PATHINFO_EXTENSION) '또한 확장 – halloei

+0

을 얻으려면,의 형태에 대한 코드를 제공하십시오 서로 다른 두 가지 기능으로 업로드하는 데 사용됩니다. – Dencker

답변

0

당신이 함께 일을 할 수 있습니다 : 당신은/사용 런타임에 end()에 배열/변수를 전달할 수 없습니다

<?php 
function upload($index,$destination,$maxsize=FALSE,$extensions=FALSE) 
{ 
    if (!isset($_FILES[$index]) OR $_FILES[$index]['error'] > 0) return FALSE; 

    if ($maxsize !== FALSE AND $_FILES[$index]['size'] > $maxsize) return FALSE; 
    // extension 
    $ext = substr(strrchr($_FILES[$index]['name'],'.'),1); 
    if ($extensions !== FALSE AND !in_array($ext,$extensions)) return FALSE; 
/
    return move_uploaded_file($_FILES[$index]['tmp_name'],$destination); 
} 

//EXEMPLES 
    $upload1 = upload('icone','uploads/monicone1',15360, array('png','gif','jpg','jpeg')); 
    $upload2 = upload('mon_fichier','uploads/file112',1048576, FALSE); 

    if ($upload1) "Successfully uploaded<br />"; 
    if ($upload2) "Failure in uploading!<br />"; 
?> 
0

. 먼저 파일 이름을 폭발시킨 다음 전달해야합니다. 이 같은.

$proposedExtension = explode(".", $_FILES["vimage1"]["name"]); 
$extension = end($imageExtension); 
$newname = $target_dir .'file_' . rand(0, 1000000) . '.'.$extension; 
echo $newname; 

는 파일의 확장자를 얻을 수 pathinfo($_FILES["vimage1"]["name"], PATHINFO_EXTENSION)를 사용하는 것이 좋을 것이다.