$image['name']

Status
Not open for further replies.

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Hi,

So, I made an script (first in dutch), and when I translated it, I somewhere made a mistake, and now I can't find it anymore. The problem is: it uploads the image, and it inserts the UPLOAD_MAP_IMAGES in the database, but it doesn't get the $image['name'] and $image['size']; anymore. I hope you guys can help me out.

The code:
PHP:
                <?php
 
define('SITE_URL', 'http://www.'. $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) .'/');
 
define('MAX_SIZE', 5);
 
define('MAX_LENGTH', 32);
 
//define('UPLOAD_MAP', '../images/');
 
define('SUFFIX', '_thumb');
 
define ('UPLOAD_MAP_IMAGES', 'http://www.mywebsite.com/map/images/');
 
function upload_image($image = null, $thumbnail = false)
{
    if (is_array($image))
    {
        if ($image['size'] > 0 && substr($image['type'], 0, 5) == 'image')
        {
            if ($image['size'] <= (MAX_SIZE * 1048576))
            {
                if (is_uploaded_file($image['tmp_name']))
                {
                    if ($image['size'] < 1024)
                    {
                        $size = $image['size'] .' bytes';
                    }
                    elseif ($image['size'] < 1048576)
                    {
                        $size = round($image['size'] / 1024, 2) .'KB';
                    }
                    elseif ($image['size'] > 1048576)
                    {
                        $size = round($image['size'] / 1048576, 2) .'MB';
                    }
                    else
                    {
                        $size = $image['size'] .' bytes';
                    }
                   
                    $file = strtolower($image['name']);
                    $filename = substr($file, 0, strrpos($file, '.'));
                    $edited = preg_replace('/[^a-z0-9]+/i', '-', $filename);
                    $suffix    = ($thumbnail == true) ? SUFFIX : null;
                    $extension = strrchr($file, '.');
                    $length = (MAX_LENGTH - (strlen($suffix) + strlen($extension)));
                    $name = substr($edited, 0, $length) . $suffix . $extension;
                   
                    if (move_uploaded_file($image['tmp_name'], UPLOAD_MAP . $name))
                    {
                        return array(    'naam' => $name,
                                        'grootte' => $size,
                                        'grootte_bytes' => $image['size']);
                    }
                    else
                    {
                        return 'Bestand <u>'. $name .'</u> werd niet verplaatst naar de map <u>'. UPLOAD_MAP .'</u>';
                    }
                }
                else
                {
                    return 'De TMP-directory van de server functioneert wellicht niet goed<br /><pre>'. print_r($image) .'</pre>';
                }
            }
            else
            {
                return 'Afbeelding is te groot (maximaal '. MAX_SIZE .'MB toegestaan)';
            }
        }
        else
        {
            return 'Afbeelding is niet van het juiste type (enkel afbeeldingen zijn toegestaan)';
        }
    }
    else
    {
        return 'Er is geen afbeelding geselecteerd';
    }
}
 
?>
 
<form enctype="multipart/form-data" method="post" action="addimage.php">
 
<strong>Afbeelding beschrijving:</strong><br />
<input type="text" name="description" /><br />
<br />
 
<strong>Welke pagina:</strong><br />
<select name="page_id" id="page_id">
<option value="1">Tekeningen</option>
<option value="2">Andere pagina</option>
</select><br />
<br />
 
<strong>Afbeelding:</strong><br />
<input type="file" name="image" /><br />
<br />
 
<strong>Thumbnail:</strong><br />
<input type="file" name="thumbnail" /><br />
<br />
 
<input type="submit" value="Tekeningen toevoegen" />
 
</form>
 
<?php
 
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $image        = upload_image($_FILES['image']);
    $thumbnail  = upload_image($_FILES['thumbnail'], true);
   
    $upload_image = UPLOAD_MAP_IMAGES . $image['name'];
    $upload_thumbnail = UPLOAD_MAP_IMAGES . $thumbnail['name'];
    $upload_description = $_POST['description'];
    $upload_page_id = $_POST['page_id'];
    $upload_date = date("d-m-y");
   
    if (is_array($image))
    {
        echo 'Uploaden van afbeelding <a href="'. SITE_URL . UPLOAD_MAP . $image['name'] .'" target="new">'. $image['name'] .' ('. $image['size'] .')</a> is geslaagd.<br />' . PHP_EOL;
 
        $query = "INSERT INTO `images` (`page_id`, `image_url`, `thumbnail_url`, `description`, `post_date`) VALUES ('".$upload_page_id."', '".$upload_image."', '".$upload_thumbnail."', '".$upload_description."', '".$upload_date."')";
        //$log_query = "INSERT INTO `logs` (`action`, `executed_by`, `executed_on`, `extra_info`, `execution_date`) VALUES ('Afbeelding toegevoegd', '$username', '".$upload_image."<br />".$upload_thumbnail."', '', '".$upload_date."')";
        mysql_query($query) or die(mysql_error());
        //mysql_query($log_query) or die(mysql_error());
    }
    else
    {
        echo 'Foutmelding: '. $image .'.<br />' . PHP_EOL;
    }
   
    if (is_array($thumbnail))
    {
        echo 'Uploaden van afbeelding <a href="'. SITE_URL . UPLOAD_MAP . $thumbnail['name'] .'" target="new">'. $thumbnail['name'] .' ('. $thumbnail['size'] .')</a> is geslaagd.<br />' . PHP_EOL;
    }
    else
    {
        echo 'Foutmelding: '. $thumbnail .'.<br />' . PHP_EOL;
    }
}
 
?>

Thanks.
 
Status
Not open for further replies.

Users who are viewing this thread

Top