每个上传多张照片并将它们分配给数据库中的一个用户ID

i am building an ad site that allows users to upload multiple photos per ad, how do i get this to work? i am new to php

<?php echo $error_msg; ?>

<?php
    include_once "connection.php";

    if(isset($_FILES['files'])){

    $errors= array();

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  

        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }

        $sql = mysql_query("INSERT INTO upload_data (`postid`,`file_name`,`file_size`,`file_type`) VALUES('$postid','$file_name','$file_size','$file_type')")or die (mysql_error());

        $desired_dir="uploads";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir);                
            }
            mysql_query($sql);          
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
    echo "Success";
        }
    }
?>

The most common problem encountered when trying to upload multiple files is the upload_max_filesize and the max_post_size in your php.ini file.

If you have no problems uploading a series of very small images, then this will be your problem.

This quesion: php file uploads' size discusses the issue effectively.

You should also consider echoing the $_FILES['files']['error'] which will often be instructive. The php manual explains the codes.

Don't forget to check that you have included the correct call in your HTML FORM element too:

<form method='POST' enctype='multipart/form-data' action='myfile.php'>

Make sure you have not specified an undersized max_file_size in your HTML form too.

I suggest that you don't prohibit large file uploads but rather you resize any overlarge images in your script. This will help your users substantially.

I hope this helps.