This PHP example program demonstrates how to upload a file and move it from its temporary upload location. The code is listed directly below as the file "UploadAndMove.php". Below the code are several images that show what is displayed when the program is executed. The initial image shows the two buttons that first show up. Once the "Choose File" button is pressed, the file chooser window opens to allow the user to select a file as shown in the "Choose File" image. Once the user selects a file, like the "XoaX.txt" file shown here, and presses the "Open" button, the file name is displayed as shown in the "File Chosen" image. Finally, the output is displayed after the user clicks the "Submit Form" button.
Finally, the file is moved to the specified location. The temporary upload location is shown as well as the location where the file was moved. The temporary file is, as indicated, a temporary location; it will simply be deleted unless it is moved to a different location. Using the Move_Uploaded_File() function provides additional protection against malicious trickery.
<?php if (IsSet($_POST['Submit']) && IsSet($_FILES['File'])) { // Move the selected file to the "moved" directory inside the current directory. $sSource = $_FILES['File']['tmp_name']; $sDestination = './moved/'.basename($_FILES['File']['name']); $bWasMoved = Move_Uploaded_File($sSource, $sDestination); // Show where the file was moved from and where it went. echo '<p>TempSource='.$sSource.'</p>'; echo '<p>Destination='.$sDestination.'</p>'; // The returned value tells us whether the move was successful. echo '<p>The file was ' . (($bWasMoved) ? 'moved</p>' : '<b>NOT</b> moved</p>'); } else { ?> <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data"> <input type="file" name="File" /> <input type="submit" name="Submit" value="Submit form" /> </form> <?php } ?>
TempSource=C:\Windows\Temp\phpA4A2.tmp Destination=./moved/XoaX.txt The file was moved
© 20072025 XoaX.net LLC. All rights reserved.