为什么我的上传表格不会使用php上传超过2.1mb的文件?

时间:2012-06-19 06:30:40

标签: php forms upload

我正在使用效果很好的上传脚本:

ini_set('memory_limit', '256M');

function uploadImage($files_name, $files_tmp_name, $files_error, $files_type, $uploaded_photos_array, $image_type, $replace_position='69') { //Checks if the file uploaded correctly, saves the folder name to the DB, and calls the resizeAndPlaceFile 3 times     
    global $address;

    //If the directory doesn't exist, create it
    if (!is_dir('../images/uploads/temp/'.$address)) {
        mkdir('../images/uploads/temp/'.$address);
    }

    $myFile_original = $files_name; //Store the filename into a variable

    //Change the filename so it is unique and doesn't contain any spaces and is all lowercase
    $myFile = str_replace(' ', '_', $myFile_original); //change all spaces to underscores within a file name
    $myFile = strtolower($myFile); //Make all characters lowercase
    //$anyNum = rand(20,500789000); //Generate a random number between 20 and 500789000  
    //$newFileName = $anyNum.'_'.$myFile; //Combine the random number with the filename to create a unique filename
    $newFileName = $myFile;

    $info = pathinfo($newFileName); //Finds the extension of the filename
    $directory_name =  basename($newFileName,'.'.$info['extension']); //Removes the extension from the filename to use as the name of the directory

    $folder = '../images/uploads/temp/'.$address.'/'.$directory_name.'/'; //Folder to upload to

    //If the directory doesn't exist, create it
    if (!is_dir($folder)) {
        mkdir($folder);
    }


    //===Check if the File already exists======== 
    if (file_exists($folder.'large.jpg')) {
        echo $myFile_original." already exists.";
    } //******If file already exists in your Folder, It will return zero and Will not take any action===

    else { //======Otherwise File will be stored in your given directory and Will store its name in Database===


        //copy($_FILES['fileField']['tmp_name'],$folder.$newFileName); //===Copy File Into your given Directory,copy(Source,Destination)


        // Check if file was uploaded ok
        if(!is_uploaded_file($files_tmp_name) || $files_error !== UPLOAD_ERR_OK) {
            exit('There was a problem uploading the file. Please try again.');
        } else {
            /*
            $sql = 'INSERT into tblfileupload SET
                                file_name = "'.$folder.'"'; 
                        $result = $conn->query($sql) or die($conn->error);

                        if($result > 0) { //====$res will be greater than 0 only when File is uploaded Successfully====:)
                            echo 'You have Successfully Uploaded File';
                        }
            */


            if(!function_exists('resizeAndPlaceFile')){
                function resizeAndPlaceFile($image_size, $files_tmp_name, $files_type, $folder) { //Resizes the uploaded file into different sizes

                    //echo '<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />The resizeAndPlaceFile function is being called!';

                    // Create image from file
                    switch(strtolower($files_type)) {
                        case 'image/jpeg':
                            $image = imagecreatefromjpeg($files_tmp_name);
                            break;
                        case 'image/png':
                            $image = imagecreatefrompng($files_tmp_name);
                            break;
                        case 'image/gif':
                            $image = imagecreatefromgif($files_tmp_name);
                            break;
                        default:
                            exit('Unsupported type: '.$files_type);
                    }

                    // Get current dimensions
                    $old_width  = imagesx($image);
                    $old_height = imagesy($image);

                    // Target dimensions for large version
                    switch($image_size) {
                        case 'large':
                            $max_width = '600'; //Large Photo (Listing Page)
                            break;
                        case 'medium':
                            $max_width = '157'; //Medium Photo (Dashboard)
                            break;
                        case 'thumbnail':
                            $max_width = '79'; //Small Photo (Listing Page - Thumbnail)
                            break;
                    }

                    if($max_width > $old_width) {
                        $max_width = $old_width;
                    }

                    $max_height = ($old_height/$old_width)* $max_width;


                    // Get current dimensions
                    $old_width  = imagesx($image);
                    $old_height = imagesy($image);

                    // Calculate the scaling we need to do to fit the image inside our frame
                    $scale = min($max_width/$old_width, $max_height/$old_height);

                    // Get the new dimensions
                    $new_width  = ceil($scale*$old_width);
                    $new_height = ceil($scale*$old_height);


                    // Create new empty image
                    $new = imagecreatetruecolor($new_width, $new_height);

                    // Resize old image into new
                    imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

                    //Output the image to a file
                    imagejpeg($new, $folder.$image_size.'.jpg',100);

                    // Destroy resources
                    imagedestroy($image);
                    imagedestroy($new);

                } //end function resizeAndPlaceFile
            } //end if !function_exists['resizeAndPlaceFile']
            resizeAndPlaceFile('large',$files_tmp_name, $files_type, $folder); //Large Photo (List Page)
            resizeAndPlaceFile('medium',$files_tmp_name, $files_type, $folder); //Medium Photo (Dashboard)
            resizeAndPlaceFile('thumbnail',$files_tmp_name, $files_type, $folder); //Small Photo (List Page - Thumbnail)

            if($image_type == 'replace') { //If this is being run for a replace, then replace one of the values instead of adding it to the end of the array
                $uploaded_photos_array[$replace_position] = $folder; //This replaces the value of the old image with the new image
            } else if($image_type == 'initial') { //otherwise, add it to the end of the array
                array_push($uploaded_photos_array,$folder);
            }

            return $uploaded_photos_array;
        } //end else         
    } //end else
} //end function uploadImage

当我尝试上传任何超过2.1mb的文件时,它不会上传文件,也不会显示任何错误,所以我不知道为什么它不起作用。为什么我的上传表格不能使用php上传超过2.1mb的文件?

3 个答案:

答案 0 :(得分:3)

检查并更改以下php.ini说明:

memory_limit = 32M
upload_max_filesize = 10M
post_max_size = 20M

答案 1 :(得分:1)

在php.ini中更新这些参数

upload_max_filesize
post_max_size

答案 2 :(得分:0)

您无法从php脚本更改最大文件上传大小。你应该在php.ini文件中更改为 upload_max_filesize = 30M post_max_size = 30M

有些主机不允许更改php.ini(在共享主机的情况下),你应该使用.htaccess并使用以下 php_value upload_max_filesize 30M