0

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>
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by diafol
1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.