upload fil script, 2+ filer!
Hey! Jeg har et upload script og køre på min site, og det fungerer fint. Nu vil jeg så gerne have lavet så jeg kan uploade mere end en fil til sitet, og til databasen. Jeg havde forestillet mig at man kunne uploade op til 3 filer, altså enten 1, 2 eller 3 :DJeg er ikke inde i hvordan GD lib fungerer, så smider en pæn stak points efter denne da jeg skal bruge det for at få lavet mit site færdigt =)
(deler points mellem folk der kommer med ledende svar)
Filen med upload form:
<?php
// START
?>
<?php include("include-top.php"); ?>
<?php
if ($_SESSION['user_level'] == "") {
echo "Du er ikke logged ind!";
}
else {
?>
<?php
$max_fil_storelse = "100000"; // i bytes
$billede_max_bredde = "1200"; // I pixels
$billede_max_hojde = "1200"; // Også i pixels
$fil_path = $_SERVER['DOCUMENT_ROOT'] . "/screenshots";
require($_SERVER['DOCUMENT_ROOT'] . "/image_resize.php");
require($_SERVER['DOCUMENT_ROOT'] . "/conn.php");
function form() {
global $max_fil_storelse;
$action = $_SERVER['PHP_SELF'];
?>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" class="site-tekst">
<tr>
<td height="75" valign="middle"><form method="post" action="<? echo $action ?>" enctype="multipart/form-data">
<p>Din kommentar til billedet (udfyld venligst).<br>
<textarea name="comment" cols="59" rows="10" class="form-field-blue" ></textarea>
<br>
Fil :
<input name="filen" type="file" class="form-field-blue" size="37">
*Max dimentioner på 1200x900!</p>
<p>Billede kategori:
<select name="kategori" class="form-field-blue" id="kategori">
<option value="1">Generelle screenshots</option>
<option value="2">Scores</option>
<option value="3">War screenshots</option>
<option value="4">Cheaters cought in action</option>
<option value="5">Diverse screenshots</option>
<option value="6">Screenshots fra andre spil</option>
</select>
</p>
<p> bruger:
<input name="forfatter" type="text" class="form-field-greay" id="forfatter" onclick="java script:alert('Du kan ikke ændre brugernavnet!');this.blur();" value="<?php print_r($_SESSION['username']);?>">
<br>
<br>
<input name="submit_fil" type="submit" class="form-button-white" value="Upload">
<input name="B2" type="reset" class="form-button-white" value="Slet alt">
</p>
</form></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><a href="screenshots.php?show=index" target="_top" class="10-bold-blue"><<
Tilbage til screenshot index</a> |</td>
</tr>
</table>
<?php
}
function upload() {
?>
<?php
global $fil_path, $vare_id;
$user_id = $_SESSION["user_id"];
$fil_path_temp = $fil_path . "/temp/" . $_FILES['filen']['name'];
if(!eregi(".jpg$|.jpeg$", $fil_path_temp)) { $error = "Det er ikke en jpg fil !"; }
if ($error) {
echo $error;
form();
} else { # nice, vi kan fortsætte
if (!@copy($_FILES['filen']['tmp_name'],$fil_path_temp)) {
echo $_FILES['filen']['tmp_name'];
echo "<br>";
echo $fil_path;
echo "\n<b>Tag dig sammen hehe!</b>";
form();
} else {
//image_resize($fil_path_temp, 300, 1000, $fil_path_temp);
$comment = $_POST["comment"];
$kategori = $_POST["kategori"];
$bruger_id = $_SESSION["user_id"];
mysql_query("INSERT INTO screenshots (bruger_id, kommentar, kategori, dato) values ('$bruger_id', '$comment', '$kategori', NOW())");
$id = mysql_insert_id();
$endeligt_navn = $fil_path . "/" . $id . ".jpg";
$endeligt_thumb_navn = $fil_path . "/tn" . $id . ".jpg";
rename($fil_path_temp, $endeligt_navn);
image_resize($endeligt_navn, 164, 123, $endeligt_thumb_navn);
$fraurl = $_SERVER['HTTP_REFERER'];
echo "<span class=\"site-tekst\">Screenshot tilføjet til databasen!</a><br><br>";
echo "<a href=\"screenshots.php?show=index\" class=\"10-bold-blue\" target=\"_top\"><< Tilbage til screenshots index!</a>";
}
}
?>
<?php
}
### Start koden ####
if (isset($_REQUEST['submit_fil'])) {
upload();
}else{
form();
}
?>
<?php
};
?>
<?php
// SLUT
?>
image-resize.php:
<?php
function image_resize($sourcefile,$maxwidth,$maxheight,$destinationfile=false)
{
$types=Array();
$types[1]='gif';
$types[2]='jpeg';
$types[3]='png';
$types[6]='wbmp';
list($width,$height,$type)=getimagesize($sourcefile);
$type_name=$types[$type];
$imagecreatefromtype='imagecreatefrom'.$type_name;
$imagetype='image'.$type_name;
if(!function_exists($imagecreatefromtype) || !function_exists($imagetype))
{
return false;
}
if($img_src=$imagecreatefromtype($sourcefile))
{
$ratio_height=$maxheight/$height;
$ratio_width=$maxwidth/$width;
$ratio=min($ratio_width,$ratio_height);
$newwidth=$width*$ratio;
$newheight=$height*$ratio;
$img_dst=imagecreatetruecolor($newwidth,$newheight);
// $img_dst=imagecreate($newwidth,$newheight);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// imagecopyresized($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if($destinationfile==false)
{
//header("Content-type: image/jpeg");
$imagetype($img_dst);
}
else
$imagetype($img_dst,$destinationfile);
imagedestroy($img_dst);
return true;
}
else
{
return false;
}
}
?>
