How can a PHP script be modified to display a direct link to the uploaded file instead of a list?
To modify a PHP script to display a direct link to the uploaded file instead of a list, you can simply use the file path of the uploaded file to create a direct link. This can be done by echoing an anchor tag (<a>) with the href attribute set to the file path.
<?php
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
echo 'File uploaded successfully. Direct link: <a href="' . $uploadFile . '">' . $uploadFile . '</a>';
} else {
echo 'Error uploading file.';
}
}
?>
Related Questions
- What are the potential pitfalls of converting an object to an array in PHP?
- How can the use of a dedicated mailer class improve the functionality and security of email sending in PHP applications?
- What are the advantages and disadvantages of using UNION versus separate SELECT statements in PHP MySQL queries?