How can the $_FILES variable be utilized to manipulate file names during uploading in PHP?
When uploading files in PHP using the $_FILES variable, you can manipulate the file names before saving them to the server. This can be useful for adding prefixes, suffixes, or unique identifiers to the file names. By accessing the 'name' key of the $_FILES array, you can modify the file name as needed before moving it to the desired location on the server.
// Get the original file name
$originalFileName = $_FILES['file']['name'];
// Manipulate the file name (e.g. add a timestamp)
$newFileName = time() . '_' . $originalFileName;
// Specify the destination directory
$uploadPath = 'uploads/' . $newFileName;
// Move the uploaded file to the destination directory with the new file name
move_uploaded_file($_FILES['file']['tmp_name'], $uploadPath);
Keywords
Related Questions
- How can the strtotime() function be utilized effectively in PHP for date calculations like determining the next Monday?
- How can one effectively limit the total size of a folder in PHP to prevent arbitrary file uploads?
- Welche Browserkompatibilitätsprobleme können beim Styling von Scrollbalken mit CSS auftreten?