File upload

Hey,

Whats the best way to impliment a file size check on the code below…

Not sure if I should check the combined size of files to make sure its under the server max file size of 2meg or if I just check each file and echo a message in place of each file the exceeds the max file size?

also stop the process if no files are selected and echo message… the above doesnt apear to work…



	$number_of_fields = 5;
	$counter = 1;
	
	
	if($_FILES['logoname']['size'] > 2293760) { 
  	$error_msg = 'Combined file size is to large';
	}elseif ($_FILES['logoname'] == NULL) { 
	$error_msg = 'You must upload atleast one file';
	}
	
	if (($error_msg == '') && (isset($uploadlogo))){
	
	if(isset($_FILES['logoname'])) {
		
	$counter = 0;
	
	$known_photo_types = array(
	'image/pjpeg' => 'jpg',
	'image/jpeg' => 'jpg');
        
	$gd_function_suffix = array(
	'image/pjpeg' => 'JPEG',
	'image/jpeg' => 'JPEG');

	$photos_uploaded = $_FILES['logoname'];        
	
	while($counter <= count($_FILES['logoname']['tmp_name'])) {
	if($photos_uploaded['size'][$counter] > 0) {
	if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) {
	echo 'File '.($counter+1).' is not a photo!<br />';

	}else{

 process files....


Thanks

I would have thought below should work… but it appears to stop all files no matter what the size…


if($_FILES['logoname']['size'] > 2293760) { 
      $error_msg = 'Combined file size is to large';
    }

I am confused by the “elseif” in line 3. Where is the “else”?

The else statment is not correct but ment for… if no files were uploaded then stop script and echo error message…

For checking if a file has been uploaded or not, use the built in error code. if it is 4 then there was no file selected.
See http://php.net/manual/en/features.file-upload.errors.php for full code details.


if($_FILES['logoname']['error'] == 4) {
// no file uploaded

} else {
//file uploaded

}

the size check you have looks ok so its probably the combination of how you have put them together that is killing it.

Sweet thanks sikeZ, that links helps… There can be 1-5 files uploaded off the same form… Im not sure if that affects things or not?

Thanks

Arrg not sure what is happening…

With the code below… when I submit the form with no photos it returns a blank page… it should reload the form along with the error message… Cant see why it wouldnt be working?



	$images_dir = 'images/logos';
	$number_of_fields = 5;
	$counter = 1;
	
	if($_FILES['logoname']['error'] == 4) {
  		$error_msg = 'You must upload atleast one logo';
	}
	
	
	if(($error_msg == '') && (isset($uploadlogo))){
	
	// Process photos
	
	}else{
	
	echo '
	<div id="containerRegistration">
	<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">
	 <d1>
      <d2></d2>
	   <d3><label for="error">'.$error_msg.'</label></d3>
    </d1>
	
	<d1>
     <d2></d2>
       <d3>';
		while($counter <= $number_of_fields){
			echo '<input name="logoname[]" type="file" id="logoname" />';
  				$counter++;
		}
	echo '
	   </d3>
     </d1>
	
     <d1>
      <d2></d2>
       <d3>
	     <input name="companyid" type="hidden" value="'.$_REQUEST['companyid'].'" />
	     <input type="submit" name="uploadlogo" id="submit" value="Submit" />
	   </d3>
    </d1>
	</div>
	';
	
	}


I have tweaked the code a little for you and there are some comments in there too.


 <?php
    $error_msg = ''; /* set empty variable first! */
    $images_dir = 'images/logos'; 
    $number_of_fields = 5; 
    $counter = 1; 
    
    if(isset($_POST['uploadlogo'])) {
    /* Once the form is submitted, check for errors
    ------------------------------------------------------*/


    /*
     | Loop through the $_FILES array and check each error
     | message
     ------------------------------------------------------*/
    for($i=0; $i<=count($_FILES['logoname'])-1; $i++) {
        if($_FILES['logoname']['error'][$i] == 4) {
            /* if it is empty, append using .= to the empty error var */
            $error_msg .= '<p>You must upload atleast one logo</p>'; 
        } 
            
      }
    /* debug area
    | Useful to see what's going
    | through the form.
    -----------------------------*/
    echo '<pre>';
    print_r($_FILES);
    echo '</pre>';
    /*----------------------------*/ 
    }
    
     
     
    if(($error_msg == '') && (isset($_POST['uploadlogo']))){ 
     
    // Process photos 
     
    }else{ 
     
    echo ' 
    <div id="containerRegistration"> 
    <form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data"> 
     <d1> 
      <d2></d2> 
       <d3><label for="error">'.$error_msg.'</label></d3> 
    </d1> 
     
    <d1> 
     <d2></d2> 
       <d3>'; 
        while($counter <= $number_of_fields){ 
            echo '<input name="logoname[]" type="file" id="logoname" />'; 
                  $counter++; 
        } 
    echo ' 
       </d3> 
     </d1> 
     
     <d1> 
      <d2></d2> 
       <d3> 
         <input name="companyid" type="hidden" value="'.$_REQUEST['companyid'].'" /> 
         <input type="submit" name="uploadlogo" id="submit" value="Submit" /> 
       </d3> 
    </d1> 
    </div> 
    '; 
     
    }
    ?>

Thanks spikeZ… Code looks good… except it expects all the file fields to be populated before allowing the form to submit, but need to make sure atleast one of the fields holds a file and process.

cheers