angel1987 Posted February 23, 2013 Share Posted February 23, 2013 I have a web page that lets user upload their profile photo. The PHP script works, but sometimes it just wont upload the photo. It will even give you a success message that the image was uploaded but it wont add the filename to the database nor will it save the file on the server. If you keep trying to upload the same image for couple more times then it will work and upload the photo. So i need some help in debugging it. Where am i going wrong? I am also using this thumb_function.php file which i got from internet, it basically saves thumbnails in a seperate folder, it works fine. Here is the PHP script that i am using. // Check if the button was clicked or not and process the profile updation if(isset($_POST['btnphoto'])){ include('php/thumb_function.php'); $filename = $_FILES["txtphoto"]["name"]; $newfilename = sha1($userid.$email); $extension = substr(strrchr($filename,'.'),1); $newfilename = $newfilename.".".$extension; $allowedExts = array("jpg", "jpeg", "gif", "png"); if ((($_FILES["txtphoto"]["type"] == "image/gif") || ($_FILES["txtphoto"]["type"] == "image/jpeg") || ($_FILES["txtphoto"]["type"] == "image/png")) && ($_FILES["txtphoto"]["size"] < 1048576) && in_array($extension, $allowedExts)){ if ($_FILES["txtphoto"]["error"] > 0){ $msg = "Error: " . $_FILES["file"]["error"] . "<br>"; } else { $path="../photos/".$newfilename; // the path with the file name where the file will be stored, upload is the directory name. if(move_uploaded_file ($_FILES["txtphoto"]["tmp_name"],$path)){ if ($stmt = $mysqli->prepare("UPDATE student_profile SET photo = ? WHERE user_id = ?")) { // Bind the variables to the parameter as strings. $stmt->bind_param("si", $newfilename, $userid); // Execute the statement. $stmt->execute(); // Close the prepared statement. $stmt->close(); } $msg = 'Your Photo has been updated... '; } else { $msg = 'Failed to upload file Contact Site admin to fix the problem'; exit; } $file = $newfilename; $save = $file; $t_w = 100; $t_h = 100; $o_path = "../photos/"; $s_path = "../photos/thumbnails/"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); } } else { $msg = 'File is of a wrong type or too heavy.'; } } Here is the HTML form code.. <form name="frmphoto" method="post" action="" enctype="multipart/form-data"> <table border="0" cellpadding="5" cellspacing="5"> <tr><td>Upload Profile Photo : </td></tr> <tr><td><input type="file" name="txtphoto" size="45"></td></tr> <tr><td><input type="submit" name="btnphoto" value="Update Photo"></td></tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/274844-image-upload-php-script-sometimes-wont-work-need-help-debugging-it/ Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 (edited) To debug, you need to make either echo, print_r or var_dump your best friend. Basically, you put them just inside if statements, and if they display on the web page, then you know that the code inside that if was processed, in which case you move on to the next if statement etc. So for starters, I would put a var_dump() just inside your if statement checking the conditions of the uploaded file: if ((($_FILES["txtphoto"]["type"] == "image/gif") || ($_FILES["txtphoto"]["type"] == "image/jpeg") || ($_FILES["txtphoto"]["type"] == "image/png")) && ($_FILES["txtphoto"]["size"] < 1048576) && in_array($extension, $allowedExts)){ var_dump("MARKER"); ...... } If you see 'MARKER' appear on the page, then this if condition is satisfied, and you move on to the next if (the error checking one) That's all that is involved in debugging, just printing words to the screen so you know which parts of the script was processed and which parts weren't. Hope that gets the ball rolling! Denno Edited February 23, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/274844-image-upload-php-script-sometimes-wont-work-need-help-debugging-it/#findComment-1414354 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.