If you want your users to be able to upload a file, you need an HTML page with a form and a php script to process that form.
To upload files the form’s method has to be set to POST and you have to set the enctype of that form to multipart/form-data.
To display information about the uploaded file you can use the $_FILES superglobal
Handling file uploads
Here is the HTML form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<html> <head> <title>FORM TO UPLOAD A FILE</title> </head> <body> <form action="#" method="POST" enctype="multipart/form-data"> <p> file: <input type="file" name="uploadedFile" value=""/> </p> <p> <input type="submit" name="upload" value="upload"/> </p> </form> </body> </html> |
STEP 1. Accessing file data via $_FILES:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php #uploadFileForm.php //checking if the form has been submitted if( isset($_POST['upload']) ){ //display $_FILES content echo "<pre>"; print_r($_FILES); echo "</pre>"; } /* **** the content of $_FILES **** Array ( [uploadedFile] => Array ( [name] => picture.png [type] => image/png **** when the file is uploaded, it's given a temporary name and it's saved in a temp folder **** [tmp_name] => /Applications/MAMP/tmp/php/php3mvXFR [error] => 0 [size] => 391750 ) ) [...] */ ?> |
STEP 2. Storing file data into variables if the temporary file has been uploaded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php #uploadFileForm.php //checking if the form has been submitted if( isset($_POST['upload']) ){ //display $_FILES content /*echo "<pre>"; print_r($_FILES); echo "</pre>";*/ //you can use is_uploaded_file() to check whether the file has been uploaded if( is_uploaded_file($_FILES['uploadedFile']['tmp_name']) ){ //storing file data into variables //this is the actual name of the file $fileName = $_FILES['uploadedFile']['name']; //this is the temporary name of the file $fileTempName = $_FILES['uploadedFile']['tmp_name']; //this is the path where you want to save the actual file $path = "path/to/images/folder/"; //this is the actual path and actual name of the file $newPathAndName = $path . $fileName; //[...] }//end if is_uploaded_file }//end if isset upload ?> |
STEP 3. Move and rename the temporary file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php #uploadFileForm.php //checking if the form has been submitted if( isset($_POST['upload']) ){ //display $_FILES content /*echo "<pre>"; print_r($_FILES); echo "</pre>";*/ //you can use is_uploaded_file() to check whether the file has been uploaded if( is_uploaded_file($_FILES['uploadedFile']['tmp_name']) ){ //storing file data into variables //this is the actual name of the file $fileName = $_FILES['uploadedFile']['name']; //this is the temporary name of the file $fileTempName = $_FILES['uploadedFile']['tmp_name']; //this is the path where you want to save the actual file $path = "path/to/images/folder/"; //this is the actual path and actual name of the file $newPathAndName = $path . $fileName; //you can use move_uploaded_file() to move and rename the temp file if( move_uploaded_file($fileTempName, $newPathAndName) ){ echo "The file has been successfully uploaded"; } else { echo "Could not upload the file"; }//end if move_uploaded_file }//end if is_uploaded_file }//end if isset upload ?> |