tobimichigan Posted January 22, 2011 Share Posted January 22, 2011 Code Gurus' Salute, Please I wanna catch this image upload exception without it bringing out this: "Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in C:\xampp\htdocs", Actually, I set the image size to be 32kb abd 120x120 pix. If anything varies from the above description it would not be uploaded, but it brings out "Your picture must be a GIF, JPEG, or PNG image file no greater than 32 KB and 120x120 pixels in size." alongside with that above error. Please gents how do I implement this exception without that warning signal? Quote Link to comment Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Can we see some relevant code? Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted January 22, 2011 Author Share Posted January 22, 2011 ok here we go: edit.php <?php // Start the session require_once('startsession.php'); // Insert the page header $page_title = 'Edit Profile'; require_once('header.php'); require_once('appvars.php'); require_once('connectvars.php'); // Make sure the user is logged in before going any further. if (!isset($_SESSION['user_id'])) { echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>'; exit(); } // Show the navigation menu require_once('navmenu.php'); // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (isset($_POST['submit'])) { // Grab the profile data from the POST $first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname'])); $last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname'])); $gender = mysqli_real_escape_string($dbc, trim($_POST['gender'])); $birthdate = mysqli_real_escape_string($dbc, trim($_POST['birthdate'])); $city = mysqli_real_escape_string($dbc, trim($_POST['city'])); $state = mysqli_real_escape_string($dbc, trim($_POST['state'])); $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture'])); $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name'])); $new_picture_type = $_FILES['new_picture']['type']; $new_picture_size = $_FILES['new_picture']['size']; list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']); $error = false; // Validate and move the uploaded picture file, if necessary if (!empty($new_picture)) { if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') || ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) && ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) { if ($_FILES['file']['error'] == 0) { // Move the file to the target upload folder $target = MM_UPLOADPATH . basename($new_picture); if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) { // The new picture file move was successful, now make sure any old picture is deleted if (!empty($old_picture) && ($old_picture != $new_picture)) { @unlink(MM_UPLOADPATH . $old_picture); } } else { // The new picture file move failed, so delete the temporary file and set the error flag @unlink($_FILES['new_picture']['tmp_name']); $error = true; echo '<p class="error">Sorry, there was a problem uploading your picture.</p>'; } } } else { // The new picture file is not valid, so delete the temporary file and set the error flag @unlink($_FILES['new_picture']['tmp_name']); $error = true; echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) . ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>'; } } // Update the profile data in the database if (!$error) { if (!empty($first_name) && !empty($last_name) && !empty($gender) && !empty($birthdate) && !empty($city) && !empty($state)) { // Only set the picture column if there is a new picture if (!empty($new_picture)) { $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " . " birthdate = '$birthdate', city = '$city', state = '$state', picture = '$new_picture' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } else { $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " . " birthdate = '$birthdate', city = '$city', state = '$state' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } mysqli_query($dbc, $query); // Confirm success with the user echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php">view your profile</a>?</p>'; mysqli_close($dbc); exit(); } else { echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>'; } } } // End of check for form submission else { // Grab the profile data from the database $query = "SELECT first_name, last_name, gender, birthdate, city, state, picture FROM mismatch_user WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $row = mysqli_fetch_array($data); if ($row != NULL) { $first_name = $row['first_name']; $last_name = $row['last_name']; $gender = $row['gender']; $birthdate = $row['birthdate']; $city = $row['city']; $state = $row['state']; $old_picture = $row['picture']; } else { echo '<p class="error">There was a problem accessing your profile.</p>'; } } mysqli_close($dbc); ?> <table> <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <tr> <td> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" /> </td> </tr> <fieldset> <tr> <td> <legend>Personal Information</legend> </td> </tr> <tr> <td> <label for="firstname">First name:</label> </td> </tr> <tr> <td> <input type="text" id="firstname" name="firstname" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br /> </td> </tr> <tr> <td> <label for="lastname">Last name:</label> </td> </tr> <tr> <td> <input type="text" id="lastname" name="lastname" value="<?php if (!empty($last_name)) echo $last_name; ?>" /><br /> </td> </tr> <tr> <td> <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option> <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option> </select><br /> </td> </tr> </label> <label for="birthdate">Birthdate:</label> </td> </tr> <tr> <td> <input type="text" id="birthdate" name="birthdate" value="<?php if (!empty($birthdate)) echo $birthdate; else echo 'YYYY-MM-DD'; ?>" /><br /> </td> </tr> <tr> <td> <label for="city">City:</label> </td> </tr> <tr> <td> <input type="text" id="city" name="city" value="<?php if (!empty($city)) echo $city; ?>" /><br /> </td> </tr> <tr> <td> <label for="state">State:</label> </td> </tr> <tr> <td> <input type="text" id="state" name="state" value="<?php if (!empty($state)) echo $state; ?>" /><br /> </td> </tr> <input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" /> <tr> <td> <label for="new_picture">Picture:</label> </td> </tr> <tr> <td> <input type="file" id="new_picture" name="new_picture" /> </td> </tr> <?php if (!empty($old_picture)) { echo '<img class="profile" src="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture" />'; } ?> </fieldset> <tr> <td> <input type="submit" value="Save Profile" name="submit" /> </td> </tr> </form> </table> <?php // Insert the page footer require_once('footer.php'); ?> Whenever it brings out this error "Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in C:\xampp\htdocs" it refers to line 36 => <?php list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Change.... if (isset($_POST['submit'])) { to.... if (isset($_POST['submit']) && isset($_FILES['new_picture']['tmp_name'])) { Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted January 22, 2011 Author Share Posted January 22, 2011 Thorpe, her's my modified code: <?php // Start the session require_once('startsession.php'); // Insert the page header $page_title = 'Edit Profile'; require_once('header.php'); require_once('appvars.php'); require_once('connectvars.php'); // Make sure the user is logged in before going any further. if (!isset($_SESSION['user_id'])) { echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>'; exit(); } // Show the navigation menu require_once('navmenu.php'); // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (isset($_POST['submit']) && isset($_FILES['new_picture']['tmp_name'])) { // Grab the profile data from the POST $first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname'])); $last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname'])); $gender = mysqli_real_escape_string($dbc, trim($_POST['gender'])); $birthdate = mysqli_real_escape_string($dbc, trim($_POST['birthdate'])); $city = mysqli_real_escape_string($dbc, trim($_POST['city'])); $state = mysqli_real_escape_string($dbc, trim($_POST['state'])); $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture'])); $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name'])); $new_picture_type = $_FILES['new_picture']['type']; $new_picture_size = $_FILES['new_picture']['size']; list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']); $error = false; // Validate and move the uploaded picture file, if necessary if (!empty($new_picture)) { if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') || ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) && ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) { if ($_FILES['file']['error'] == 0) { // Move the file to the target upload folder $target = MM_UPLOADPATH . basename($new_picture); if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) { // The new picture file move was successful, now make sure any old picture is deleted if (!empty($old_picture) && ($old_picture != $new_picture)) { @unlink(MM_UPLOADPATH . $old_picture); } } else { // The new picture file move failed, so delete the temporary file and set the error flag @unlink($_FILES['new_picture']['tmp_name']); $error = true; echo '<p class="error">Sorry, there was a problem uploading your picture.</p>'; } } } else { // The new picture file is not valid, so delete the temporary file and set the error flag @unlink($_FILES['new_picture']['tmp_name']); $error = true; echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) . ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>'; } } // Update the profile data in the database if (!$error) { if (!empty($first_name) && !empty($last_name) && !empty($gender) && !empty($birthdate) && !empty($city) && !empty($state)) { // Only set the picture column if there is a new picture if (!empty($new_picture)) { $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " . " birthdate = '$birthdate', city = '$city', state = '$state', picture = '$new_picture' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } else { $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " . " birthdate = '$birthdate', city = '$city', state = '$state' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } mysqli_query($dbc, $query); // Confirm success with the user echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php">view your profile</a>?</p>'; mysqli_close($dbc); exit(); } else { echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>'; } } } // End of check for form submission else { // Grab the profile data from the database $query = "SELECT first_name, last_name, gender, birthdate, city, state, picture FROM mismatch_user WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $row = mysqli_fetch_array($data); if ($row != NULL) { $first_name = $row['first_name']; $last_name = $row['last_name']; $gender = $row['gender']; $birthdate = $row['birthdate']; $city = $row['city']; $state = $row['state']; $old_picture = $row['picture']; } else { echo '<p class="error">There was a problem accessing your profile.</p>'; } } mysqli_close($dbc); ?> <table> <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <tr> <td> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" /> </td> </tr> <fieldset> <tr> <td> <legend>Personal Information</legend> </td> </tr> <tr> <td> <label for="firstname">First name:</label> </td> </tr> <tr> <td> <input type="text" id="firstname" name="firstname" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br /> </td> </tr> <tr> <td> <label for="lastname">Last name:</label> </td> </tr> <tr> <td> <input type="text" id="lastname" name="lastname" value="<?php if (!empty($last_name)) echo $last_name; ?>" /><br /> </td> </tr> <tr> <td> <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option> <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option> </select><br /> </td> </tr> </label> <label for="birthdate">Birthdate:</label> </td> </tr> <tr> <td> <input type="text" id="birthdate" name="birthdate" value="<?php if (!empty($birthdate)) echo $birthdate; else echo 'YYYY-MM-DD'; ?>" /><br /> </td> </tr> <tr> <td> <label for="city">City:</label> </td> </tr> <tr> <td> <input type="text" id="city" name="city" value="<?php if (!empty($city)) echo $city; ?>" /><br /> </td> </tr> <tr> <td> <label for="state">State:</label> </td> </tr> <tr> <td> <input type="text" id="state" name="state" value="<?php if (!empty($state)) echo $state; ?>" /><br /> </td> </tr> <input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" /> <tr> <td> <label for="new_picture">Picture:</label> </td> </tr> <tr> <td> <input type="file" id="new_picture" name="new_picture" /> </td> </tr> <?php if (!empty($old_picture)) { echo '<img class="profile" src="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture" />'; } ?> </fieldset> <tr> <td> <input type="submit" value="Save Profile" name="submit" /> </td> </tr> </form> </table> <?php // Insert the page footer require_once('footer.php'); ?> I implemented your corrction but its still bringing out that error. What do you think might be wrong? Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted January 23, 2011 Author Share Posted January 23, 2011 Does any 1 have any good idea on how I can trap this error? Quote Link to comment 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.