Jump to content

Image upload PHP script sometimes wont work, Need help debugging it.


angel1987

Recommended Posts

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>

Link to comment
Share on other sites

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 by denno020
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.