Multiple image upload script

Discussion in 'PHP' started by levani, Feb 12, 2010.

  1. #1
    Hello,

    Looking for a simple php multiple upload script for images. Do you happen to have any?

    Found this script via googe:

    <?
    	
    while(list($key,$value) = each($_FILES['images']['name']))
    		{
    			if(!empty($value))
    			{
    				$filename = $value;
    					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
    
    					$add = "upimg/$filename";
                           //echo $_FILES['images']['type'][$key];
    			     // echo "<br>";
    					copy($_FILES['images']['tmp_name'][$key], $add);
    					chmod("$add",0777);
    			
    
    			}
    		}
    
    
    ?>
    PHP:
    but it has one serious bug, it overwrites files with the same names... How can I fix that problem? Also I need to send the uploaded file path to the database...

    Can anyone please help?

    Thanks in advance
     
    levani, Feb 12, 2010 IP
  2. thorie

    thorie Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you put this right before the copy(), each copy will have it's own filename:

    $i=1; $tmp = $add; while (file_exists($tmp)) { $tmp = "copy_$i_of_$add"; $i++ } $add = $tmp;

    And to store the full uploaded path to the database, you can probably do:

    $absolute_path = mysql_real_escape_string(getcwd() . $add);
    $query = "insert into my_images(path) values('$absolute_path')";

    All untested, but I think you get the picture.
     
    thorie, Feb 12, 2010 IP