Return to the Main SiteBest Forums Ever!Chat, Chat, Chat!
Battery Acid Gaming Forums
March 28, 2024, 07:01:40 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Site restructure incoming?                                                   lol nope
 
  Home Help Arcade Gallery Leakybattery.com Chat Staff List Members Login Register  

"upload avatar" feature for your website!

Pages: [1]
  Print  
Author Topic: "upload avatar" feature for your website!  (Read 351 times)
JussiFX
D Battery
*
Posts: 81



View Profile
« on: November 27, 2009, 06:29:54 pm »

Okay I've made a script, written in PHP, that allows visitors to upload an avatar for your server.
Even if you're not interested, say what you think. You can test it by using the DEMO below.

Requirements: PHP, I guess works at least with PHP4 ->, but tested with PHP5. Also some basic knowledge
Features: Filesize restriction, Image resolution restriction, accepts only JPG/GIF/PNG images, shows random avatar of uploaded avatars and also information of it, shows statistics of avatars uploaded (number of avatars and their combined size). Easy to use, easy to configure. Prevents spamming of avatars (or at least makes it more difficult). If file of same name is already uploaded, scripts adds number from 0-9999 to beginning of the filename until there is no file with same name.
What sucks: All the text is finnish. I translate to english everything that shows in web page, but comments on code and variables might look odd Smiley Also I'm not sure about the security of this script: I'm no professional. So I suggest to run this on smaller websites. It uses 3 files, even if 2 files could have been possible.
Also not sure how it will look without the site's stylesheet (CSS). It shouldn't depend on it much.

DEMO: Click here
The site uses finnish text, so it's translated with Google Translator. I'm actually pretty surprised of the translation quality - it's almost completely right.
I'm using freehost, so the site might be slow at times. However I strongly recommend Byethost (google it) for your website.
ALSO NOTE THAT THE WEB PAGE DOESN'T SHOW PROPERLY ON INTERNET EXPLORER (tested versions 7 and below)

