How to Upload a File in PHP
How to Upload a File in PHP
This wikiHow teaches you how to create a basic form to allow users to upload files to your website. You'll need to create two files. The actual upload form will be created using HTML. The PHP file will be a separate file that is referenced in the HTML code. The following scripts do not include any security restrictions. It will allow anybody to upload any file to your server.
Steps

Creating the HTML File

Open a Text document. You can use Notepad in Windows, or TextEdit in Mac to write HTML and PHP code.

Save the document as an HTML document. Click "File", then click "Save As" and name the document something like Upload_Form.html. Be sure to add the .html extension at the end of the file name. This will save the file as an html document.

Type the following code. This code tells the web browser that the language we are using is HTML. Click here to learn how to create a basic HTML page.

Create a body for your HTML page. The body is where all the displayable contents for your webpage are written. On a new line, type after the starting tag. This indicates where the body begins. At the end, you'll use the closing tag to close the body of the document along with the closing tag. Make sure at the end of your HTML file that the closing tag goes before the closing tag.

Type the first line of the form. The part of the line that says action="upload.php" references the PHP file that will be created in the next part of this tutorial. Both method="post", and enctype="multipart/form-data" are necessary for the form to work properly. Type the following line to create the upload form:

Type an upload message in the next line. Type

followed by an upload message, then type

at the end of the message. The message should say something like "Upload a file" or "Select your file".

Create the browse button and file box. This code will create the browse button and the box that contains the file to be uploaded. Type the following code to create this part of the upload form:

Close the form. Type the closing

tag to close the form. That concludes the HTML portion of the form. Your entire HTML document should look something like this:

Upload your file

Creating the PHP File

Open a new text document. You can use Notepad on Windows, or TextEdit on Mac to write PHP and HTML code.

Save the text document as upload.php. Click "File", then "Save As". Be sure to include the .php extension in the document name. If you are naming PHP document something other than "upload.php", make sure you update the text in your HTML document so that it references the correct document. When you are finished writing the document, you will upload it to the same directory were the uploaded files will go on your server.

Type

Type the file directory code in the next line. The first line specifies the directory the files will be uploaded to (you will need to create this directory on your server). The second line specifies the path of the file. Type the following code: $target_dir = "uploads/"; $target_file = $target_dir . basename( $_FILES['uploaded_file']['name']);

Create a message to indicate the file has been uploaded. The following code places the file in a temporary directory while until the user clicks "Upload". Once they click upload, they will receive a message that lets them know the file was successfully uploaded. if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) { echo "The file ". basename( $_FILES['uploaded_file']['name']). " has been uploaded"; }

Create an error message. If the file is unable to upload, there needs to be an error message to let the user know the file wasn't able to upload. Type the following code to create an error message: else{ echo "Your file was unable to upload. Please try again!"; }

Type ?> in the last line. This tag will close the PHP script. Your entire code should look something like this.

What's your reaction?

Comments

https://shivann.com/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!