Glese Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/ Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 Did 'ya set up the DB connect correctly? Where is $dbc defined? Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292424 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292429 Share on other sites More sharing options...
Spring Posted November 30, 2011 Share Posted November 30, 2011 Ok. $target = AVATAR_UPLOADPATH; What is AVATAR_UPLOADPATH; set to then? Ah I see it.. So..where is this file located? Is it in the root directory? Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292431 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292433 Share on other sites More sharing options...
Spring Posted November 30, 2011 Share Posted November 30, 2011 Any errors? error_reporting(E_ALL); ini_set('display_errors', '1'); Maybe it's your permissions? Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292434 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 Looks to me like your path is /avatar/ (at the root of the disk). I'm thinking that isn't right. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292436 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 I did remove the beginning slash, and it still does not work, as a notice, the avatar folder is located in the folder profile. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292444 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 What's the full path to that directory, and what is the value of $_SERVER['DOCUMENT_ROOT']? Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292457 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 The value of the server function is: C:/xampp/htdocs (Which is also the localhost address). The full path of the directory is: c:/xampp/htdocs/php_projects/myproject/controller/profile/avatar Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292459 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 All you should need to do is change your AVATAR_UPLOADPATH constant to the right path. You should also be developing with error_reporting = -1 and display_errors = On in your php.ini file to make problems like this easier to diagnose. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292463 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292466 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 Your constant, according to the updated code you posted, is assigned the value /avatar/. If it's something else, please post the revised code. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292471 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252080-couldnt-fetch-mysqli-avatar-upload-script/#findComment-1292474 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.