Upload, Download and Delete files – PHP
We will teach you how you can upload, download and delete files in PHP. We will be implementing 3 functions and will be creating 3 files for each function:
- upload.php for uploading the file
- index.php for downloading the file
- delete.php for deleting the file
Upload file
Start off, by creating 2 files index.php and upload.php, also create a folder named uploads all uploaded files will be stored in this folder. If you are using Mac, you may need to enable permission by following the below steps:
Folder permission for Mac users
Folder permission for Mac users
Now you need to create a simple form that allows a user to select file from his computer to upload in your server:
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
Next create a file named upload.php and paste the following code in it:
<?php
// Getting uploaded file
$file = $_FILES["file"];
// Uploading in "uplaods" folder
move_uploaded_file($file["tmp_name"], "uploads/" . $file["name"]);
// Redirecting back
header("Location: " . $_SERVER["HTTP_REFERER"]);
At this point, if you execute the index.php file you will be able to select file and when you click Upload button, the selected file should be saved in uploads folder.
Download file
In order to download the file, we are going to display a list of all files and a Download button. Paste the following code in index.php file:
<?php
// This will return all files in that folder
$files = scandir("uploads");
// If you are using windows, first 2 indexes are "." and "..",
// if you are using Mac, you may need to start the loop from 3,
// because the 3rd index in Mac is ".DS_Store" (auto-generated file by Mac)
for ($a = 2; $a < count($files); $a++)
{
?>
<p>
<!-- Displaying file name !-->
<?php echo $files[$a]; ?>
<!-- href should be complete file path !-->
<!-- download attribute should be the name after it downloads !-->
<a href="uploads/<?php echo $files[$a]; ?>" download="<?php echo $files[$a]; ?>">
Download
</a>
</p>
<?php
}
At this point, if you refresh index.php you will see a list of all your uploaded files along with a Download button. Clicking on the Download button, will open a Download dialog based on your browser.
Delete file
In order to delete the file, first display a button to be clicked to delete. Href should be the full path of the file. The following code should be inserted inside the loop, after the Download button:
<a href="delete.php?name=uploads/<?php echo $files[$a]; ?>" style="color: red;">
Delete
</a>
Create a new file named delete.php and paste the following code in it:
<?php
// Built-in PHP function to delete file
unlink($_GET["name"]);
// Redirecting back
header("Location: " . $_SERVER["HTTP_REFERER"]);
Now you will see a red button with a text Delete, on click it will delete the file and refresh the index.php page so you will see the remaining files. That’s how you can upload, download and delete the files in PHP.
You can also learn to:
- Upload file in Node JS
- Download files in BLOB
- Upload, download, and delete from Firebase storage from here
Hi this is very useful but i have i question how can i lrt it run on a server becouse i want to upload the file from a pc and than download it at a nother pc. It would be nice if someone could help me. (sorry for my bad english:))
You can simply upload this code to a hosting server. Upload the file from 1 PC. Access the web page from the 2nd PC and download the file.
Hello, I have a question, can you program me something that allows me to download all files that I upload at once that would be super nice.
For that, you need to create a ZIP file and add all files to it. Then you can download the ZIP file.
Follow this:
https://adnan-tech.com/create-zip-file-with-password-php/
Thanks for the quick answer I would like to upload individual files and then choose whether I download them all at once or download certain files individually. Does that work?
Thanks for the quick answer I would like to upload individual files and then choose whether I download them all at once or download certain files individually. Does that work?
Hi I would like to have a button where I can press it and then download all files at once as a zip file, but I want to be able to download the files individually. Can someone help me? if someone can help me he can either contact me here or write to me via Discord. My name is [FM]FredFox6 #2954
The uploading part is covered in this tutorial. You can zip the uploaded files using this:
https://adnan-tech.com/create-zip-file-with-password-php/
where is the database file?
how i will import to database?
There is no database related work in this article, it simply uploads the file and download the file.
Howdy! Would you mind if I share your blog with my twitter group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Thanks
Of course, you can.
Thanks for finally writing about > Upload, Download and Delete files – PHP – AdnanTech < Loved it!