Jump to content

What is the code for adding image to exisiting user


The_Dude_1978

Recommended Posts

Hi There,

 

I want to add a picture to an existing user. The sql looks like:

 

CREATE TABLE `klanten` (

  `id` int(4) unsigned NOT NULL auto_increment,

  `username` varchar(32) NOT NULL,

  `password` varchar(32) NOT NULL,

  `level` int(4) default '1',

  `filename` varchar(255) not null,

  `mime_type` varchar(255) not null,

  `file_size` int          not null,

  `file_data` mediumblob    not null,

 

  primary key (id),

  index (filename)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

 

 

id, username, password and level are allready filled in.

 

Now i need some code to go with

 

 

<form method="post" action="process.php" enctype="multipart/form-data">

                <div>

                    <input type="file" name="image" />

                    <input type="submit" value="Upload Image" />

                </div>

            </form>

 

 

process.php:

 

<?php
    require_once 'database.php';

    function assertValidUpload($code)
    {
        if ($code == UPLOAD_ERR_OK) {
            return;
        }

        switch ($code) {
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
                $msg = 'Image is too large';
                break;

            case UPLOAD_ERR_PARTIAL:
                $msg = 'Image was only partially uploaded';
                break;

            case UPLOAD_ERR_NO_FILE:
                $msg = 'No image was uploaded';
                break;

            case UPLOAD_ERR_NO_TMP_DIR:
                $msg = 'Upload folder not found';
                break;

            case UPLOAD_ERR_CANT_WRITE:
                $msg = 'Unable to write uploaded file';
                break;

            case UPLOAD_ERR_EXTENSION:
                $msg = 'Upload failed due to extension';
                break;

            default:
                $msg = 'Unknown error';
        }

        throw new Exception($msg);
    }

    $errors = array();

    try {
        if (!array_key_exists('image', $_FILES)) {
            throw new Exception('Image not found in uploaded data');
        }

        $image = $_FILES['image'];

        // ensure the file was successfully uploaded
        assertValidUpload($image['error']);

        if (!is_uploaded_file($image['tmp_name'])) {
            throw new Exception('File is not an uploaded file');
        }

        $info = getImageSize($image['tmp_name']);

        if (!$info) {
            throw new Exception('File is not an image');
        }
    }
    catch (Exception $ex) {
        $errors[] = $ex->getMessage();
    }

    if (count($errors) == 0) {
        // no errors, so insert the image

        $query = sprintf(
            "insert into klanten (filename, mime_type, file_size, file_data)
                values ('%s', '%s', %d, '%s')",
            mysql_real_escape_string($image['name']),
            mysql_real_escape_string($info['mime']),
            $image['size'],
            mysql_real_escape_string(
                file_get_contents($image['tmp_name'])
            )
        );

        mysql_query($query, $con);

        $id = (int) mysql_insert_id($con);

        // finally, redirect the user to view the new image
        header('Location: view.php?id=' . $id);
        exit;
    }
?>
<html>
    <head>
        <title>Error</title>
    </head>
    <body>
        <div>
            <p>
                The following errors occurred:
            </p>

            <ul>
                <?php foreach ($errors as $error) { ?>
                    <li>
                        <?php echo htmlSpecialChars($error) ?>
                    </li>
                <?php } ?>
            </ul>

            <p>
                <a href="upload.php">Try again</a>
            </p>
        </div>
    </body>
</html>

 

to add to an existing user.

 

Can somebody help me here?

 

Best regards,

 

 

Martijn

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.