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

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 thisyou can upload any file which you want with change the file extention. Here is example for image. <formaction="upload.php"method="post"enctype="multipart/form-data"> Select image to upload:<inputtype="file"name="fileToUpload"id="fileToUpload"><inputtype="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 imageif(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 existsif(file_exists($target_file)) {echo"Sorry, file already exists."; $uploadOk =0;}// Check file sizeif($_FILES["fileToUpload"]["size"] >500000) {echo"Sorry, your file is too large."; $uploadOk =0;}// Allow certain file formatsif($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 errorif($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

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 <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" accept="image/*" /> <input type="submit" value="Upload!" /> </form> </body> </html> PHP CODE $valid_formats = array("jpg", "png", "gif", "zip", "bmp"); $max_file_size = 1024*100; //100 kb $path = "uploads/"; // Upload directory $count = 0; if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ // Loop $_FILES to exeicute all files foreach ($_FILES['files']['name'] as $f => $name) { if ($_FILES['files']['error'][$f] == 4) { continue; // Skip file if any error found } if ($_FILES['files']['error'][$f] == 0) { if ($_FILES['files']['size'][$f] > $max_file_size) { $message[] = "$name is too large!."; continue; // Skip large files } elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){ $message[] = "$name is not a valid format"; continue; // Skip invalid file formats } else{ // No error found! Move uploaded files if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) $count++; // Number of successfully uploaded file } } } } Download FIle <?php function zipFilesAndDownload($file_names,$archive_file_name,$file_path) { $zip = new ZipArchive(); if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) { exit("cannot open <$archive_file_name>\n"); } foreach($file_names as $files) { $zip->addFile($file_path.$files,$files); //echo $file_path.$files,$files."<br />"; } $zip->close(); header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=$archive_file_name"); header("Pragma: no-cache"); header("Expires: 0"); readfile("$archive_file_name"); exit; } require_once("config.php"); $cqurfetch=mysql_query("SELECT * FROM albums where user_id='$user_id' and accept='1'"); while($row = mysql_fetch_array($cqurfetch)) { $file_names[] = $row['user_album_images']; } $archive_file_name=time().'.gallery.zip'; $file_path="/uploads/"; zipFilesAndDownload($file_names,$archive_file_name,$file_path); ?> read less
Comments

Related Questions

Do you have any project based training program in Bhubaneswar ?
Yes, I can provide you real time project training in php.
Priyasha
What is PHP? How it can help me?
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP...
Test
0 0
7
What are global variable in php ?
A variable declared outside a function is global
Ajay

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

Ask a Question

Related Lessons

How to Create A Master Page Template In PHP?
A master page template is essential to give a consistent look and feel to any website having multiple pages. It is quite easy to create a master page template in PHP using Dreamweaver. Let’s have...

User Authenication In PHP: Some Advanced echniques
User Authentication in CodeIgniter: Goals Security: We want our login system to be secure, we want to do everything we can to stop people’s accounts being hacked into. Tracking: We’d like...

What Is PHP?
PHP stands for PHP: Hypertext Preprocessor PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use PHP files can contain text,...

PHP
It is the base for many of the ecommerce websites. Many of the CMS develpoped in PHP platform.If programming cant done inititally try to think for logics and develop writing algorithms. Any doubts divyaravindiran@gmail.com.

Components of Digital Marketing
Content marketing Search Engine Optimization (SEO) Paid Search (PPC) Email marketing Social Media Marketing Digital Display Marketing (Ad Ops) Web Analytics and Reporting Mobile Marketing 30...
G

Geek M

0 0
0

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