Jump to content

[SOLVED] file upload.... why doesn't my code work?


tbare

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

...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";
}

?>

Link to comment
Share on other sites

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!

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.