How to Allow Users to Upload Files to My Website

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Give your users file upload capability.

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.

Advertisement

Step 1

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

Video of the Day

Choose File:

Advertisement

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:

Advertisement

<?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."; } ?>

Advertisement

Advertisement

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.

Video of the Day

Advertisement

Advertisement

references