Multipart/form-data 500 Error while it works in other servers

Hi

This is my image upload controller:

    function uploadPhoto()
    {
    	$this->load->helper('url');
    	$this->load->model('photo_model', 'picManager');
    	$this->load->model('user_model', 'userManager');
    	
		$result = array();
		
		$user = $this->userManager->get($this->session->userdata("user_id"))->result_array();
		$user = $user[0];

     	$user_id = url_title($this->session->userdata('user_id'));
        
        if(DEMO_MODE == 1) {
	        $result["status"] = 500;
        } else {
	        if($user_id == null)
	        {
		        $result["status"] = 0;
	        } else {
	        
	        	$this->load->model("site_model");
	        	$settings = $this->site_model->get_website_settings()->result_array();
			    $settings = $settings[0];
	        
				$max_upload = $settings["upload_limit"];
				$user_photos = $this->picManager->count_user_photos($this->session->userdata("user_id"));
				
				if($user_photos >= $max_upload && $max_upload != 0) {
					$result["status"] = 998;
				} else {
		        
		        	// Check if the directory already exists
		        	if (!file_exists("./uploads/photos/" . $user_id . "/")) {
		        		mkdir("./uploads/photos/" . $user_id . "/");
		        		mkdir("./uploads/photos/" . $user_id . "/thumbnails/");
		        	}
		        	
		        	// Copy the file to the correct directory
					if (!empty($_FILES))
					{
						$nameFile 	= rand(0,999999).time();
					    $tempFile 	= $_FILES['upl']['tmp_name'];
					    $fileSize 	= $_FILES['upl']['size'];
					    $fileTypes 	= array('jpg','jpeg','png', 'JPG', 'JPEG', 'PNG'); // File extensions
					    $fileParts 	= pathinfo($_FILES['upl']['name']);
					    $targetPath = "./uploads/photos/" . $user_id . "/";
					    $targetPathThumb = $targetPath . "thumbnails/";
					    $targetPathEcho = "/uploads/photos/" . $user_id . "/";
					    $targetPathEchoThumb = "/uploads/photos/" . $user_id . "/thumbnails/";
					    $targetFile =  str_replace('//','/',$targetPath) . $nameFile . "." . $fileParts["extension"];
					    $targetFileThumb = str_replace('//','/',$targetPathThumb) . $nameFile . "." . $fileParts["extension"];
					    $targetFileEcho = str_replace('//','/',$targetPathEcho) . $nameFile . "." . $fileParts["extension"];
					    $targetFileEchoThumb = str_replace('//','/',$targetPathEchoThumb) . $nameFile . "." . $fileParts["extension"];
					
						if($fileSize <= 7000000)
						{
						    if (in_array($fileParts['extension'],$fileTypes)) {
						    	// Send the file
								$file = $this->compress_image($tempFile, $targetFile, 100);
								
								$thumbWidth = 800;
								
								// Create the thumbnail
								$img = imagecreatefromjpeg( $file );
								$width = imagesx( $img );
								$height = imagesy( $img );
								
								// calculate thumbnail size
								$new_width = $thumbWidth;
								$new_height = floor( $height * ( $thumbWidth / $width ) );
								
								// create a new temporary image
								$tmp_img = imagecreatetruecolor( $new_width, $new_height );
								
								// copy and resize old image into new image
								imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
								
								// save thumbnail into a file
								imagejpeg( $tmp_img, $targetFileThumb );
								
								if($user["gender"] == 1) {
				                	$word_gender = "her";
			                	} else {
				                	$word_gender = "his";
			                	}
								
								$this->load->model('action_model', 'actionModel');
								$this->actionModel->add($this->session->userdata('user_id'), 17, "<a href='" . base_url("user/profile/" . $this->session->userdata('user_id')) . "'>" . $this->session->userdata('user_username') . "</a> uploaded new photos on " . $word_gender . " profile.", base_url("user/profile/" . $this->session->userdata('user_id')), "<i class='fa fa-picture-o'></i>");
								
								// Add picture in the DB 
								$pic_id = $this->picManager->add($targetFileEcho, $targetFileEchoThumb, $this->session->userdata('user_id'));
								$result["status"] 	= 1; 
								$result["photo"] 	= $targetFileEcho;
								$result["id"]		= $pic_id; 
								
								// Count user photos
								$nb_user_photos = $this->picManager->count_user_photos($this->session->userdata('user_id'));
								
								// User has just one photo, set it as the profile picture
								if($nb_user_photos == 1) {
									$this->userManager->update_info($this->session->userdata('user_id'), array("main_photo" => $pic_id));
									
									$this->session->set_userdata(
										array(
											"user_avatar"	=> $targetFileEchoThumb,
										)
									);
								}
								
						    } else {
						        $result["status"] = 0;
						    }
					    }
					} 
				}
			}
		}
		
		header('Content-type: application/json;');
        echo json_encode($result);
    }

