The site I'm putting together will house the ability to upload a picture as an avatar. I used some pretty wild code to do it all, as I'm pretty new to the game. The essence of what happens in steps:
1) User uploads a file that must be jpg, png, or gif
2) The file is renamed to the user's name based off the cookie and stored in a dir/
3) The name of the file extension is captured into a table
4) The site then shows the user's avatar
I'm hoping some of you more versed coders could input on your own methodologies on how to implement what I've done a bit better. Here are my codes and some explanations.
Upload.php
<?php
include('header.php');echo
'<html>
<body>
Change '.$name.'s avatar. <br />
<form action="upload_file.php" method="post" enctype="multipart/form-data"><br />
<label for="file">toast:</label><br />
<input type="file" name="';echo $name;echo'" /><br />
<input type="submit" name="submit" value="Upload File" />
</form>';
?>
Simple. Effective. Only thing to note is I've used the cookie info to identify the name of the file. The intent was to make this safer and prevent unauth'd uploads.
header.php (parts)
//ava dir
$avatar_dir = 'avatars/';
//$_FILES info
$tmp_name = $_FILES[$name]["tmp_name"];
$avatarftype = $_FILES[$name]["type"];
//Break up generated ['type'] to extract image type only
$fext = explode("image", $avatarftype);
$fext = str_replace("/", ".", $fext);
$ava = $fext[1];
//Combine user name w/ extension and store it in db
$myava = ($name.$ava);
$avatarpull = mysql_query("SELECT avatar FROM content WHERE uname = '$name'");
// Fetch avatar for use
$avatype = mysql_fetch_assoc($avatarpull);
$avatar = $avatype["avatar"];
I assign all the vars and arrays in my included header file, so i have easy access while I'm creating the site. Any explanations are below.
Upload-file.php
<?php
include('header.php');
// Determine file types and size
if ((($_FILES[$name]["type"] == "image/gif")
|| ($_FILES[$name]["type"] == "image/jpeg")
|| ($_FILES[$name]["type"] == "image/pjpeg")
|| ($_FILES[$name]["type"] == "image/png"))
&& ($_FILES[$name]["size"] < 60000)){
//Notify if error
if ($_FILES[$name]["error"] > 0){
echo "Return Code: " . $_FILES[$name]["error"] . "<br />";
}else{
// Upload it to server and log into database
move_uploaded_file($tmp_name, $avatar_dir.$name.$ava);
mysql_query("UPDATE content SET avatar = '$ava2' WHERE uname = '$name'");
}
//Kick out
}else{
echo 'Invalid file,';
}
?>
I kept getting awkward results when trying to rename the file to the user's name, so I had to explode and replace a little bit. This dropped 'image/png' from the file type, and left me with just png (or jpeg/gif). After that was done, the file was uploaded into the server with their name, and into the database. This prevents upload abuse and makes the call later on easier.
So when user 'John', uploads 'funnyhaha.gif' into the server, it gets renamed to "John.gif" and stored into the db. All i have to do is invoke the vars into the page as so to display his avatar:
<img src="'.$avatar_dir.$avatar.'" height="100px" width="100px" />
What you guys think?