Hello i am having problems with the code below for some reason the echo is showing when i load the file its showing on start up any ideas on how to fix this?
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "<a href='http://$url/files/". basename( $_FILES['uploadedfile']['name'])."'>Download</a>";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999999999999">
<input name="uploadedfile" type="file"><br>
<input type="submit" value="Upload">
</form>
I'll just make it an answer, see if OP accepts it to close the question; since it did work for OP.
Name your submit button <input type="submit" value="Upload" name="submit">
then wrap your PHP inside if(isset($_POST['submit'])){...}
Another way to do this is to use two seperate files.
For example. HTML form
<form enctype="multipart/form-data" action="handler.php" method="POST">...</form>
handler.php (while keeping the named submit button)
if(isset($_POST['submit'])){
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "<a href='http://$url/files/". basename( $_FILES['uploadedfile']['name'])."'>Download</a>";
} else{
echo "There was an error uploading the file, please try again!";
}
} // brace for if(isset($_POST['submit']))
the echo show because you have a if statement and on load page it's always false. put a if with a isset first like this :
if(isset($_POST['uploadedfile'])){
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "<a href='http://$url/files/". basename( $_FILES['uploadedfile']['name'])."'>Download</a>";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999999999999">
<input name="uploadedfile" type="file"><br>
<input type="submit" value="Upload">
</form>
Should do the trick if not post more information / code and specifie witch echo is showing... thx
Would be cleaner to add name to the button like name="submit" and do the isset on the button name OR a input type="hidden" with a name for the form.
You never bothered checking if a POST was actually performed, and you also don't bother checking if the upload actually succeeded. NEVER assume success. Always assume failure and treat success as a pleasant surprise:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_FILES['uploadedfile']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code {$_FILES['uploadedfile']['error']}");
}
... do your upload processing here
}