Jump to content

Couldn't Fetch Mysqli - Avatar Upload Script


Glese

Recommended Posts

I am getting the error message "Couldn't fetch mysqli on line 48".

 

Here's the script:

 

By the way the uploading of the first file does work, but the upload of the second file so it updates and refreshes with a new avatar file name does not work.

 

<?php
// This file gets included into profile_content.php



// AVATAR code 
define ('AVATAR_UPLOADPATH', 'avatar/');
define ('AVATAR_MAXFILESIZE', '32768');




// _UPLOAD_ and _MOVE_ avatar to target location - START
if (isset($_POST['submit'])) {

$avatar = $_FILES['avatar_upload']['name'];
$avatar_type = $_FILES['avatar_upload']['type'];
$avatar_size = $_FILES['avatar_upload']['size'];

	// file type + file size + file upload + width & height VALIDATION
	if ((($avatar_type == 'image/gif') || ($avatar_type == 'image/jpeg') ||
		($avatar_type == 'image/pjeg') || ($avatar_type == 'image/png') &&
		($avatar_size > 0) && ($avatar_size <= AVATAR_MAXFILESIZE))) {
			if ($_FILES['avatar_upload']['error'] == 0) {

			list($width, $height, $type, $attr) = getimagesize($_FILES['avatar_upload']['tmp_name']);

			if ($width == 64 && $height == 64) { 

                                    
                                    
                                
                              
                                
                                

                                // UPLOAD + MOVE
                                
                                    
                                   

                                
//////////////////////// HERE IS THE LINE  /////////////////////////////////////////////
                                // Update the file name
                                
                                $query3 = "UPDATE user SET avatar = '$avatar' WHERE user_id = '$user_id'";

                                $row3 = mysqli_query ($dbc, $query3) or die (mysqli_error($dbc));
                                
                              
                                                                                          
                                // Move the uploaded file on the disk to its folder
                                
			move_uploaded_file ($_FILES['avatar_upload']['tmp_name'], $target . $avatar);

                               
                                
                                
                                
                                // Rename the file into a more usable file name
                                    
                                $avatar = rename($avatar, $user_name . '_' . rand(111111, 999999)); 
                                
                                // Success 
                                
			echo "Your avatar has been successfully uploaded. Please refresh the page to see the changes.";


                                
                            
                                
                                
                                
	// error messages		

					// width and height error
					} else {
						echo "Your avatar has to have a width and height of 64 pixels, please crop it or use a different avatar.";
					}

			// file upload error		
			} else {
				echo "Error: " . $_FILES['avatar_upload']['error']; 
				}
	// file type error
	 } else { 
		echo "The avatar must be a GIF, JPEG or PNG image file and no greater than " . AVATAR_MAXFILESIZE / 1024 .
				" KB in size.";
		}
			} 
// END


?>

 

Any ideas why I am getting the error message. I marked the corresponding line with ///// HERE IS THE LINE //// in the mid area.

Link to comment
Share on other sites

LOL .... I missed that one, I must have deleted it out of accident.

 

But now I have a different problem, the file gets uploaded and renamed, but it does not get moved into the folder "avatar" even though the target is right, can you see what the issue is, I am not seeing much wrong with it, even though, there must be a mistake somewhere.

 

Here's the updated code:

 

<?php
// This file gets included into profile_content.php



// AVATAR code 
define ('AVATAR_UPLOADPATH', '/avatar/');
define ('AVATAR_MAXFILESIZE', '32768');

$target = AVATAR_UPLOADPATH; 


// _UPLOAD_ and _MOVE_ avatar to target location - START
if (isset($_POST['submit'])) {

$avatar = $_FILES['avatar_upload']['name'];
$avatar_type = $_FILES['avatar_upload']['type'];
$avatar_size = $_FILES['avatar_upload']['size'];

	// file type + file size + file upload + width & height VALIDATION
	if ((($avatar_type == 'image/gif') || ($avatar_type == 'image/jpeg') ||
		($avatar_type == 'image/pjeg') || ($avatar_type == 'image/png') &&
		($avatar_size > 0) && ($avatar_size <= AVATAR_MAXFILESIZE))) {
			if ($_FILES['avatar_upload']['error'] == 0) {

			list($width, $height, $type, $attr) = getimagesize($_FILES['avatar_upload']['tmp_name']);

			if ($width == 64 && $height == 64) { 

                                    
                                    
                                
                              
                                
                                

                                // UPLOAD + MOVE
                                
                                    
                                   

                                

                                // Update the file name
                                    
                                $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);   
                                    
                                $query3 = "UPDATE user SET avatar = '$avatar' WHERE user_id = '$user_id'";

                                $row3 = mysqli_query ($dbc, $query3) or die (mysqli_error($dbc));
                                
                              
                                
                                 // Rename the file into a more usable file name
                                    
                                $avatar = rename($_FILES['avatar_upload']['tmp_name'], $user_name . '_' . rand(111111, 999999)); 
                              
        
                                                                                          
                                // Move the uploaded file on the disk to its folder
                                
			move_uploaded_file ($avatar, $target);

                               
                                
                                
                                
                               
                                // Success 
                                
			echo "Your avatar has been successfully uploaded. Please refresh the page to see the changes.";


                                
                            
                                
                                
                                
	// error messages		

					// width and height error
					} else {
						echo "Your avatar has to have a width and height of 64 pixels, please crop it or use a different avatar.";
					}

			// file upload error		
			} else {
				echo "Error: " . $_FILES['avatar_upload']['error']; 
				}
	// file type error
	 } else { 
		echo "The avatar must be a GIF, JPEG or PNG image file and no greater than " . AVATAR_MAXFILESIZE / 1024 .
				" KB in size.";
		}
			} 
// END


?>

Link to comment
Share on other sites

Upload folder is located at:

 

xampp/php_projects/myproject/controller/profile/avatar

 

The upload script is located in the profile folder and everything else is there too, including the upload form, which is on the profile page.

 

The image does get uploaded, but it does get uploaded into the profile folder and not into the avatar folder as it is supposed to.

 

Link to comment
Share on other sites

You are saying change it to the right path, but in my perspective it is the right path, since the folder avatar is contained in the same folder as the upload  script, and an absolute path e.g.:

 

php_projects/myproject/controller/profile/avatar

 

does not work for me, probably because of a misconfiguration of the URL's.

Link to comment
Share on other sites

I turned on the error reporting as you suggested and this is the most recent error I am getting:

 

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in 

 

I used a directory as the second argument which is not allowed and does work, I fixed that problem and

the problem is now solved, the script works as expected, thanks for all the help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.