webForumDet fria alternativet

PHP upload

3 svar · 397 visningar · startad av -mjansson-

-mjansson-Medlem sedan mars 200541 inlägg
#1

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

PeeerMedlem sedan mars 20025 362 inlägg
#2

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

-mjansson-Medlem sedan mars 200541 inlägg
#3

Tack :)
så denna kod funkar då?

<?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>
";
?>
@ndersMedlem sedan juni 200026 914 inlägg
#4

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

Genererad på 382 ms · cache AV · v20260730165559-full.f96bc7eb