Good image uploader with thumbnails?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

I want to make a portfoliosystem where user can register and get their
own portfolio... I've started the developer work, but I'm stuck on the
image upload part... I'm experiencing some problems getting the picture
resized and thumbnailed... I'm on a apache server running php 5 with
8MB php_memory.

When uploading, the script works fine most times when uploading small
files (below 100kb and sometimes up close to 300kb too), but when
uploading files bigger than 300kb it only reloads the page, and the
form fields are empty.

Does any of you know of a smoothly working script for uploading,
resizing and thumbnailing? I need it to write a line to the database
too, but that's no problem writing... Excuse my bad english, but I'm
norwegian :)

If you're intrested, the code I have now is pasted below:



//Last inn global konfigurasjonsfil - denne filen må finnes for at
systemet skal laste
    echo("global_config.php finnes ikke");

//Last inn globale funksjoner

    echo("global_functions.php finnes ikke");

//Hent ut brukernavn

//Koble til databse


$dir = "temp/"; //Change this to the correct dir  RELATIVE TO WHERE
THIS SCRIPT IS, or /full/path/

//MIME types to allow, Gif, jpeg, zip ::Edit this to your liking
$types =

// Nothing to edit below here.

//Opprett thumbnail-funksjonen

function endre($name,$filename,$new_w,$new_h){
    if (preg_match('/jpg|jpeg/',$system[1])){
    if (preg_match('/png/',$system[1])){

    if ($old_x > $old_y) {
    if ($old_x < $old_y) {
    if ($old_x == $old_y) {


    if (preg_match("/png/",$system[1]))
    } else {

//Check to determine if the submit button has been pressed
if(isset($_POST['submit'])) {

    //Shorten Variables
    $tmp_name = $_FILES['upload']['tmp_name'];
    $new_name = $_FILES['upload']['name'];
    $cat_id = $_POST['cat_id'];
    $tittel = $_POST['tittel'];
    $beskrivelse = $_POST['beskrivelse'];
    $fullpath = "$dir$path/";
    $temppath = "bilder/";
    $fullpath = str_replace("..", "", str_replace("\.", "",
str_replace("//", "/", $fullpath)));
    $clean_name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_",
str_replace("%20", "_", strtolower($new_name) ) ) );

// lets see if we are uploading a file or doing a dir listing
        //Sjekk MIME-typen
        if ((in_array($_FILES['upload']['type'], $types)) and

            //Opprett mapper som er nødvendige
            if (!is_dir($fullpath)){
                mkdir("$fullpath", 0777);
            if (!is_dir("thumb/")){
                mkdir("thumb/", 0777);
            if (!is_dir("temp/")){
                mkdir("temp/", 0777);
            //Flytt filen til /temp-mappen
            move_uploaded_file($tmp_name,$fullpath . $clean_name);


            //Slett originalfilen
            $ourFileName = "temp/$clean_name";
            $ourFileHandle = fopen($ourFileName, 'w') or die("kunne ikke
&aring;pne fil for sletting");

            $query = "INSERT INTO `portfolio_brukere_doffer` (`id`, `cat_id`,
`url`, `url_full`, `tittel`, `beskrivelse`, `om`) VALUES ('',
'$cat_id', 'thumb/thumb_$clean_name', 'bilder/$clean_name', '$tittel',
'$beskrivelse', '')";
            mysql_query($query) or die('Error, insert query failed');
            echo "$clean_name ble lagt til";


            //Print Error Message
            echo "<small>Filen
<strong><em></em></strong> finnes fra
f&oslash;r av</small><br />";
            $name =  $_FILES['upload']['name'];
            $type =    $_FILES['upload']['type'];
            $size =    $_FILES['upload']['size'];
            $tmp =     $_FILES['upload']['name'];

            echo "Navn: $name<br />Type: $type<br />St&oslash;rrelse: $size<br
/>Tmp: $tmp";

        } */

} else {
    echo 'Ready to upload your file';
} ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"

<legend>Last opp bilde</legend>
<table border="0" cellspacing="0" cellpadding="0">
    <td>Bilde som skal lastes opp </td>
      <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
      <input type="file" name="upload" />&nbsp;</td>
    <td><input name="tittel" type="text" maxlength="100">&nbsp;</td>
        <select name="cat_id">
        <option selected>Velg en kategori</option>
        <option>- - -</option>
        $kategorier = "SELECT * FROM `portfolio_cat` LIMIT 0 , 100";
        $cat_resultat = mysql_query($kategorier);
        while ($row= @mysql_fetch_array($cat_resultat)) {
        $cat_id        = $row["id"];        //hent ut kategori-id
        $cat_navn    = $row["name"];        //hent ut kategori-navn

        //Send informasjonen ut til leseren

        echo("<option value=\"$cat_id\">$cat_navn</option>");

          $count++ ;
    <td><textarea name="beskrivelse"></textarea></td>

<input type="submit" name="submit" value="Last opp" />

Site Timeline