How to Allow Users to Upload Files to My Website

A website that allows for interaction in some way provides a richer experience for visitors. File upload capability enables your users to place their content on your site. This may be useful in the case of a classified or video sharing site for instance. Use PHP and an HTML file browser form to allow users to upload files to your website.

Give your users file upload capability.

Step 1

Open a text or HTML. Type the following data to create the file browser form:

Choose File:

Save your HTML file (for example "uploadfile.html").

Step 2

Create the "uploadfile.php" PHP file noted in the "action" parameter of the above form. Type the following data in a new file:

<?php $ path = "files/"; $path = $path . basename( $_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) { echo "Success uploading". basename($_FILES['userfile']['name']); } else{ echo "Error when uploading file."; } ?>

Note that the "userfile" attribute in "$_FILES" corresponds with the "name" attribute of your form. It can be whatever you like, but must be identical in both places.

Step 3

Save the file and upload them to your server. Test your page by navigating to the web page and using the form.

Things You'll Need

  • HTML or text editor

  • PHP-enabled server

references