How to Upload Files to a Web Server in PHP
You want a website that allows visitors to upload files and interact with your site. PHP is a scripting language you can use to provide this capability. PHP contains methods you can use to send a file to a web server. An HTML file browse form on your web page can enable the user to select the file.
Instructions
-
-
1
Open your HTML or text editor and type the following to create the file browse form:
<form enctype="multipart/form-data" action="upload_files.php" method="POST">
Choose File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Save the file; for example "uploadform.html."
-
2
Create the PHP file that executes the "action" in the above form by typing the following in a new file:
<?php
$ path = "files/";
$path = $path . basename( $_FILES['userfile']['name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) {
echo "Successful upload of ". basename($_FILES['photofile']['name']);
} else{
echo "Error when uploading the file.";
}
?>
Save your file as "upload_files.php."
-
-
3
Upload the files to your server. To test your form, navigate to the web page that contains the form.
-
1
References
- Photo Credit laptop connection to internet world black bg image by patrimonio designs from Fotolia.com