dnoland Posted October 7, 2007 Share Posted October 7, 2007 I have tried everything and still can not explain this... I just hope one of you can... So i have someForm.html like this... <form action = "basic.php" method = "post" enctype="multipart/form-data"> File: <input type = "file" name = "file" id = "file" /><br /> <input type="submit" name = "submit" id = "submit" value = "Submit" /><br /> </form> And I have basic.php like this <html> <body> <?php function validateFile($upload) { else { if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000) { echo "Upload: " . $upload["name"] . "<br />"; echo "Type: " . $upload["type"] . "<br />"; echo "Size: " . $upload["size"] . "<br />"; echo "Stored in: " . $upload["tmp_name"] . "<br />"; return true; } else { echo "Invalid file<br />"; return false; } } } function saveFile($upload) { if (validateFile($uplaod)) { // Do some stuff... that never seems to actually happen } else { echo "Can't save invalid file <br />"; return false; } } validateFile($_FILES["file"]); saveFile($_FILES["file"]); ?> </body> </html> And this is what I get back when I upload some gif (civilDisobidence.gif in this case) <html> <body> Upload: civilDisobidience.gif<br /> Type: image/gif<br /> Size: 13139<br /> Stored in: /tmp/phpIZBJ3R<br /> Invalid file<br /> Can't save invalid file <br /> </body> </html> So what this comes down to is that the function validateFile is returning false in spite of the fact that it obviously is executing a part of the script that obviously ends in return true. Further... it is running both the 'then' and 'else' parts of nested if else statement in the validateFile function. I have never had any error like that before. Any thoughts?? Thanks for any help on this one... Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/ Share on other sites More sharing options...
d22552000 Posted October 7, 2007 Share Posted October 7, 2007 if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000) Well, I (me) woudl do the following just for debugging's sake: if ($upload["size"] < 20000000) { if ($upload["type"] == "image/gif") { //call the function that does stuff if its valid } else if ($upload["type"] == "image/jpeg") { //call the function that does stuff if its valid } else if ($upload["type"] == "image/pjpeg") { //call the function that does stuff if its valid } else { return(false); } } Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364202 Share on other sites More sharing options...
teng84 Posted October 7, 2007 Share Posted October 7, 2007 whats this function validateFile($upload) { else else comes first without if lol Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364206 Share on other sites More sharing options...
darkfreaks Posted October 7, 2007 Share Posted October 7, 2007 ^ theres your glitch Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364208 Share on other sites More sharing options...
dnoland Posted October 8, 2007 Author Share Posted October 8, 2007 Well d22552000, I can do that... but I don't really understand why... the file output already told me that it is a gif and all of that... Upload: civilDisobidience.gif<br /> Type: image/gif<br /> Size: 13139<br /> Stored in: /tmp/phpIZBJ3R<br /> Invalid file<br /> Can't save invalid file <br /> So I can't really see how their could be an error in any of those conditions. Besides, the whole condition (($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000 should return true or false. The fact that the server kicks back both the section about Type, Size, ect... and the words "invalid file" seem to indicate that it ran BOTH the 'then' and 'else' part of the code. As far as I know, this should not happen in ANY event. Thanks... I will try what you suggested and get back to you... As for teng84... my mistake, I copied that section wrong... the script on the server says the following... <html> <body> <?php function validateFile($upload) { if ($upload["error"] > 0) { echo "Error: " . $upload["error"] . "<br />"; return false; } else { if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000) { echo "Upload: " . $upload["name"] . "<br />"; echo "Type: " . $upload["type"] . "<br />"; echo "Size: " . $upload["size"] . "<br />"; echo "Stored in: " . $upload["tmp_name"] . "<br />"; return true; } else { echo "Invalid file<br />"; return false; } } } function saveFile($upload) { if (validateFile($uplaod)) { if(file_exists("upload_" . $upload["name"])) { echo $upload["name"] . " already exists.<br />"; return false; } else { move_uploaded_file($upload["tmp_name"], "upload_" . $upload["name"]); echo "Store sucess <br />"; return true; } } else { echo "Can't save invalid file <br />"; return false; } } validateFile($_FILES["file"]); saveFile($_FILES["file"]); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364216 Share on other sites More sharing options...
teng84 Posted October 8, 2007 Share Posted October 8, 2007 if your using file first try to print_r($_FILES); to see its content and to have an idea whether your condition is right or logic Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364231 Share on other sites More sharing options...
dnoland Posted October 8, 2007 Author Share Posted October 8, 2007 Ok... print_r($_FILES); Gives the following... Array ( [file] => Array ( [name] => civilDisobidience.gif [type] => image/gif [tmp_name] => /tmp/phpAjHCsS [error] => 0 [size] => 13139 ) ) Upload: civilDisobidience.gif Obviously the temp location is randomized... but the rest of that seems to be in order to me... Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364236 Share on other sites More sharing options...
teng84 Posted October 8, 2007 Share Posted October 8, 2007 try to if (validateFile($uplaod)=== true) that might be just the same but try it and i guess your function can only be one Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364245 Share on other sites More sharing options...
teng84 Posted October 8, 2007 Share Posted October 8, 2007 this is thesame function validateFile($upload){ if ($upload["error"] > 0) { echo "Error: " . $upload["error"] . "<br />"; $return= false; } elseif(file_exists("upload_" . $upload["name"])){ echo $upload["name"] . " already exists.<br />"; $return =false; } else { if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000) { echo "Upload: " . $upload["name"] . "<br />"; echo "Type: " . $upload["type"] . "<br />"; echo "Size: " . $upload["size"] . "<br />"; echo "Stored in: " . $upload["tmp_name"] . "<br />"; move_uploaded_file($upload["tmp_name"], "upload_" . $upload["name"]); echo "Store sucess <br />"; $return= true; } else { echo "Invalid file<br />"; $return =false; } } return $return; } sigle but the same with your two function Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364249 Share on other sites More sharing options...
dnoland Posted October 8, 2007 Author Share Posted October 8, 2007 try to if (validateFile($uplaod)=== true) that might be just the same but try it and i guess your function can only be one Well... I did the === true change. No effect... but at least now we are positive that the validateFile function is returning false... so the bug must live in that function... As for that rewrite... if just gives me "invalid file" when I run that. That means that the condition (($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000 is false... no idea what that is about... Now... here is something interesting (were back to the 2 function version here...) if i echo validateFile($_FILES["file"]) . "<br />"; it prints the normal output of validateFile as well as a 1. I thought 1 was true??? validateFile must be returning false, otherwise I would not be getting "Can't save invalid file" from the saveFile function??? Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364263 Share on other sites More sharing options...
teng84 Posted October 8, 2007 Share Posted October 8, 2007 when you echo the true it will give you 1 means true 1 or morethan one is true 0 or empty is false Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364280 Share on other sites More sharing options...
yzerman Posted October 8, 2007 Share Posted October 8, 2007 in your saveFile upload function: if(file_exists("upload_" . $upload["name"])) should be if(!file_exists("upload_" . $upload["name"])) In your current code, you are telling it if the file exists, upload it, or else return "Can't save invalid file" Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364289 Share on other sites More sharing options...
dnoland Posted October 8, 2007 Author Share Posted October 8, 2007 Sorry... my bad... all of this trouble over some stupid typo... function saveFile($upload) { if (validateFile($uplaod)===true) //Note to self... learn to spell upload... damn. I guess that is the trouble with loosely typed languages... or being unable to type... one of the two. in your saveFile upload function: if(file_exists("upload_" . $upload["name"])) should be if(!file_exists("upload_" . $upload["name"])) In your current code, you are telling it if the file exists, upload it, or else return "Can't save invalid file" I am going to have to disagree with you on that one... sudo code for that save function if (file already exists) { say so } else { upload it } All in all I have to say thanks to all of you... I am really impressed with this whole site, and very glad I joined this group. Hopefully I will be able to give back (after I learn some more php). Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364294 Share on other sites More sharing options...
yzerman Posted October 8, 2007 Share Posted October 8, 2007 your right, I read that wrong ??? Sorry I couldn't be more help Quote Link to comment https://forums.phpfreaks.com/topic/72228-solved-file-upload-script-glitch/#findComment-364305 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.