php upload images

Unidentified

Living the Developer Life...
Jun 19, 2012
144
20
I cant find a script that uploads an image and updates a field in the database with the file location please help :/
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
PHP:
<?php
function uploadImage($img_ff, $dst_path, $dst_img){
 
    $dst_cpl = $dst_path . basename($dst_img);
    $dst_name = preg_replace('/\.[^.]*$/', '', $dst_img);
    $dst_ext = strtolower(end(explode(".", $dst_img)));
 
    while(file_exists($dst_cpl) == true){
        $i = $i+1;
        $dst_img = $dst_name . $i . '.' . $dst_ext;
        $dst_cpl = $dst_path . basename($dst_img);
    }
 
    move_uploaded_file($_FILES[$img_ff]['tmp_name'], $dst_cpl);
.
    $dst_type = exif_imagetype($dst_cpl);
 
    if(( (($dst_ext =="jpg") && ($dst_type =="2")) || (($dst_ext =="jpeg") && ($dst_type =="2")) || (($dst_ext =="gif") && ($dst_type =="1")) || (($dst_ext =="png") && ($dst_type =="3") )) == false){
        unlink($dst_cpl);
        die('<p>The file "'. $dst_img . '" with the extension "' . $dst_ext . '" and the imagetype "' . $dst_type . '" is not a valid image. Please upload an image with the extension JPG, JPEG, PNG or GIF and has a valid image filetype.</p>');
    }
}
 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
 
        $img_ff = 'image';
        $dst_img = strtolower($_FILES[$img_ff]['name']);
        $dst_path = '/directory/';
$website_url = 'http://yourwebsitehere.com';
 
        uploadImage($img_ff, $dst_path, $dst_img);
mysql_query("INSERT INTO images(file_location) VALUES('".$website_url . $dst_path . $dst_img ."')");
 
echo "Image uploaded succesfully!";
    }
?>

Code:
<form enctype="multipart/form-data" name="image" method="post" action="">
    <label for="image">Image:</label>
    <input type="file" id="image" name="image" />
    <br />
    <input type="submit" value="Upload" />&nbsp;<input type="reset" value="Reset" />
</form>
 

Users who are viewing this thread

Top