tbare Posted June 19, 2008 Share Posted June 19, 2008 I've been staring at this code long enough, and have compared to other code of mine that works great... what's wrong here? i click "submit" and nothing happens, the page reloads, but i get neither success nor failure message. <?php if($_POST['Submit']){ $uploadDir = '../images/error_msgs/'; $fileName = basename($_FILES['uploadedFile']['name']); $newFile = $uploadDir . basename($_FILES['uploadedFile']['name']); if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $newFile)) { chmod($newFile, 0666); echo "<h1>$fileName successfully uploaded and permissions have been set to 0666.</h1><br /><br \>\n"; echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n"; echo "Please choose a file:<br /> <input name=\"uploadedFile\" type=\"file\" /><br />\n"; echo "<input type=\"submit\" value=\"Upload\" />\n"; echo "</form>\n"; } else{ echo "There was an error. Try again.\n"; } } else{ echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n"; echo "Please choose a file:<br /><input name=\"uploadedFile\" type=\"file\" /><br />\n"; echo "<input name=\"Submit\" type=\"Submit\" value=\"Upload\" />\n"; echo "</form>\n"; } ?> error_msgs dir has permissions of 0666, so it should be very writable... any help will be much appreciated... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/ Share on other sites More sharing options...
xyn Posted June 19, 2008 Share Posted June 19, 2008 chmod to 0777 and see if it is your mode settings or not? I believe it should be 744 or 644 to upload content to a folder... can't remember though. Also you might want to research on only allowing specific file types, because Shell injections can be nasty. Mostly exectued via upload forms, which arent correctly secured. Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569169 Share on other sites More sharing options...
tbare Posted June 19, 2008 Author Share Posted June 19, 2008 i'll try... but shouldn't i at least get the "file has not been uploaded" message if it can't move the file? I'm not even getting that.... <edit> ... changed dir permissions to 0777 and still no dice... Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569173 Share on other sites More sharing options...
xyn Posted June 19, 2008 Share Posted June 19, 2008 ok at the top of your script do this.. <? error_reporting(E_ALL); // if no error, id say its a notice error, usually turned off. ?> Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569174 Share on other sites More sharing options...
tbare Posted June 19, 2008 Author Share Posted June 19, 2008 the first snippet returned "There was an error. Try again." Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569176 Share on other sites More sharing options...
xyn Posted June 19, 2008 Share Posted June 19, 2008 Wait i spotted it.. your form is wrong.. add this enctype="multipart/form-data" Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569177 Share on other sites More sharing options...
xyn Posted June 19, 2008 Share Posted June 19, 2008 Btw i usually: print_r($_FILES); to see if anything has been sent? if not, and your code is correct, then its your form. if there are things returned (as displayed in an array) youll be able to spot the problem. Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569178 Share on other sites More sharing options...
tbare Posted June 19, 2008 Author Share Posted June 19, 2008 hmm... alright... i think i'm heading in the right direction... at least i'm getting send failed every time now... i'll have to track down the culprit from here... thanks! Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569187 Share on other sites More sharing options...
tbare Posted June 19, 2008 Author Share Posted June 19, 2008 yeah... the array's definitely empty.. crap.. Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569190 Share on other sites More sharing options...
xyn Posted June 19, 2008 Share Posted June 19, 2008 then your form needs to look like this. <FORM METHOD="POST" ACTION="your page.php" ENCTYPE="multipart/form-data"> </FORM> MULTIPART/FORM-DATA is what sends uploaded items Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569209 Share on other sites More sharing options...
tbare Posted June 19, 2008 Author Share Posted June 19, 2008 yeah... that's what it looks like now... still no dice. ??? <?php if($_POST['Submit']){ $uploadDir = '../images/error_msgs/'; $fileName = basename($_FILES['uploadedFile']['name']); $newFile = $uploadDir . basename($_FILES['uploadedFile']['name']); print_r($_FILES); if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $newFile)) { chmod($newFile, 0666); echo "<h1>$fileName successfully uploaded and permissions have been set to 0666.</h1><br /><br \>\n"; echo "<form action=\"".$PHP_SELF."\" method=\"post\" enctype=\"multipart/formdata\">\n"; echo "Please choose a file:<br /> <input name=\"uploadedFile\" type=\"file\" /><br />\n"; echo "<input name=\"Submit\" type=\"Submit\" value=\"Upload\" />\n"; echo "</form>\n"; } else{ echo "There was an error. Try again.\n"; } } else{ echo "<form action=\"".$PHP_SELF."\" method=\"post\" enctype=\"multipart/formdata\">\n"; echo "Please choose a file:<br /><input name=\"uploadedFile\" type=\"file\" /><br />\n"; echo "<input name=\"Submit\" type=\"Submit\" value=\"Upload\" />\n"; echo "</form>\n"; } ?> returns: Array ( ) There was an error. Try again. Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-569254 Share on other sites More sharing options...
tbare Posted June 20, 2008 Author Share Posted June 20, 2008 bump, and any other thoughts / ideas? i'm stumped on this one now... Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-570439 Share on other sites More sharing options...
tbare Posted June 20, 2008 Author Share Posted June 20, 2008 ...and done... must have had some quotes wrong or something... re-typed it and it worked... ??? coding in notepad is kind of a pain... all black and white... it's easier to see what's going on when it's all color-coded as it is here: <?php if($_POST['Submit']){ $uploadDir = '../images/error_msgs/'; $fileName = basename($_FILES['uploaded']['name']); $newFile = $uploadDir . basename($_FILES['uploaded']['name']); if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $newFile)) { chmod($newFile, 0666); echo "$fileName successfully uploaded and permissions have been set to 0666.<br /><br \>\n"; echo "<form enctype=\"multipart/form-data\" action=\"".$PHP_SELF."\" method=\"POST\">\n"; echo "Please choose a file:<br /> <input name=\"uploaded\" type=\"file\" /><br />\n"; echo "<input name=\"Submit\" type=\"Submit\" value=\"Upload\" />\n"; echo "</form>\n"; } else{ echo "There was an error. Try again.\n"; } } else{ echo "<form enctype=\"multipart/form-data\" action=\"".$PHP_SELF."\" method=\"POST\">\n"; echo "Please choose a file:<br /><input name=\"uploaded\" type=\"file\" /><br />\n"; echo "<input name=\"Submit\" type=\"Submit\" value=\"Upload\" />\n"; echo "</form>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-570448 Share on other sites More sharing options...
the shig Posted June 20, 2008 Share Posted June 20, 2008 you missed a dash (-) echo "<form action=\"".$PHP_SELF."\" method=\"post\" enctype=\"multipart/formdata\">\n"; change to echo "<form action=\"".$PHP_SELF."\" method=\"post\" enctype=\"multipart/form-data\">\n"; Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-570452 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 Try Notepad++ if you don't like Notepad. =P Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-570453 Share on other sites More sharing options...
tbare Posted June 20, 2008 Author Share Posted June 20, 2008 shig: thanks! it was still bothering me... stupid typo's! guess i should watch who i copy/paste from, eh? darkwater: yeah, i use notepad++ at home, but when i'm dead at work, i use what i have available to me, and that's not something i have available here. so... i code "blindly." thanks for the suggestion, though! Quote Link to comment https://forums.phpfreaks.com/topic/110945-solved-file-upload-why-doesnt-my-code-work/#findComment-570491 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.