UrbanPro

Learn PHP from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I upload and download multiple type of files in PHP from MySQL database?

Asked by Last Modified  

2 Answers

Learn PHP

Follow 3
Answer

Please enter your answer

Software Developer | Freelancer | Trainer |

For upload <html lang="en"> <head> <meta charset="UTF-8" /> <title>Multiple File Ppload with PHP</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" id="file" name="files" multiple="multiple"...
read more

For upload

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8" />
  4. <title>Multiple File Ppload with PHP</title>
  5. </head>
  6. <body>
  7. <form action="" method="post" enctype="multipart/form-data">
  8. <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  9. <input type="submit" value="Upload!" />
  10. </form>
  11. </body>
  12. </html>

PHP CODE

  1. $valid_formats = array("jpg", "png", "gif", "zip", "bmp");
  2. $max_file_size = 1024*100; //100 kb
  3. $path = "uploads/"; // Upload directory
  4. $count = 0;
  5. if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
  6. // Loop $_FILES to exeicute all files
  7. foreach ($_FILES['files']['name'] as $f => $name) {
  8. if ($_FILES['files']['error'][$f] == 4) {
  9. continue; // Skip file if any error found
  10. }
  11. if ($_FILES['files']['error'][$f] == 0) {
  12. if ($_FILES['files']['size'][$f] > $max_file_size) {
  13. $message[] = "$name is too large!.";
  14. continue; // Skip large files
  15. }
  16. elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
  17. $message[] = "$name is not a valid format";
  18. continue; // Skip invalid file formats
  19. }
  20. else{ // No error found! Move uploaded files
  21. if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
  22. $count++; // Number of successfully uploaded file
  23. }
  24. }
  25. }
  26. }

Download FIle

  1. <?php
  2. function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
  3. {
  4. $zip = new ZipArchive();
  5. if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
  6. exit("cannot open <$archive_file_name>\n");
  7. }
  8. foreach($file_names as $files)
  9. {
  10. $zip->addFile($file_path.$files,$files);
  11. //echo $file_path.$files,$files."<br />";
  12. }
  13. $zip->close();
  14. header("Content-type: application/zip");
  15. header("Content-Disposition: attachment; filename=$archive_file_name");
  16. header("Pragma: no-cache");
  17. header("Expires: 0");
  18. readfile("$archive_file_name");
  19. exit;
  20. }
  21. require_once("config.php");
  22. $cqurfetch=mysql_query("SELECT * FROM albums where user_id='$user_id' and accept='1'");
  23. while($row = mysql_fetch_array($cqurfetch))
  24. {
  25. $file_names[] = $row['user_album_images'];
  26. }
  27. $archive_file_name=time().'.gallery.zip';
  28. $file_path="/uploads/";
  29. zipFilesAndDownload($file_names,$archive_file_name,$file_path);
  30. ?>
read less
Comments

Php Web Design and Development Tutor

In this you can upload any file which you want with change the file extention. Here is example for image. <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit"...
read more

In this you can upload any file which you want with change the file extention. Here is example for image.

 

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

 

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

 

read less
Comments

Related Questions

What is the best way to learn PHP without taking any coaching classes?
If you have prior knowledge on any server side programming definitely you can learn it on your own without attending classes. There are so many online tutorials to learn . Wish you all the best.
Kavidassan
0 0
6
What is PHP? How it can help me?
PHP is a programming language. Through this, you can make web-based software or website or even developed your own CMS, E-commerce site. Todays, most of the websites are on PHP
Test
0 0
7
PHP (LAMP) for entrepreneurs, is there any full time, fast track classes on PHP(LAMP) for entrepreneurs, who want are want to do some tech part in their startups
Yes, You have option. You can Join. Xpert Infotech, New Delhi. Before going through Silverlight live project training candidate should have knowledge of given concepts listed below: Knowledge of HTML...
Saee
0 0
6
How can we complete PHP language in 30 days?
First at all as a php trainer i would ask if you know html,csss,javascript nicely or not. As per me you should be at mediator level in those things to start php. Ok hopping you know that. Then go for some...
Veerendra
0 0
6

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

SQL Tips (1 to 3)
SQL tip 1: Instead of Full Outer Join, you can use Union All. Difference is, Full Outer Join provides results side by side. Union All provides result s one by one. Note: MYSQL does not support Full...

How to be a good developer?
I think practice more and read less.see what the logic in the programs then you must be a good developer
B

What is M.S.Project ?
MICROSOFT PROJECT contains project work and project groups, schedules and finances.Microsoft Project permits its users to line realistic goals for project groups and customers by making schedules, distributing...

How To Connect To Server Using PHP?
It is very easy to connect to server using PHP and MYSQL. PHP is a very friendly Programming Language that any one can learn who know the basics of the Front End Development. PHP is a programming Language...

Top Programming Languages 2017: Every Beginner Should Learn
Every year a plethora of job opportunities are being created for skilled programmers. So if you are thinking of honing your coding skills it is really a bright idea. But with so many programming languages...

Recommended Articles

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Applications engineering is a hot trend in the current IT market.  An applications engineer is responsible for designing and application of technology products relating to various aspects of computing. To accomplish this, he/she has to work collaboratively with the company’s manufacturing, marketing, sales, and customer...

Read full article >

Looking for PHP Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for PHP Classes?

The best tutors for PHP Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn PHP with the Best Tutors

The best Tutors for PHP Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more