Josh Curren
Josh Curren

Reputation: 10226

Problems uploading a file in php

I am trying to upload a file to a sever using PHP. I cannot get it to work. Here is the code:

if( isset($_POST['Upload']) )
{

   //size condition 
   if ( $_FILES['uploaded']['size'] > 350000) 
   { 
      $mesg = "Your file is too large.<br>"; 
      exit; 
   } 

   if( move_uploaded_file($_FILES['uploaded']['tmp_name'], "upload/" . $_FILES['uploaded']['name'] ) )
   { 
      $mesg =  "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";    
   } 
   else 
   {     
      $mesg =  "Sorry, there was a problem uploading your file."; 
   }    
}
else
{
   $mesg = "Select a File to upload.";
}

Here is the code for the form I am using to submit the file:

<?
echo $mesg;
?>
<br /><br />
<form enctype="multipart/form-data" action="" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>

Upvotes: 0

Views: 607

Answers (3)

Tom Haigh
Tom Haigh

Reputation: 57845

Your submit button does not have a name:

<input type="submit" value="Upload" />

You are checking for $_POST['Upload'], so you probably want:

<input type="submit" value="Upload" name="Upload" />

Upvotes: 3

ABCoder
ABCoder

Reputation: 306

Change your form to

<form action="" method="POST" enctype="multipart/form-data">

Upvotes: 1

Paolo Bergantino
Paolo Bergantino

Reputation: 488704

You need enctype="multipart/form-data" inside your <form> tag or nothing will be uploaded.

For more, check out the PHP manual.

Also, I am not sure if you're just doing this to test the functionality, but you should be wary of putting uploaded files in a web accessible folder, especially with their original names. This leaves an open door for someone to upload a malicious script and potentially take over your server.

Upvotes: 6

Related Questions