Php:How to prevent from OverWrite ImageUpload File

Status
Not open for further replies.

HyDra_92

Active Member
46
2012
0
0
Heys, Guys i need Help i want to stop file overwrite when some upload file its check if file already exit same name like sample.jpeg then it will sample1.jpg




PHP:
<?php


require 'db.inc.php';
if(isset($_POST['up']))
{
    
    
function getExtension($name){
    
    $pos = strpos($name,".");
    $len =strlen($name);
    $str =substr($name,$pos+1,$len);
    return $str;
    }
    
    
    
    for($i=1; $i<=2; $i++)
    {
        $ext = getExtension($_FILES['img'.$i]['name']);
        
    $allowed_filetypes = array("jpg","jpeg","png","gif"); 
        //if($ext !="jpg" and $ext !="gif" and $ext !="jpeg" and $ext !="png")
            if(!in_array($ext,$allowed_filetypes))

        //if($ext!= in_array($Allow))
        {
            echo "Invlaid File"."<br />";
        
        }
        else
        {
            
        
            echo "Valid File"."<br />";
            $path = "images/".$_FILES['img'.$i]['name'];
            $name = $_FILES['img'.$i]['name'];
            copy($_FILES['img'.$i]['tmp_name'],$path);
            echo $path."<br />";
            mysql_query("insert into img_data (url)values('$name') ")or die($query_error);
        }
        
        }
                }
?>


---------- Post added 1st Aug 2012 at 09:56 AM ---------- Previous post was 31st Jul 2012 at 06:21 PM ----------

solved
 
7 comments
suffixes are good. But for the name part, you better make it md5 generated. At least it would be homogene and controlled ([a-f0-9]). Then you won't need to worry about special chars or file system issues.
 
Just add the date and time as a prefix or suffix down to the microsecond and you will never have a duplicate and will have the dates to use for sorting.

That would be better than using random and time.
 
Status
Not open for further replies.
Back
Top