Page 1 of 1

Uploading images into MySQL - help

Posted: Thu Apr 16, 2009 12:01 pm
by anivad
Trying to have a system where users can upload an avatar; I went through several online tutorials on image uploading but they all seem to have different ways of doing it. So I read a few of those, came up with my own code, and naturally it didn't work.

Any suggestions on how to edit my code to make it functional?

($uname and the error function have been earlier defined.)

Code: Select all

<?php
 
if($_SERVER['REQUEST_METHOD'] == 'POST') {
 
$maxsize == 1000000;
 
    if(is_uploaded_file($_FILES['avatar'])) {
        $avatar = addslashes(file_get_contents($_FILES['avatar']));
        $size = getimagesize($_FILES['avatar']);
        if($size > $maxsize) {
            error('File exceeds maximum size');
            }
            else {
        include 'db.php';
 
$sql = "UPDATE logintest SET avatar='$avatar' WHERE uname = '$uname' ";
$result = mysql_query($sql) or die (mysql_error());
$num_rows = mysql_num_rows($result);
 
    if ($result) {      
        error('File uploaded');
        header("Location: loginsuccess.htm");
        }
    else {
    error('Error uploading file. Please try again.');
    }
}
}
}
 
 
?>
 
<form enctype="multipart/form-data" name="avatarform" method="post" action="scriptchangeavatar.php">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<table border="0">
<tr>
<td align="right"><b>Choose a file to upload</b></td><td><input type="file" name="avatar"></td></tr>
<tr><td align="right" colspan="2"><input type="submit" name="avatarupload" value="Upload"></td></tr></table>
</form>
Also, how do I restrict uploaded files to a certain size e.g. 100x100 pixels? The current getimagesize function only seems to work for file size in bytes.

Thanks!

Re: Uploading images into MySQL - help

Posted: Fri Apr 17, 2009 11:29 pm
by skullmunky
I think that ...

$_FILES['avatar'] is actually an array, containing the supplied name of the file, the temporary file, and the filetype. Your code is probably ending up with an empty or bogus filename for the image, and no actual file.

Also file_get_contents reads the entire contents of the file into a single string variable, so that also might be trying to open the actual image file as a text file and inserting slashes into it, or something :P

You maybe want something like:

Code: Select all

 
$avatar = addslashes($_FILES['avatar']['name];
copy($_FILES['avatar']['tmp_name'],$avatar);
$sql = "UPDATE logintest SET avatar='$avatar' WHERE uname = '$uname' ";
 
for the size: getimagesize() returns an array which includes the size in bytes, but it should include the dimensions as well. as a string, so split and parse.

Re: Uploading images into MySQL - help

Posted: Fri Apr 17, 2009 11:54 pm
by anivad
Tried that, but got the error message:

failed to open stream: Permission denied in /paththingy/scriptchangeavatar.php on line 11

where line 11 =

copy($_FILES['avatar']['tmp_name'],$avatar);

Did you leave out a ' and ) at the end of the first line?
for the size: getimagesize() returns an array which includes the size in bytes, but it should include the dimensions as well. as a string, so split and parse.
How do I do that? PHP newbie here. :(