1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Multiple File upload - need help with validating file size

Discussion in 'PHP' started by izbryte, Mar 5, 2012.

  1. #1
    I have this validation script for a multiple file upload form and it works great until I add in the validation for the max file size. This doesn't seem to work at all. I'm at the point, I'm ready to pull my hair out and I'm willing to pay someone to help if necessary.
    Here's my code:
    if (isset($_POST['Submit'])) {
    	$user_id = $userdata[user_id];
        $number_of_file_fields = 0;
        $number_of_uploaded_files = 0;
        $moved_files = 0;
        $uploaded_files = array();
    	$max_filesize = 5242880; // Maximum filesize in BYTES (currently 5MB).
        $upload_directory = dirname(__file__) . '/'.$user_id.'/'; //set upload directory
    	if (!is_dir($upload_directory)) {
        mkdir($upload_directory, 0777, true);
    	}
    
        for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
            $number_of_file_fields++;
            if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
                $number_of_uploaded_files++;
    			if($_FILES['images']['size'][$i] > $max_filesize){
    				echo "<b class='red'>Max file size is 5MB.</b><br/>";		
    				$sz = false;
    			}else{ $sz = true;}
    			$ext = validate_extension($_FILES['images']['name'][$i]);
    			if (($ext == true) && ($sz == true)){
    				$uploaded_files[] = $_FILES['images']['name'][$i];		
    				if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
    					$moved_files++;
               		 }
    			}else { 
    			echo "<b class='red'>File extention error. Only .doc, .pdf, .jpg and .gif files are allowed. </b><br/>";
    			}
    
            }
    
        }
    	if ($number_of_uploaded_files >= 1){
    		echo "Number of files submitted:<b class='red'>".$number_of_uploaded_files."</b><br/>Number of successfully uploaded files:<b class='red'>".$moved_files."</b><br/><br/>Uploaded File Name(s):<br/>" . implode('<br/>', $uploaded_files) .', '. implode('<br/>', $uploaded_file_size);
    	}
    
    
    }
    PHP:

     
    izbryte, Mar 5, 2012 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    maybe the files you uploaded are really over the max file size?
     
    JohnnySchultz, Mar 6, 2012 IP
  3. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #3
    this can be an issue of host. check the max_upload_limit in php.ini details. it really can be a pain in ass ,as was for my case
     
    arpit13, Mar 6, 2012 IP
    atari likes this.
  4. earnnet

    earnnet Member

    Messages:
    100
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #4
    You can try to do some modifications in .htaccess. See wether it's allowed or not.
     
    earnnet, Mar 6, 2012 IP