To upload a file, you need at least an HTML POST form with multipart/form-data . In this case, you put the input type="file" field to view the file and the submit button to submit the form.
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit"> </form>
In upload.php downloaded file is available $_FILES with the field name as the key.
$file = $_FILES['file'];
You can get its name as follows:
$name = $file['name'];
You need to move it to a permanent place using move_uploaded_file() , otherwise it will be lost:
$path = "/uploads/" . basename($name); if (move_uploaded_file($file['tmp_name'], $path)) { // Move succeed. } else { // Move failed. Possible duplicate? }
You can save the path in the database in the usual way:
$sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')"; // ...
source share