Image Hosting Script [HELP] [PHP]

Status
Not open for further replies.

NaziPhreak

Member
Mar 24, 2011
33
0
When a user uploads a image for example called "me.gif" , and when a different person uploads the image with
with the same name and extention that user over-writes the previous image and replaces it.

How do I make it so it will create a random upload name like from "example.png" to "18j2nx01.png" and it will continue
making random upload names so it will not over-write other images or how do I make it so it will inform a different user already has uploaded the same name and extention so you will need to change the name of the image file.I perfer the first one.

This is a image hostings script by M0nsta.
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 = "imageuploads/"; //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>
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=&quot;http://". $_SERVER['HTTP_HOST'] ."/" . $this->uploadTarget . time() . $this->fileName . "&quot;>\" /><br><br>
View image <a href=\"" . $this->uploadTarget . time() . $this->fileName . "\"><b>here</b></a>";  
        }
        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>";
}
?>

Image Hosting Script Release:
.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Change
PHP:
$this->fileName = $_FILES['uploaded']['name'];
to:
PHP:
$this->fileName = rand(9, 99999) . '-file-' . $_FILES['uploaded']['name'];

That would stop it! Unless you expect to host more than 50,000 files!
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
You could also do something as simple as:

Changing:
PHP:
$this->fileName = $_FILES['uploaded']['name'];

To:
PHP:
$this->fileName = mt_rand( 1, 9999 ) . substr( md5( $_FILES['uploaded']['name'] ), ( mt_rand( 1, 10 ), 8 ) ) . $this->getExt();

That should encrypt the file name in MD5 and select a random chunk of the MD5 string name. I've not tested it but it looks like it should work.
 
Status
Not open for further replies.

Users who are viewing this thread

Top