Show DevBest [PHP/OOP] Image Uploader by m0nsta.

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
This is just a quick and simple 30 minute code which I have been doing.

To get this working, all you have to do is save the code below as a PHP file, edit the $uploadTarget variable. When you change this variable, make sure that you create the folder too or you may receive a PHP error.

You can also edit the $maxSize variable if you wish to.

You can do whatever you wish to with this script, but please leave credits.. :)

Features:
- checks file size
- checks file extension
- checks if file exists
- checks if your $uploadTarget is writeable or not

index.php
PHP:
<?php
/*
Simple PHP OOP Image Upload script by m0nsta.
*/
class imgUploader
{
	var $exts = array( ".png", ".gif", ".png", ".jpg", ".jpeg" ); //all the extensions that will be allowed to be uploaded
	var $maxSize = 9999999; //if you set to "0" (no quotes), there will be no limit
	var $uploadTarget = "uploads/"; //make sure you have the '/' at the end
	var $fileName = ""; //this will be automatically set. you do not need to worry about this
	var $tmpName = ""; //this will be automatically set. you do not need to worry about this
	
	public function startUpload()
	{
		$this->fileName = $_FILES['uploaded']['name'];
		$this->tmpName = $_FILES['uploaded']['tmp_name'];
		if( !$this->isWritable() )
		{
			die( "Sorry, you must CHMOD your upload target to 777!" );
		}
		if( !$this->checkExt() )
		{
			die( "Sorry, you can not upload this filetype!" );
		}
		if( !$this->checkSize() )
		{
			die( "Sorry, the file you have attempted to upload is too large!" );
		}
		if( $this->fileExists() )
		{
			die( "Sorry, this file already exists on our servers!" );
		}
		if( $this->uploadIt() )
		{
			echo "Your file has been uploaded!<br><br>Click <a href=\"" . $this->uploadTarget . time() . $this->fileName . "\">here</a> to view your file!";
		}
		else
		{
			echo "Sorry, your file could not be uploaded for some unknown reason!";
		}
	}
	
	public function uploadIt()
	{
		return ( move_uploaded_file( $this->tmpName, $this->uploadTarget . time() . $this->fileName ) ? true : false );
	}
	
	public function checkSize()
	{
		return ( ( filesize( $this->tmpName ) > $this->maxSize ) ? false : true );
	}
	
	public function getExt()
	{
		return strtolower( substr( $this->fileName, strpos( $this->fileName, "." ), strlen( $this->fileName ) - 1 ) );
	}
	
	public function checkExt()
	{
		return ( in_array( $this->getExt(), $this->exts ) ? true : false );
	}
	
	public function isWritable()
	{
		return ( is_writable( $this->uploadTarget ) );
	}
	
	public function fileExists()
	{
		return ( file_exists( $this->uploadTarget . time() . $this->fileName ) );
	}
}
$img = new imgUploader();



if( $_POST['upload_file'] )
{
	$img->startUpload();
}
else
{
	echo "<form method=\"post\" enctype=\"multipart/form-data\">
		<p>
			<label for=\"file\">Select a file to upload:</label> <input type=\"file\" name=\"uploaded\" id=\"file\"><br>
			<input type=\"submit\" name=\"upload_file\" value=\"Upload!\">
		<p>
	</form>";
}
?>

Live demo:


Tips:
Put a blank index.html file in your $uploadTarget folder so that people can't view the uploaded files, this is up to you.

If you would like to view the files what have been uploaded to my site, click .

If you choose to use this, I hope you like it.

Thanks,
- m0nsta.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Nice
With some styling, that would be cool :)

I'll leave that to the people who choose to use it. The one I have created is just temporary, I'm not bothered how it looks :)
 

Mac

New Member
Feb 9, 2011
111
2
I prefer you using "public", "private", "static" and "protected" , not "var" !
PHP:
public $you; private $me; protected $he; static $she;