The files needed:
upload.php // this shows the upload form and random avatar (user interface)
Code:
<?php
include('uploadconfig.php');
if (
$_COOKIE['flood'] != "flood") {
?>

<h2>Avatar-Download</h2>
You can send your avatar to the server and use it on the internet!<br/>
However, there are some limitations:<br/>
-Filesize max. <?php echo ($maxsize 1024?>kb<br/>
-No larger avatars than <?php echo $maxwidth."x".$maxheight?><br/>
-You can only send PNG, GIF and JPG formats.<br/><br/>
<form enctype="multipart/form-data" action="doupload.php" method="POST">
<input name="tiedosto" type="file" style="width:200px;"><br/>
<input type="submit" value="Send" style="width:100px; letter-spacing:3px;">
</form>
<?php
}
else { echo 
"Wait 3min before sending again!<br/><br/>"; }
?>

<br/>Random avatar:<br/>
<?php
//Avataan hakemisto
$hak opendir($avatardir);

//Luetaan hakemisto muuttujaan
  
$nimi readdir($hak);
  while (
$nimi) {

  
$kuvat[] = $nimi;
  
$nimi readdir($hak);
  }

//Poistetaan "." ja ".." pois taulukosta
$maara count($kuvat);
$i 0;
while (
$i $maara) {
  if (
$kuvat[$i] == "." or $kuvat[$i] == ".." or $kuvat[$i] == "") {
unset($kuvat[$i]);
      
$i++;
      continue;
  }
  
$i++;
}
$maara count($kuvat);
//Näytetään Random Avatar
$random mt_rand(0$maara);
while ($kuvat[$random] == "") {
$random rand(0$maara);
}

echo " <img src=\"".$avatardir.$kuvat[$random]."\" alt=\"Loading...\"></img> ";
list(
$width$height) = getimagesize($avatardir.$kuvat[$random]);
$size filesize($avatardir.$kuvat[$random]);
$size $size 1024;
echo 
"<br/>".$width."x".$height." / <b>".round($size$ranavadec)."</b> kb<br/>";
echo 
"<i>http://".$_SERVER['HTTP_HOST'].$avatardir.$kuvat[$random]."</i><br/><br/>";
echo 
"Total avatars on the server: ".count($kuvat)."<br/>";
echo 
"Avatars take up space on the server ";
$kaikki = array();
for($i 0$i <= $maara$i++) {
$koko filesize($avatardir.$kuvat[$i]);
$kaikki[] = $koko;
}
$avamaara count($kaikki);
$yhteen 0;
for($i 0$i <= $avamaara$i++) {
$yhteen $yhteen $kaikki[$i];
}
$yhteen $yhteen 1024 1024;
echo 
round($yhteen$roundmb)."mb ("round($yhteen*1024$roundkb) ."kb)";
?>




doupload.php // does the actual file transfer
Code:
<?php
include('uploadconfig.php');

//Haetaan tietoja yksinkertaisempiin muuttujiin
$filename  $_FILES['tiedosto']['name'];
$filesize  $_FILES['tiedosto']['size'];
$filetmp  $_FILES['tiedosto']['tmp_name'];
$filetype  $_FILES['tiedosto']['type'];

//Tarkistetaan onko tiedosto oikeankokoinen
if($filesize $maxsize) {
die("Filesize is too big! (more than ".($maxsize 1024)."kb)");
}
//Tarkistetaan heti onko kuva liian iso
list($leveys$korkeus) = getimagesize($filetmp);
if($leveys $maxwidth or $korkeus $maxheight) {
die("Image resolution is too large! ($leveys x $korkeus)");
}
//Tarkistetaan onko tiedostoa olemassa. Jos on, lisätään satunnainen numero 0-9999 väliltä loppuun.
if (file_exists($polku.$filename)) {
while (file_exists($polku.$filename)) {
$random rand(0,9999);
$filename $random "_" $filename;
}
}
//Tarkistetaan onko tiedosto oikeantyyppinen
if($filetype == "image/jpeg" or $filetype == "image/gif" or $filetype == "image/png") {

//Siirretään tiedosto hakemistoon
move_uploaded_file($filetmp$polku $filename);
//Kerrotaan tiedot
echo "Image was uploaded succesfully!<br/><br/>
<img src=\"
$polku"."$filename\"></img><br/>
Address:
$sivu/$polku"."$filename<br/>";
$filesize $filesize 1024;
$filesize round($filesize$ranavadec);
echo "Your file's size was $filesize"."kb.";
//Estetään FLOOOOOOD asettamalla cookie
setcookie("flood""flood"time()+180); 
} else {
die(
"File isn't in right format! (jpg/gif/png)");
}
?>




And last, but certainly not least:
// Configuration file. Easy configuration to suit your needs! DON'T CHANGE ANYTHING WHAT STARTS WITH $ OR THE SCRIPT WILL NOT WORK! Be careful!
Only change the numbers and things in quotes ("quoted text"). Make sure you know what you're editing.
uploadconfigure.php
Code:
<?php
//Configure mostly UPLOAD.PHP
//Where the avatars are (eg. /avatars)
$avatardir "avatarit/"//this means avatars are found from www.example.com/avatarit
//How many decimals to show in filesize
$roundmb 2//In MB (used to show all server avatars size)
$roundkb 0//In kb (used to show all server avatars size)
$ranavadec 1//In kb (used to show random avatar's size)

//Configure mostly DOUPLOAD.PHP
//Path to this file
//Eg. If upload.php is in http://yoursite.com/upload.php, then enter:
//http://yoursite.com
$sivu "http://jussifx.byethost6.com";
//Path where to save the avatars
$polku "avatarit/";
//Max. filesize (in bytes... 40960=25kb).
//bytes / 1024 will give you kb
$maxsize 40960;
//Image Max Width and Height
$maxwidth 200;
$maxheight 200;
?>


And that's it!
Make sure upload.php, doupload.php and uploadconfiguration.php are in the same directory. Otherwise it won't work!
Also make sure you're webhost provides PHP support.
Report Spam   Logged

If you don't see it does it exist?

Share on Facebook Share on Twitter

Zaitrox
Former Tetris Champion of the World
Solar Farm
*
Posts: 2062


I fix computers while I drive my hated honda


View Profile WWW
« Reply #1 on: December 01, 2009, 06:40:56 pm »

Nice job!
Report Spam   Logged

Screw the money I have rules!
BigGumby
Community Manager
Nuclear Power Plant
*
Posts: 3616



View Profile
« Reply #2 on: March 13, 2010, 11:16:32 pm »

That's really good dude
Report Spam   Logged
Pages: [1]
  Print  
 
Jump to:  

Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy