---
title: "PHP upload"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/127603-php-upload"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "-mjansson-"
published: "2005-05-03T17:28:33.000Z"
updated: "2005-05-03T17:46:52.000Z"
replies: 3
views: 397
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/127603-php-upload"
---

# PHP upload

## #1 — -mjansson-, 2005-05-03T17:28Z

Hej!

Kan man ladda upp saker till en mapp samtidigt som man lägger in text i en databas? eller måste man använda två separata formulär?

snabba svar uppskattas =)

//Martin

Permalänk: https://www.webforum.nu/p/127603

## #2 — Peeer, 2005-05-03T17:37Z

Ja det går. Fildatan ligger i $\_FILES och textdatan ligger i $\_POST

Permalänk: https://www.webforum.nu/p/1605428

## #3 — -mjansson-, 2005-05-03T17:41Z

Tack :)
så denna kod funkar då?

```php
<?php
session_start();
echo "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
<html>
<head>
<title></title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />
</head>
<body>
<center>
";
//¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤\\

###
# Config
###
$host 		= "localhost"; # MySQL host name usualy localhost
$username 	= ""; # Username in order to connect to database
$password 	= ""; # Password to authenticate with database
$database	= ""; # Database to choose
$prefix 	= ""; # Same as in the SQL file
@mysql_connect($host,$username,$password) or die("Connection to databse faiure");
@mysql_select_db($database) or die("Choice of database failure");
switch($_GET['act'])
{
default:
 display_addnews($prefix);
break;
}

function display_addnews($prefix){
switch($_POST['addnews'])
{
case 'insert':
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, gif, png, pdf"; // These are the allowed extensions of the files that are uploaded
$max_size = "50000"; // 50000 is the same as 50kb
$max_height = "100"; // This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "100"; // This is in pixels - Leave this field empty if you don't want to upload images 

// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
_if ($allowed_paths[$i] == "$extension") {
_$ok = "1";
_}
}
// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big!";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
}

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
} else {
print "Incorrect file extension!";
}
if(mysql_query("INSERT INTO ".$prefix."namn (`namn`,`nick`,`mail`,`timestamp`) VALUES ('".$_POST['namn']."','".$_POST['nick']."','".$_POST['mail']."','".time()."')"))
{
echo "Tillagd <br />";
}
else {
echo mysql_error();
}
break;
default:
echo "
<form method=\"post\">
<span class=\"news_brod\">Namn:<br />
<input type=\"text\" name=\"namn\" /><br />
Nick: <input type=\"text\" name=\"nick\" /><br />
Mail: <input type=\"text\" name=\"mail\" />
Bild: <input type=\"file\" name=\"bild\" value=\"V&auml;lj bild.\" >
<input type=\"submit\" name=\"submit\" value=\"L&auml;gg till\" /><br />
<input type=\"hidden\" name=\"addnews\" value=\"insert\" /><br />
</span></form>
";
break;
}
}
echo "
</center>
</body>
</html>
";
?>
```

Permalänk: https://www.webforum.nu/p/1605430

## #4 — @nders, 2005-05-03T17:46Z

Om du ska skicka filer från ett formulär måste du ha *enctype="multipart/form-data"* i din form-tagg.

Permalänk: https://www.webforum.nu/p/1605432

---

Tråden på webben: https://www.webforum.nu/amne/php/127603-php-upload