while this is my form:

    <div class="row">
	    <div class="main_container clearfix">
			<div class="photo_area clearfix">
	    		<div class="photoblock col-md-12 clearfix">
	    			<div class="add_photos clearfix">
						<form id="upload" method="post" action="<?php echo base_url() ?>photo/uploadPhoto" enctype="multipart/form-data" class="clearfix">
				            <fieldset>
				            	<legend><?php echo $this->lang->line("select_photos_to_upload"); ?></legend>
				            </fieldset>
				            <div id="drop">
				             	<a class="btn btn-primary"><?php echo $this->lang->line("select_some_photos"); ?></a>
				                <input type="file" name="upl" multiple />
				            </div>
				            			
				            <ul class="wait_list">
				                <!-- The file uploads will be shown here -->
				            </ul>
				
				        </form>
				        
	    			</div>
	    		</div>

and this is my js:

(document).ready(function() {
	
	$(".btn-save-photos").click(function(e) {
		e.preventDefault();
	
		var photo_array = [];
	
		$(".photo_line").each(function() {
		
			var photo = $(this).find(".photo_url").attr("data-id");
	    	var title = $(this).find(".title_img").val();
	    	var tags = $(this).find(".tags_img").val();
		
			// Create the info_array
	    	var info_array = {
	        	photo_id : photo,
	        	title : title,
	    	};
	    	
	    	// Add the info_array to the global array
	    	photo_array.push(info_array);
		});
		
		$.ajax({ 
            url: base_url + "photo/save_photos_infos",
            type: "POST",
            dataType: "json",
            data: {photo_array: photo_array},
            success: function(data) {
				window.location.href = base_url + "user/settings?action=photos_added";
            }
        });   
	});

and

    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),
        limitMultiFileUploads: 5,
        sequentialUploads: true,

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {
        				
			if(data.files[0]['type'] != 'image/png' && data.files[0]['type'] != 'image/jpg' && data.files[0]['type'] != 'image/jpeg'){ alert("Whoops! You can only add photos with the .PNG or .JPG extension."); return; }
			
            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
               
			if(data.files[0].size > 7000000)
			{
				alert(photo_weight_error_str);
				return;
			}

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });
            
            var t = "";
            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit().success(function(result, textStatus, jqXHR){
            	
            	if(result.status == 1)
            	{
                        	            	
	            	$(".no-photo-yet").hide();
			            	
	            	var nb_photo_line = $(".photo_line").length+1;
	            	
	            	// We create the table of images for the informations (title, tags...)
	            	var photo_block = "<tr class='photo_line' data-cpt='" + nb_photo_line + "'>";
	            	photo_block += '<td style="width:35%;"><img class="col-md-12 photo_url" data-id="' + result.id + '" src="' + base_url + result.photo + '" class="img-responsive" /></td>';
	            	photo_block += '<td class="infos_photo">';
	            	photo_block += '<div class="form-group form-title">';
	            	photo_block += '<label for="inputTitle">' + description_str + ' :</label>';
	            	photo_block += '<textarea class="form-control title_img" id="inputTitle" placeholder="' + description_placeholder_str + '"></textarea>';
					photo_block += '</div>';
					photo_block += '</td>';
					photo_block += '</tr>';
	            	
	            	$(".img_list").append(photo_block);
	            		
	            	$(".img_list").fadeIn();
	            	$(".buttons_send_photos").fadeIn();
            	
            	} else if(result.status == 500) {
	            	alert(demo_mode_str);
            	} else if(result.status == 998) {
	            	alert(upload_limit_reached_str);
            	}
				
			});
			
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }

In the google console it shows error 500 however, Its not showing any error in logs,

When I click networks and preview in google console it does show 500 but nothing more.

this works in other servers but in one of the servers of my clients, It does not work.

comment-out all the code and comment-in it line by line until you find out where the error occurs.

The most efficient way to debug a problem. I do this all the time. I place a var_dump() line after every line of code. It is the best solution ever. Just forget about smart debuggers echo and var dumps after every line of code are the most efficient means of solving any problem.

Is the session object a custom handler for storing sessions in the database? On the servers where it’s not working, what version of PHP are they running?

Why is var_dump more efficient than xdebug to you?

That was my attempt at online sarcasm.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.