1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Rename file name during uploading ! Script Help

Discussion in 'PHP' started by maihannijat, Jun 12, 2010.

  1. #1
    Hi,

    This script upload images, but it replace existing ones.

    I want to rename files during uploading in add with file name something like random numbers or data&time.

    Thanks in Advance


    My Script Code:

    <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
    <?php
    // upload the file
    if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
    	
    	// file needs to be jpg,gif,bmp,x-png and 4 MB max
    	if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
    	{
    		
      
    		// some settings
    		$max_upload_width = 2592;
    		$max_upload_height = 1944;
    		  
    		// if user chosed properly then scale down the image according to user preferances
    		if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
    			$max_upload_width = $_REQUEST['max_width_box'];
    		}    
    		if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
    			$max_upload_height = $_REQUEST['max_height_box'];
    		}	
    
    		
    		// if uploaded image was JPG/JPEG
    		if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){	
    			$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
    		}		
    		// if uploaded image was GIF
    		if($_FILES["image_upload_box"]["type"] == "image/gif"){	
    			$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
    		}	
    		// BMP doesn't seem to be supported so remove it form above image type test (reject bmps)	
    		// if uploaded image was BMP
    		if($_FILES["image_upload_box"]["type"] == "image/bmp"){	
    			$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
    		}			
    		// if uploaded image was PNG
    		if($_FILES["image_upload_box"]["type"] == "image/x-png"){
    			$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
    		}
    		
    
    
    		$remote_file = "image_files/".$_FILES["image_upload_box"]["name"];
    		imagejpeg($image_source,$remote_file,100);
    		chmod($remote_file,0644);
    	
    	
    
    		// get width and height of original image
    		list($image_width, $image_height) = getimagesize($remote_file);
    	
    		if($image_width>$max_upload_width || $image_height >$max_upload_height){
    			$proportions = $image_width/$image_height;
    			
    			if($image_width>$image_height){
    				$new_width = $max_upload_width;
    				$new_height = round($max_upload_width/$proportions);
    			}		
    			else{
    				$new_height = $max_upload_height;
    				$new_width = round($max_upload_height*$proportions);
    			}		
    			
    			
    			$new_image = imagecreatetruecolor($new_width , $new_height);
    			$image_source = imagecreatefromjpeg($remote_file);
    			
    			imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
    			imagejpeg($new_image,$remote_file,100);
    			
    			imagedestroy($new_image);
    		}
    		
    		imagedestroy($image_source);
    		
    		
    		header("Location: submitimage.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
    		exit;
    	}
    	else{
    		header("Location: submitimage.php?upload_message=make sure the file is jpg or gif and that is smaller than 4MB&upload_message_type=error");
    		exit;
    	}
    }
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Image Upload with resize</title>
    <style type="text/css">
    <!--
    body,td,th {
    	font-family: Arial, Helvetica, sans-serif;
    	color: #333333;
    	font-size: 12px;
    }
    
    .upload_message_success {
    	padding:4px;
    	background-color:#009900;
    	border:1px solid #006600;
    	color:#FFFFFF;
    	margin-top:10px;
    	margin-bottom:10px;
    }
    
    .upload_message_error {
    	padding:4px;
    	background-color:#CE0000;
    	border:1px solid #990000;
    	color:#FFFFFF;
    	margin-top:10px;
    	margin-bottom:10px;
    }
    
    -->
    </style></head>
    
    <body>
    
    <h1 style="margin-bottom: 0px">Submit an image</h1>
    
    
            <?php if(isset($_REQUEST['upload_message'])){?>
                <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
                <?php echo htmlentities($_REQUEST['upload_message']);?>
                </div>
    		<?php }?>
            
            
            
            
            
            
            
    
    
    <form action="submitimage.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
    <label>Image file, maximum 4MB. it can be jpg, gif:</label><br />
              <input name="image_upload_box" type="file" id="image_upload_box" size="40" />
              <input type="submit" name="submit" value="Upload image" />     
         
         <br />
    	<br />
    
         
          <label>Scale down image? (2592 x 1944 px max):</label>
          <br />
          <input name="max_width_box" type="text" id="max_width_box" value="1024" size="4">
          x      
          
          <input name="max_height_box" type="text" id="max_height_box" value="768" size="4">
          px.
          <br />
          <br />
          
          <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;"> <b>Note:</b> Don't change above scale values for uploading your image in default scale.<br />
          
          Script Integrated by Maihan Nijat | <a href="http://www.afghanistanforums.com"> <b>Afghanistan Forum</b></a>
          
         | For <b>Donation</b> visit my Forum and help me to promote it. <br /><br />
        <a href="http://www.afghanistanforums.com"> <img hieght="15" width="300" src="./image_files/afglogo.png" /></a> 
           </p>
          
    <br />
    <br />
          
    
    <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
              </form>
    
    
    <!-- Code for Image Path to copy -->
    
    
    <?php 
    if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
    <p>
    <input name="url" type="text" value="<?php echo $_REQUEST ['show_image'] ; ?>"  readonly="true" />
    </p>
    <?php }?>
    
    
    <!-- Code for displaying uploaded image in page -->
    
    
    <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
    <p>
    	<img src="image_files/<?php echo $_REQUEST['show_image'];?>" />
    </p>
    <?php }?>
    
    
    </body>
    </html>
    PHP:
     
    maihannijat, Jun 12, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ever consider just naming it the md5sum of the file?
     
    krsix, Jun 12, 2010 IP
  3. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    I didn't get you.

    I want file automatically take randomly name during uploading.

    for example the file name is Afghan.jpg and after uploading to my website the name changed to something like Afghan1234.jpg or Current date&time+afghan.jpg.


    Thanks
     
    maihannijat, Jun 12, 2010 IP
  4. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $filename = time().'-'.$variableoffilename; or something similar then
     
    krsix, Jun 12, 2010 IP
  5. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I hope it stores images in images_files

     $remote_file = "image_files/".$_FILES["image_upload_box"]["name"];
    PHP:
    There for to use Date Declare a variable and go with it
    $date = date("m-d-y");


     $remote_file = "image_files/".$date.$_FILES["image_upload_box"]["name"];
    PHP:
     
    roopajyothi, Jun 12, 2010 IP
  6. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    Still not working
     
    maihannijat, Jun 13, 2010 IP
  7. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    Sorry It is working perfect now.

    Thanks for your help
    which code I need to show me the uploaded image path.

    Example: I uploaded image. cuteboy.jpg and in the same page show me the url like :

    http://www.mydomain.com/image_files/cuteboy.jpg


    I know I am asking too much but since days I am trying to fix this but no positive result.

    thanks again in advance.
     
    maihannijat, Jun 13, 2010 IP
  8. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    looking at
            header("Location: submitimage.php?upload_message=make sure the file is jpg or gif and that is smaller than 4MB&upload_message_type=error");
    PHP:
    is kind of creepy - are you sanitizing $_GET['upload_message']?
     
    krsix, Jun 13, 2010 IP
  9. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9

    Hi, I am not professional

    Could you explain it more.

    Thanks
     
    maihannijat, Jun 13, 2010 IP
  10. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    I have FORUM and for my users I want to add this script to upload image and copy the url and paste it into post.


    so how can I get url of image after uploading file.

    is there any code to show me uploaded file url?
     
    maihannijat, Jun 13, 2010 IP
  11. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #11
    Is there any solution for this or no?
     
    maihannijat, Jun 14, 2010 IP
  12. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #12
    lukeg32, Jun 14, 2010 IP
  13. maihannijat

    maihannijat Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #13
    I searched for other script but didn't find good one.

    regarding this script.

    see the request is going to PHP server for generating name, so how can I echo which name is generated for uploaded file???
     
    maihannijat, Jun 17, 2010 IP