PHP move_uploaded_file
The move_uploaded_file() function is used to move uploaded file to the specified location.
The move_uploaded_file() function returns true if the move is successful and false if it fails.
The PHP move_uploaded_file will overwrite if the file already exists with the same name.
For illustration, how to use move_uploaded_file() function see an example below.
Example of using move_uploaded_file
The following example is a script for PHP file uploading that uses the move_uploaded_file function to move a file to the specified location in server.
For making it clearer, both HTML (client side) and PHP script are given below in two steps:
Step 1 – User interface
Step 1 is just the HTML file to select a file from the system or device.
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
|
<html>
<head>
<title>A simple example of uploading a file using PHP script</title>
</head>
<body>
<b>Simple example of uploading a file</b><br />
Choose a file to be uploaded<br />
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
|
Step 2- Using PHP move_uploaded_file function
This step uses the move_uploaded_file function as below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
$targetfolder = "testupload/";
//Usage of basename() function
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
}
else {
echo "Problem uploading file";
}
?>
|
For more on PHP file uploading, read its complete chapter here: PHP file upload.
Leave A Comment?