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

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

Step

Choose File:

Step

Step

Save your HTML file (for example “uploadfile.html”).

Step

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

Step

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

Step

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

Step

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

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