How to Upload a File in Php for Dummies
Download Article
Download Article
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 split file that is referenced in the HTML code. The following scripts do not include any security restrictions. It will let anybody to upload whatsoever file to your server.
-
1
Open a Text document. Yous tin use Notepad in Windows, or TextEdit in Mac to write HTML and PHP lawmaking.
-
ii
Salvage the document as an HTML document. Click "File", then click "Salvage As" and name the document something like Upload_Form.html. Be sure to add the .html extension at the terminate of the file proper name. This will relieve the file equally an html document.
Advertising
-
three
Blazon the following code. This code tells the web browser that the language nosotros are using is HTML. Click hither to learn how to create a basic HTML page.
<!DOCTYPE html> < html > </ html >
-
4
Create a body for your HTML page. The body is where all the displayable contents for your webpage are written. On a new line, blazon <body> after the starting <html> tag. This indicates where the body begins. At the end, you'll use the endmost </trunk> tag to close the body of the document along with the closing </html> tag.
- Make certain at the end of your HTML file that the closing </body> tag goes earlier the closing </html> tag.
-
v
Type the first line of the form. The role of the line that says activeness="upload.php" references the PHP file that will be created in the next role of this tutorial. Both method="post", and enctype="multipart/course-information" are necessary for the form to work properly. Blazon the post-obit line to create the upload course:
< form enctype = "multipart/form-data" action = "upload.php" method = "mail service" >
-
half-dozen
Type an upload message in the next line. Type <p> followed by an upload message, and then type </p> at the terminate of the message. The message should say something similar "Upload a file" or "Select your file".
-
vii
Create the browse push 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:
< input type = "file" name = "uploaded_file" > < input blazon = "submit" value = "Upload" >
-
8
Shut the form. Blazon the closing </class> tag to close the form. That concludes the HTML portion of the form. Your entire HTML certificate should look something like this:
<!DOCTYPE html> < html > < trunk > < form enctype = "multipart/form-data" action = "upload.php" method = "post" > < p >Upload your file</ p > < input type = "file" name = "uploaded_file" > < input type = "submit" value = "Upload" > </ form > </body </ head >
Advertising
-
1
Open a new text certificate. You lot tin use Notepad on Windows, or TextEdit on Mac to write PHP and HTML code.
-
2
Salve the text document equally upload.php. Click "File", so "Save As". Be sure to include the .php extension in the certificate proper noun. If you lot are naming PHP document something other than "upload.php", make sure y'all update the text in your HTML document so that information technology references the correct document. When yous are finished writing the document, you will upload it to the same directory were the uploaded files will get on your server.
-
3
Type <?php in the first line. This tells the spider web browser that yous are using a PHP script.
-
iv
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 lawmaking:
$target_dir = "uploads/" ; $target_file = $target_dir . basename ( $_FILES [ 'uploaded_file' ][ 'proper name' ]);
-
5
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 volition receive a bulletin 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" ; }
-
vi
Create an error bulletin. If the file is unable to upload, there needs to be an fault message to permit the user know the file wasn't able to upload. Type the following lawmaking to create an mistake message:
else { echo "Your file was unable to upload. Please try over again!" ; }
-
7
Blazon ? > in the last line. This tag will shut the PHP script. Your entire code should expect something like this.
<?php $target_dir = "uploads/" ; $target_file = $target_dir . basename ( $_FILES [ 'uploaded_file' ][ 'name' ]); if ( move_uploaded_file ( $_FILES [ 'uploaded_file' ][ 'tmp_name' ], $path )) { echo "The file " . basename ( $_FILES [ 'uploaded_file' ][ 'name' ]) . " has been uploaded" ; } else { repeat "Your file was unable to upload. Please try again!" ; $uploadOk = 0 ; } ?>
Advertisement
Ask a Question
200 characters left
Include your email address to go a message when this question is answered.
Submit
Advertisement
About This Commodity
Thanks to all authors for creating a page that has been read 11,680 times.
Is this article up to engagement?
harveymandivether.blogspot.com
Source: https://www.wikihow.com/Upload-a-File-in-PHP
0 Response to "How to Upload a File in Php for Dummies"
Post a Comment