PHP Files

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:

<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:

<?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

<?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

<?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 ?>

 

PHP Files

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:

<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:

<?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

<?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

<?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 ?>

 

PHP Files

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:

STEP 1. Accessing file data via $_FILES:

 

STEP 2. Storing file data into variables if the temporary file has been uploaded

STEP 3. Move and rename the temporary file