File Rename in Image Upload Script help!

Discussion in 'PHP' started by MediaHustler, Feb 28, 2006.

  1. #1
    <?
    $num_of_uploads=1;
    $file_types_array=array("JPG","jpeg","jpg","gif","bmp");
    $max_file_size=1048576;
    $upload_dir="/home/content/d/u/m/dumbnerd/html/is/img/";
    
    function uploaderFILES($num_of_uploads=1, $file_types_array=array("JPG","jpeg","jpg","gif","bmp"), $max_file_size=1048576, $upload_dir=""){
      if(!is_numeric($max_file_size)){
       $max_file_size = 1048576;
      }
      foreach($_FILES["file"]["error"] as $key => $value)
      {
         if($_FILES["file"]["name"][$key]!="")
         {
           if($value==UPLOAD_ERR_OK)
           {
             $origfilename = $_FILES["file"]["name"][$key];
             $filename = explode(".", $_FILES["file"]["name"][$key]);
             $filenameext = $filename[count($filename)-1];
             unset($filename[count($filename)-1]);
             $filename = implode(".", $filename);
             $filename = substr($filename, 0, 15).".".$filenameext;
             $file_ext_allow = FALSE;
             for($x=0;$x<count($file_types_array);$x++){
               if($filenameext==$file_types_array[$x])
               {
                 $file_ext_allow = TRUE;
               }
             } // for
             if($file_ext_allow){
               if($_FILES["file"]["size"][$key]<$max_file_size){
                 if(move_uploaded_file($_FILES["file"]["tmp_name"][$key], $upload_dir.$filename)){
                   echo("File uploaded successfully. - <a href='/is/img/".$filename."' target='_blank'>".$filename."</a><br />");
                 } 
                 else { echo('<font color="#FF0000">'.$origfilename."</font> was not successfully uploaded - khong the upload duoc <br />");}
               }
               else  { echo('<font color="#FF0000">'.$origfilename."</font> was too big, not uploaded - Kich thuoc file qua' lon <br />"); }
             } // if
             else{ echo('<font color="#FF0000">'.$origfilename." </font>had an invalid file extension, not uploaded - File nay khong ton tai <br />");  }
           }
           else{ echo('<font color="#FF0000">'.$origfilename." </font>was not successfully uploaded - khong the upload duoc <br />");  } // else
         }
      }
    } // funtion
    
    /////////////////////////////////////////
    ?>
      
      
    
    //////////////////////////////////////
    <?
    if(isset($_POST["submitted"])){
       uploaderFILES($num_of_uploads, $file_types_array, $max_file_size, $upload_dir);
    }
    ?> 
    PHP:
    I want it to rename the image even if one doesn't exsist to like say if I upload a file named cophit I want it to be like
    cophit242482725295.jpg or something along them lines.

    Help please!
     
    MediaHustler, Feb 28, 2006 IP
  2. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    oh boy here we go

    ur trying to reinvent the wheel

    heres a fragment from my php5 code i use based on PEAR::HTTP_Upload library (this library works fine in php4 as well)

    its neat and ellegant (dont we all love OO)

    
    
    if( ($_GET['do'] === 'upload') && $SecureForm->unlock( $_POST['hash'], $_GET['id']) ){
    
    	
    	require_once "HTTP/Upload.php";
    
    	$upload = new HTTP_Upload("en");
    	$file = $upload->getFiles('f');
    	
    	
    	if ($file->isValid()) {
    
    		$file->setName ('uniq', '', '');
    
    
    	   	$size = $file->getProp('size');
    	   	
    	   	if ($size > 60000) {
    	   		$Warning->setWarning('Image has to be lees than 60KB in lenght', '?module=upload', 3);
    	   		$Warning->display();
    	   	}
    	   	
    	   	
    	   	
    	   	$extension = $file->getProp('ext');
    	   	
    	   	if ($extension == 'jpg' || $extension == 'gif' || $extension == 'png') {
    	   		
    	   	}
    	   	else {
    	   		$Warning->setWarning('Only <strong>.jpg, .png, .gif</strong> filetypes are allowed', '?module=upload', 3);
    	   		$Warning->display();
    	   	}
    	   	
    	   	
    	   	$image = getImageSize($file->getProp('tmp_name')); 
    	   	
    	   	$imgX = $image[0];
    		$imgY = $image[1];
    		
    		if ($imgX > 130 || $imgY > 170) {
    	   		$Warning->setWarning('Image has to up to a maximum of 130x170 pixels', '?module=upload', 3);
    	   		$Warning->display();
    	   	}
    	   	
    
    	   	$file_name = $file->moveTo('../images/');
    	   	
    	   	
    	   	$name = $file->getProp('name');
    	}
    
    
    PHP:
    look into the PEAR libraries they will save you alot of work (and time and money)

    pear.php.net
     
    wwm, Mar 1, 2006 IP
  3. cjohnweb

    cjohnweb Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you know anything about PHP, then this may help you. PHP has a pretty simple random number generator. This line assigns a random number to a variable. By default I think it goes from 1 to max_range or something like that.

    $random = rand () ;

    If you wanted it to generate a random number between 5 and 1000, it would go:
    $random = rand (5,1000) ;

    Its pretty simple. You can add this in conjunction to nearly anything, for instance, if you put your picture filename into a variable $filename, and split it into 2 parts (filename and extension), then you can randomly generate a number to add to that file name, anywhere in the file, as follows:

    $new_filename = $original_filename.$random_number.$extension;

    or

    $new_filename = $random_number.$original_filename.$extension;


    Hope that helps!!




    New RP Site at http://www.chibiusa.net
     
    cjohnweb, Nov 27, 2007 IP