How to upload images, audio files, video files using single form and store their names in appropriate tables?

database

CREATE TABLE IF NOT EXISTS `photos` (
  `id` bigint(12) NOT NULL AUTO_INCREMENT,  
  `name` varchar(250) NOT NULL,  
  PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `audios` (
  `id` bigint(12) NOT NULL AUTO_INCREMENT,  
  `name` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `videos` (
  `id` bigint(12) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) NOT NULL,
   PRIMARY KEY (`id`)
)

form.php

<form enctype="multipart/form-data" name="file_upload" method="post" action="insert.php">  
  <input type="file" name="file" />
</form>

Recommended Answers

All 2 Replies

Member Avatar for diafol

you can use: $_FILES['file']['type'] or even:

$ext = pathinfo($_FILES['file']['tmp_name'], PATHINFO_EXTENSION);

It's then just a matter of:

switch(true){
  case in_array($ext,array("jpg","jpeg","png","gif","svg")):
   $sqltable = "photos";
   break;
  case in_array($ext,array("mpg","mp4","flv","avi","wmv")):
   $sqltable = "videos";
   break;
  case in_array($ext,array("mp3","wav")):
   $sqltable = "audios";
   break;
}

if(isset($sqltable)){
   $r = mysql_query("INSERT INTO `$sqltable` ...");
}

IMPT - not tested and data not validated and cleaned
Also values for photos etc, just off top of my head, you'll obviously have a better idea of which extensions you want to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.