What functions can be used to access and manipulate files uploaded through a form in PHP?
To access and manipulate files uploaded through a form in PHP, you can use the $_FILES superglobal array along with various file handling functions such as move_uploaded_file() to save the file to a directory on the server, file_get_contents() to read the contents of the file, and file_put_contents() to write data to a file.
<?php
// Check if the file was uploaded successfully
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
// Move the uploaded file to a directory on the server
move_uploaded_file($fileTmpPath, 'uploads/' . $fileName);
// Read the contents of the uploaded file
$fileContent = file_get_contents('uploads/' . $fileName);
// Manipulate the file content if needed
// For example, you can echo or save the content to another file
echo $fileContent;
} else {
echo 'File upload failed.';
}
?>
Keywords
Related Questions
- What are the limitations of using a function like CLEAN() to sanitize input data in PHP?
- Are there alternative methods or libraries in PHP that can be used to highlight search terms in database output more efficiently than str_replace, str_ireplace, or preg_replace?
- How does the PHP class "PDO" simplify database connections and what are its advantages over other methods like "mysqli_" or "odbc"?