Move uploaded files to a different directory using PHP

PHP stores uploaded files in a temporary directory with temporary file names. It is a good practise to move uploaded files to a permanent directory, if you want to keep them permanently. The move_uploaded_file() function can be used to move uploaded files.

Example code:

<?php
  $file = '/tmp/w3image.gif';
  print("<pre>\n");
  move_uploaded_file($_FILES['w3img']['tmp_name'], $file);
  print("File uploaded: ".$file."\n");
  print("

\n”);
?>

In a shared hosting environment, you may need to ask the administrators which directories you can use to store files.

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Filed Under: PHP File Handling

Tags:

RSSComments (0)

Trackback URL

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.