PHP多文件上传

时间:2012-04-25 18:10:13

标签: php upload multifile

我有一个上传和一个多上传。 当我在6MB大小的文件上尝试时,单个上传工作。

如果我在6MB文件上尝试多文件上传,多个或只是1,它就不起作用。

有什么想法吗?我试着把时间设定为:

$messages = array();
$errors = array();

$upload_dir = '.';
if(isset($_FILES['file']['tmp_name'])){
    //Number of uploaded files.
    $num_files = count($_FILES['file']['tmp_name']);

    //loop over array of files
    for($i=0; $i<$num_files; $i++){

        //check if there is a file in the array.
        if(!is_uploaded_file($_FILES['file']['tmp_name'][$i])){
            //echo $_FILES['file']['tmp_name'][$i].': Upload Failed!';

        }else{
            //echo $_FILES['file']['tmp_name'][$i].': Upload good!';
            //lets insert the data into the DB.
            $filename = stripslashes($_FILES['file']['name'][$i]);
            $extension = getExtension($filename);
            $description = $_POST['description'][$i];

            if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
            {
                //print error message
                $error[$i] = $filename.': '.'Unknown file extension! ';

            }
            else
            {
                //get the size of the image in bytes
                //$_FILES['image']['tmp_name'] is the temporary filename of the file
                //in which the uploaded file was stored on the server
                $size=filesize($_FILES['file']['tmp_name'][$i]);


                //compare the size with the maxim size we defined and print error if bigger
                if ($size > MAX_SIZE*1024)
                {
                    //echo '<h1>You have exceeded the size limit of 100MB!</h1>';
                    $errors[$i]=$filename.': You have exceeded the size limit of 100MB';
                }

                //we will give an unique name, for example the time in unix time format
                $time = time()+$i;
                $image_name=$time.'.'.$extension;

                //the new name will be containing the full path where will be stored (images folder)
                $newname='D:\\uploads\\'.$image_name;
                //$newname=$image_name;
                //we verify if the image has been uploaded, and print error instead
                $copied = copy($_FILES['file']['tmp_name'][$i], $newname);
                if (!$copied)
                {
                    $messages[$i]= $filename.': '.'Copy unsuccessful!';
                    //echo 'Copy unsuccessful!';

                }else{
                    $messages[$i]= $filename.': '.'Copy successful!';
                    //get image information here.
                    $fileInfo = getimagesize($newname);
                    $mySize = $fileInfo[0];
                    $mimetype = image_type_to_mime_type($fileInfo[2]);
                    $dimensions = $fileInfo[3];
                    savePhotoInfo($image_name,$dimensions, $description,$mySize);
                    createThumb('D:\\uploads\\', 'D:\\uploads\\thumbnails\\', '180',$image_name);
                    //echo 'Copy successful!';
                }
            }
        }
    }

    echo 'Upload results:';
    echo '<br />';

    for($x=0;$x<count($messages);$x++){
        echo $messages[$x];
        echo '<br />';
    }

    echo 'Upload errors:';
    echo '<br />';
    for($x=0;$x<count($errors);$x++){
        echo $errors[$x];
        echo '<br />';
    }

}

1 个答案:

答案 0 :(得分:0)

你看看php.ini,上传大小的配置吗?