liamdawe Posted July 27, 2009 Share Posted July 27, 2009 Basically my image upload seems to work fine, it uploads the image in question ive tested that a few times, but it seems to then return with a blank error message. Here is my code: function upload_item($item_id) { global $site_config, $db_pxs, $lang, $pxs, $error; $error = ''; // first we check if it is allowed $allowed_extensions = array('image/jpeg'); if (in_array($_FILES['new_upload']['type'], $allowed_extensions)) { // check file size not bigger than 1.47mb if ($_FILES['new_upload']['size'] > 1542000) { $error = 'Sorry file is too big!'; return false; } // THE MAIN FILE // give the file a random file name $filename = rand() . 'id' . $item_id . $_FILES['new_upload']['name']; // the actual file $source = $_FILES['new_upload']['tmp_name']; // where to upload to $target = "uploads/" . $filename; // if (move_uploaded_file($source, $target)) { return $filename; } else { $error = 'Could not upload file, please contact the admin!'; return false; } } else { $error = 'Wrong file type, allowed to upload .jpg files only.'; return false; } } The image appears in my upload directory fine. I don't see why it gives a blank error, i set only 3 error messages possible? Here is the checking code: // initial screenshot if ($_FILES['new_upload']['error'] == UPLOAD_ERR_OK) { if (upload_item($db_pxs->last_id()) == false) { $pxs->admin_error($error); } $screenshot_sql = "INSERT INTO `item_screenshots` SET `item_id` = {$db_pxs->last_id()}, `image` = '{$filename}'"; $query_shot = $db_pxs->query($screenshot_sql); } It doesn't seem to get to the screenshot sql, giving a blank error message above , yet it does upload the file :S Any help would be great! Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/ Share on other sites More sharing options...
patrickmvi Posted July 27, 2009 Share Posted July 27, 2009 Is the code in your second block in a function also? If so, you need to make sure that $error is set as global there as well. Besides that, you should try and echo some troubleshooting text at different stages of your upload_item function to try and figure out where it's dying. Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883892 Share on other sites More sharing options...
liamdawe Posted July 27, 2009 Author Share Posted July 27, 2009 The other block is not in a function no it prints out the other error messages fine. I've tried to do some debugging but i cannot figure it out so i posted here. Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883894 Share on other sites More sharing options...
WolfRage Posted July 27, 2009 Share Posted July 27, 2009 It returns a blank error message because there is no error and you initialized it as blank. "$error='';" instead set it to NULL and check to see if it is set. <?php $error=NULL; if(isset($error)) { //We have an error so print it and do something. } ?> Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883927 Share on other sites More sharing options...
liamdawe Posted July 27, 2009 Author Share Posted July 27, 2009 Actually it will only show the error if this happens: if (upload_item($db_pxs->last_id()) == false) The only times it is returned false is when there is an a error message set. Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883933 Share on other sites More sharing options...
WolfRage Posted July 27, 2009 Share Posted July 27, 2009 So obviously you are encountering an error so set your error message. <?php if (upload_item($db_pxs->last_id()) == false) { $error=$pxs->admin_error($error); } ?> Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883942 Share on other sites More sharing options...
liamdawe Posted July 27, 2009 Author Share Posted July 27, 2009 That will do nothing, "admin_error" outputs a css error box "$error" is the error message. I do appreciate the help, but like i said it already works for the other error messages like i stated. Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883949 Share on other sites More sharing options...
liamdawe Posted July 27, 2009 Author Share Posted July 27, 2009 I found the error, i changed the uploader code to this: function upload_item($item_id) { global $site_config, $db_pxs, $lang, $pxs, $error, $filename; $error = ''; // first we check if it is allowed $allowed_extensions = array('image/jpeg'); if (in_array($_FILES['new_upload']['type'], $allowed_extensions)) { // check file size not bigger than 1.47mb if ($_FILES['new_upload']['size'] > 1542000) { $error = 'Sorry file is too big!'; return false; } // THE MAIN FILE // give the file a random file name $filename = rand() . 'id' . $item_id . $_FILES['new_upload']['name']; // the actual file $source = $_FILES['new_upload']['tmp_name']; // where to upload to $target = "uploads/" . $filename; // if (move_uploaded_file($source, $target)) { return true; } else { $error = 'Could not upload file, please contact the admin!'; return false; } } else { $error = 'Wrong file type, allowed to upload .jpg files only.'; return false; } } Link to comment https://forums.phpfreaks.com/topic/167617-image-uploader-uploads-but-still-gives-error/#findComment-883958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.