views:

94

answers:

4

I am using the following code to upload the file on my server. How do I store it in a particular folder?

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
A: 

The file will be stored in a default folder, and then you move_uploaded_file to the desired folder.

pavium
+1  A: 
$src = $_FILES['file']['tmp_name'];
$dest = '/your/favorite/path/myfile.jpg';
move_uploaded_file($src,$dest);

Obviously, you'll want to do some basic error checking - make sure the destination directory is writable, handle any exceptions that may occur, etc. That's the basic idea, though.

inkedmn
+1  A: 

Here's a decent article on the topic.

secure file upload check list

Trevor Tippins
Very Good Article about security. Nice.
Fábio Antunes
+3  A: 

Use 'move_uploaded_file'

move_uploaded_file($_FILES["file"]["tmp_name"], "$uploads_dir/$name");

'$uploads_dir' holds the folder you want to store the uploaded file.

NawaMan