# ENUM in MySQL

**URL:** <https://forum.kirupa.com/t/enum-in-mysql/34342>\
**Category:** programming\
**Created:** [October 31, 2003, 5:48pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342 "2003-10-31T17:48:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamza84](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@hamza84](https://forum.kirupa.com/u/hamza84)\
**Post date:** [October 31, 2003, 5:48pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/1 "2003-10-31T17:48:43Z")

</div>

Could anybody post an example of using MySQL’s ENUM option for specific file uploads please? Thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 31, 2003, 9:09pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/2 "2003-10-31T21:09:04Z")

</div>

SQL doesn’t handle the file upload, so there is no reason to use ENUM. ENUM is there to allow only specific values to be entered into that field of the database. (set is the same thing). What would you like to do and maybe we can go from there…?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 31, 2003, 9:14pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/3 "2003-10-31T21:14:31Z")

</div>

I would like to upload images to a database that are jpegs, pngs, and gifs only. I thought you could control this with the enum option as it allows only certain values. Guess I’m wrong.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 31, 2003, 9:25pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/4 "2003-10-31T21:25:36Z")

</div>

nah, you can’t really do that using ENUM… there are two ways to do that:

+check the file name(easy to hack, just rename the file)  
+check the actual contents of the file(harder to hack, harder to implement)

You could look into the GD (image) functions in PHP. Gif’s are a no-no though, because of copyright issues.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 31, 2003, 10:21pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/5 "2003-10-31T22:21:54Z")

</div>

well you would do a check in the actual upload script.

with PHP it would look like:

```php

if(substr($_FILES['userfile']['name'], -3) == "jpg"){
     // upload script here....
}

```

something like that would work for you…

you don’t actually upload the image into the database, you upload the path of the image to be stored in the database. But its not really necessary to make the SQL do the work in the case, because you still have to check the extension with PHP, its better to just include the extension-checker in the PHP code.

say you wanted to allow gifs, jpgs, and pngs…

```php

<?
$allowed_ext = ("jpg", "gif", "png");

$file = $_FILES['userfile'['tmp_name'];
$destination = $_FILES['userfile']['name'];

for($x=0;$x<count($allowed_ext);$x++){
     if(substr($dest, -3) != $allowed_ext[$x]){
         die("Not a valid file format.");
     }
}

copy($file, $destination) or die("Couldn't upload the file!");
?>

```

like njs said, this is pretty easy to hack tho. If you want you can actually check the file-type with a more complex function, but this will work if you don’t want something that is super secure.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 1, 2003, 3:18am UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/6 "2003-11-01T03:18:29Z")

</div>

Thanks a lot guys. could u give me a link where i could look up the more secure way in case i needed it?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 1, 2003, 8:25am UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/7 "2003-11-01T08:25:36Z")

</div>

actually Jubba, you can store images directly in a database 🙂

[http://phpbuilder.com/columns/florian19991014.php3](http://phpbuilder.com/columns/florian19991014.php3)

but it’s a lot easier to save the path.

I’m not absolutely positive how well this would work, but I was thinking something along the lines of this:

```php

$path = $_FILES["userfile"["tmp_name"]];
$im = @imagecreatefromgif($path);
if($im){
	$isValidGif = true;
}
imagedestroy($im);
if(!$isValidGif){
	$im = @imagecreatefromjpeg($path);
	if($im){
		$isValidJpeg = true;
	}
	
}
imagedestroy($im);
if(!($isValidGif && $isValidJpeg)){
 	$im = @imagecreatefrompng($path);
	if($im){
		$isValidPNG = true;
	}
}
imagedestroy($im);
if(!($isValidGif || $isValidJpeg || $isValidPNG)){
	die("Please upload a file of the right format.");
}
//do copy and everything like that here...

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 1, 2003, 5:45pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/8 "2003-11-01T17:45:37Z")

</div>

How would u save the path?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 1, 2003, 11:08pm UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/9 "2003-11-01T23:08:02Z")

</div>

njs: I didn’t say it wasn’t possible, just not really non-feasible. 🙂 Nice link tho. Also i don’t really see a reason to bring the GD-lib into thise problem. Well actually you only need getimagesize()… check out the link here:

[http://www.php.net/manual/en/function.getimagesize.php](http://www.php.net/manual/en/function.getimagesize.php)

that will tell you the type of the file and then you can run a check thru on that.

Hamza: You save the path by using the destination: $\_FILES[‘userfile’][‘name’] And you just place that value into the database.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 2, 2003, 12:24am UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/10 "2003-11-02T00:24:22Z")

</div>

Thanks a million Jubba :thumb:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 2, 2003, 8:16am UTC](https://forum.kirupa.com/t/enum-in-mysql/34342/11 "2003-11-02T08:16:35Z")

</div>

> \*Originally posted by Vash \*  
> \*\*njs: I didn’t say it wasn’t possible, just not really non-feasible. 🙂 Nice link tho. Also i don’t really see a reason to bring the GD-lib into thise problem. Well actually you only need getimagesize()… check out the link here:

[http://www.php.net/manual/en/function.getimagesize.php](http://www.php.net/manual/en/function.getimagesize.php)

that will tell you the type of the file and then you can run a check thru on that.

Hamza: You save the path by using the destination: $\_FILES[‘userfile’][‘name’] And you just place that value into the database. \*\*

Hmm, I didn’t know that. Thanks for letting me know 🙂