Also it is better if you throw new exception instead of dieing.
PHP:
 throw new exception("Error"); /* If you wanna throw new invalid exception then do that : */ throw new invalidargumentexception("Error");

Otherwise : Very good! 10/10
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
'var' and 'public' are pretty much the same thing, so do some research before you state things

Nice release, really love it.
 

Mac

New Member
Feb 9, 2011
111
2
i know , but standard is public , private , protected and static (don't be against this , this is what php.net says)

my post above edited!
 

Bazinga

Posting Freak
Aug 3, 2010
819
54
Something I just did ;P

Replace

PHP:
echo "Your file has been uploaded!<br><br>Click <a href=\"" . $this->uploadTarget . time() . $this->fileName . "\">here</a> to view your file!";

With

PHP:
echo "Your file has been uploaded!<br><br>
Direct Link: <input name=\"direct\" size=\"70\" readonly=\"readonly\" value=\"http://". $_SERVER['HTTP_HOST'] ."/" . $this->uploadTarget . time() . $this->fileName . "\" / ><br><br>
BB Code: <input name=\"forum\" size=\"70\" readonly=\"readonly\"  value=\"[IMG]http://". $_SERVER['HTTP_HOST'] ."/" . $this->uploadTarget . time() . $this->fileName . "[/IMG]\" / ><br><br>
HTML Code: <input name=\"html\" size=\"70\" readonly=\"readonly\"  value=\"<img src="http://". $_SERVER['HTTP_HOST'] ."/" . $this->uploadTarget . time() . $this->fileName . "" />\" /><br><br>
or click <a href=\"" . $this->uploadTarget . time() . $this->fileName . "\">here</a>";

Gives:
6qp0l2


I'm crap at PHP so if you don't like, I don't want to know!
 

Jo$h

Posting Freak
Jul 7, 2010
1,030
79
On: Very Nice! I have been looking for one of these for a long time but the ones I find are always just too complicated.
OFF: Every Coder codes differently, if you don't like how he coded the script, don't use it.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I don't like the look of 'throw new exception', it just seems to do exactly why die does.

Also, nice Bazinga, I just wanted to give a basic outline of how it works rather than complicating it with the HTML/BB/Direct links, but nice.

And Jo$h, exactly.
 

extacy

Member
Jan 6, 2011
106
2
What would be cool is as if you could make a page that is all designed and stuff that shows all the pictures and stuff, like were we can see the pictures on your site now but all nice and put together you know?



Edit: what do i do when this happens?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
What would be cool is as if you could make a page that is all designed and stuff that shows all the pictures and stuff, like were we can see the pictures on your site now but all nice and put together you know?

Edit: what do i do when this happens?

You must have changed something in the code because it works for everyone else.
 

Mastah

the funny thing is \r\n i did
Oct 25, 2010
739
41
Thats some cool stuff there il use it for my website thanks.
 

Mac

New Member
Feb 9, 2011
111
2
Jo$h and m0nsta. {
"die()" is more used at "or" and throw new exception() is used more at classes . i didn't say don't use it , i said it's better if you..
You can do it both ways , you also can do echo() but i like doing throw new exception , i just wanted to give him my opinion so if he wants he can use it! Thanks
}
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Jo$h and m0nsta. {
"die()" is more used at "or" and throw new exception() is used more at classes . i didn't say don't use it , i said it's better if you..
You can do it both ways , you also can do echo() but i like doing throw new exception , i just wanted to give him my opinion so if he wants he can use it! Thanks
}

I suppose it looks smart, but I prefer to just use die :p
 

Mac

New Member
Feb 9, 2011
111
2
Okay , its your choice ,
again : the script is very good and usable!!
 

extacy

Member
Jan 6, 2011
106
2
Oh thanks, but how do i fix it? i made a file called uploads and i just copy and pasted that code into a file named index.php
 
Status
Not open for further replies.

Users who are viewing this thread

Top