2013-02-11 2 views
2

다음 두 가지 문제가 발생했습니다. 1. 스크립트의 아래 줄은 최대 파일 크기를 확인하지 않고 Please Select a Photo을 표시합니다.여러 파일 업로드 및 최대 크기 문제

내가 : 사용자가 1-5 사진을 업로드 할 수있는 권리를 가지고 있지만 하나 개의 사진이 필요합니다, 한 장의 사진이 요구되는 동안 사용자가 다섯 개 사진을 업로드 할 수 있도록하려는
//Check that the file is not too big 
if ($_FILES[$file_field]['size'] > $max_size) { 
    $out['error'][] = "File is too big"; 
    } 

2. 그/그녀는 할 수 없습니다 모든 입력이 비어있는 양식을 제출하십시오. 이 두 가지 문제를 어떻게 해결할 수 있습니까?

<?php 

function uploadFile ($file_field = null, $check_image = false, $random_name = false) { 

//Config Section  
//Set file upload path 
$path = 'Productpic/'; //with trailing slash 
//Set max file size in bytes 
$max_size = 2097152; 
//Set default file extension whitelist 
$whitelist_ext = array('jpg','png','gif', 'JPG'); 
//Set default file type whitelist 
$whitelist_type = array('image/jpeg', 'image/png','image/gif'); 

//The Validation 
// Create an array to hold any output 
$out = array('error'=>null); 

if (!$file_field) { 
$out['error'][] = "Please specify a valid form field name";   
} 

if (!$path) { 
$out['error'][] = "Please specify a valid upload path";    
} 

if (count($out['error'])>0) { 
return $out; 
} 

//Make sure that there is a file 
if((!empty($_FILES[$file_field])) && ($_FILES[$file_field]['error'] == 0)) { 

// Get filename 
$file_info = pathinfo($_FILES[$file_field]['name']); 
$name = $file_info['filename']; 
$ext = $file_info['extension']; 

//Check file has the right extension   
if (!in_array($ext, $whitelist_ext)) { 
    $out['error'][] = "Invalid file Extension"; 
} 

//Check that the file is of the right type 
if (!in_array($_FILES[$file_field]["type"], $whitelist_type)) { 
    $out['error'][] = "Invalid file Type"; 
} 

//Check that the file is not too big 
if ($_FILES[$file_field]['size'] > $max_size) { 
    $out['error'][] = "File is too big"; 
    } 

//If $check image is set as true 
if ($check_image) { 
    if (!getimagesize($_FILES[$file_field]['tmp_name'])) { 
    $out['error'][] = "The file you trying to upload is not an Image, we only accept images"; 
    } 
} 

//Create full filename including path 
if ($random_name) { 

    // Generate random filename 
    $tmp = str_replace(array('.',' '), array('',''), microtime()); 

    if (!$tmp || $tmp == '') { 
    $out['error'][] = "File must have a name"; 
    }  
    $newname = $tmp.'.'.$ext;         
} else { 
    $newname = $name.'.'.$ext; 
} 

//Check if file already exists on server 
if (file_exists($path.$newname)) { 
    $out['error'][] = "The image you trying to upload already exists, please upload only once"; 
} 

if (count($out['error'])>0) { 
    //The file has not correctly validated 
    return $out; 
} 

if (move_uploaded_file($_FILES[$file_field]['tmp_name'], $path.$newname)) { 
    //Success 

    $out['filepath'] = $path; 
    $out['filename'] = $newname; 
    return $out; 
} else { 
    $out['error'][] = "Server Error!"; 
} 

} else { 
$out['error'][] = "Please select a photo"; 
return $out; 
}  
} 
?> 



<?php 

if (isset($_POST['submit'])) { 

$file = uploadFile('file', true, false); 
if (!is_array($file['error'])) { 
$message = ''; 
$sub=1; 
$message = "File uploaded successfully"; 
echo $message; 
} 

} 
?> 



<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
<?php 
ini_set("display_errors", 0); 
if($sub==0) 
{ 
?> 
<input name="file" type="file" size="20" /><span><?php 

if (isset($_POST['submit'])) { 

$file = uploadFile('file', true, false); 
if (is_array($file['error'])) { 
$message = ''; 
foreach ($file['error'] as $msg) { 
    $message = $msg;  
} 
} 
echo $message; 
} 
?></span> <br> 
<input name="submit" type="submit" value="Upload" /> 
<?php 
} 
?> 
</form> 
+0

파일 업로드가 아파치에서 확인되었습니다 ... –

+0

그게 무슨 뜻입니까? 나는 너를 못 봤어. – alte

+0

php.ini 파일에서 file_uploads를 검색하여 file_uploads =로 만드십시오. –

답변

0

파일 경로를 확인하십시오. 경로가 사실이라면 그 일을 할 수 있습니다. exif_imagetype() 함수를 사용하여 파일 속성을 확인하는 것이 좋습니다.이 기능은 getimage 크기보다 빠르고 더 좋습니다. 최대 파일 문제에서는 filesize() 함수를 사용해야합니다. 파일이 업로드되었는지 확인하기 전에 확인 하시겠습니까? 업로드 된 파일은 $ _FILES ('tmp_name')입니다.

+0

실제로 사진의 크기를 다르게 변경하려고 시도했는데 사진 중 일부는 작동하는 것 같습니다 ... 스크립트 작성 방법을 알고 있습니까? 여러 사진을 업로드 하시겠습니까? – alte