Upload files script

wohawsnng

New Member
Messages
2
Reaction score
0
Points
0
hey I've recently created a script so that people can upload files onto my domain. however i keep getting "500 internal server error". please help me look at my script and tell me what's wrong.(I have a "files" folder in the same folder as the scripts with permissions 777) Thank you very much.

Index.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
</head>

<body>
<?php


$password = "password";

if ($_POST['txtpassword']!= $password) {
echo "Invalid Password./No password entered.";
?>

<form name="form" method="post" action="<?php echo $_SERVER['php_SELF'];?>">

Password: 
  <input type="password" name="txtpassword" />
<input type="submit" value="Submit" />

</form>

<?php

}

else {

?>

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="33554432" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


<?php

}

?> 
</body>
</html>

upload.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head>

<body>
<?php
$target_path = "files/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

</body>
</html>
 
Last edited:

nightscream

New Member
Messages
948
Reaction score
0
Points
0
I've added an isset() function so it only starts when the submit button is clicked. You don't get an error with more information?

I can't find any problem..
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
</head>

<body>
<?php


$password = "password";
if(isset($_POST['form1'])){
if ($_POST['txtpassword']!= $password) {
echo "Invalid Password./No password entered.";
}
?>

<form name="form" method="post" action="<?php echo $_SERVER['php_SELF'];?>">

Password: 
  <input type="password" name="txtpassword" />
<input type="submit" name="form1" value="Submit" />

</form>

<?php

}

else {

?>

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="33554432" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


<?php

}

?> 
</body>
</html>
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
First of all: we might want to know more about that internal error...
How to get that info:
1. Go to your cp
2. Click "Error log" under "Logs"
3. Find the right error and tell us ^^

Second, there's a tutorial on PHP and file uploads here:
http://www.tizag.com/phpT/fileupload.php

I hope this was useful,
Marshian
 
Top