www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to rename Image's name

  1. #1
    Join Date
    Sep 2011
    Posts
    15

    How to rename Image's name

    I would like to know that how to rename the uploaded image's name which need to be unique in the file. The follow is my PHP code

    PHP Code:
    <?php session_start();
    include (
    "check_session_user.php");
    //connect to database server
    $link mysql_connect("localhost","root","");
    //connect to database
    $dbconnect mysql_select_db("supportdb",$link);

    //send user input data to database        
    $result $_POST['TicketNumber'];
    $NameOfRequestor $_POST['NameOfRequestor'];
    $NameOfRequestor str_replace("'","''",$NameOfRequestor);
    $Department $_POST['Department'];
    $Department str_replace("'","''",$Department);
    $ProblemDescription $_POST['ProblemDescription'];
    $ProblemDescription str_replace("'","''",$ProblemDescription);
    $Email $_POST['Email'];
    $Email str_replace("'","''",$Email);
    $Photo $_FILES['Photo'];
    $file_size $_FILES['Photo']['size'];
     
     if (
    $file_size 102400) {
                echo 
    "<script>alert('Image size exceeds maximum file size allowed'); history.go(-1);</script>";
                exit;
      }
     if(
    $file_size == "") {
                echo 
    "<script>alert('No image found.'); history.go(-1);</script>";
                exit;
      }
     
     
    //uploading and resizing image width 300 max
     
    if (isset ($_FILES['Photo'])){
                  
    $imagename $_FILES['Photo']['name'];
                  
    $source $_FILES['Photo']['tmp_name'];
                  
    $target "C:\\wamp\\www\\ITSupportApp\\error_images\\".$imagename;
                  
    move_uploaded_file($source$target);
     
                  
    $imagepath $imagename;
                  
    $save "C:\\wamp\\www\\ITSupportApp\\error_images\\" $imagepath//This is the new file you saving
                  
    $file "C:\\wamp\\www\\ITSupportApp\\error_images\\" $imagepath//This is the original file
     
                  
    list($width$height) = getimagesize($file) ; 
                 
                  
    //resize if width is more than 300
                  
    if($width 140)
                  {
                  
    $modwidth 140
     
                  
    $diff $width $modwidth;
     
                  
    $modheight $height $diff;
                
                  }
                  else
                  {
                   
    $modwidth $width;
                   
    $modheight $height;
                  }
                 
                  
    $tn imagecreatetruecolor($modwidth$modheight) ;
                  
    $image_info getimagesize($file);
                  
    $image_type $image_info[2];
                  if( 
    $image_type == IMAGETYPE_JPEG ) {
                     
    $image imagecreatefromjpeg($file) ; 
                  } elseif( 
    $image_type == IMAGETYPE_GIF ) {
                     
    $image imagecreatefromgif($file);
                  } elseif( 
    $image_type == IMAGETYPE_PNG ) {
                     
    $image imagecreatefrompng($file);
                  }

                  
                  
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height) ; 
                  
    $save "C:\\wamp\\www\\ITSupportApp\\error_images\\" $imagepath//This is the new file you saving
                  
                  
    if( $image_type == IMAGETYPE_JPEG ) {
                     
    imagejpeg($tn,$save,100);
                  } elseif( 
    $image_type == IMAGETYPE_GIF ) {
                     
    imagegif($tn,$save);         
                  } elseif( 
    $image_type == IMAGETYPE_PNG ) {
                     
    imagepng($tn,$save);
                  } 
                 
                  
                 
    chmod($save,0777);
                 
                 
    $save "image/" $imagepath;
                 
     }
     else
     {    echo(
    'here3');
         echo 
    "<script>alert('No image found'); history.go(-1);</script>";
         exit;
     }

    $Status "Open"//set 'open' value for Status column
    //inserting user's input to the database
    $sql "INSERT INTO reported_problem (TicketNumber, NameOfRequestor, UserID, Department, ProblemDescription, Date, Status, EscalatedTo, Email, Resolution, UpdatedOn, UpdatedBy, ImagePath) VALUES (".$result.",'".$NameOfRequestor."', '".$_SESSION['userID']."', '".$Department."','".$ProblemDescription."',".date('ymd').",'".$Status."','','".$Email."','',000000,'', '".$save."');";
    $count mysql_query($sql,$link) or die(mysql_error()); // excuted sql

    if ($count == 1//check whether the data is inserted to the database or not
    {
    require_once(
    'C:\php\PHPMailer_v5.1\class.phpmailer.php');
                
    $mail = new PHPMailer();
                
    $mail->IsSMTP();
                
    $mail->Host 'mail.bsgroup.com.sg';
                
    $mail->Port 25;

                
    $mail->SMTPAuth true;
                
    $mail->Username '***';
                
    $mail->Password '****';
                
    $mail->From '*****';
                
    $mail->FromName $NameOfRequestor;
                
    $mail->Subject 'Support Request';
                
    $body "The user has logged a IT support request.<br><br><br> Please click <a href=http://ibmx305/ITsupport/stafflogin.php>here</a> to login and check the request. <br><br><br> ";
                
    $mail->MsgHTML($body);
                
    $mail->AddAddress('potepote20@gmail.com''IT Department'); 

                if (
    $mail->Send()) {
                    echo 
    "<script>alert('Your request has been sent out successfully. Someone from the IT Department will be contacting you shortly to follow up your request. Thank you.'); history.go(-1); </script>";
                } 
    }
    ?>
    Thanks in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    I am not sure what you are asking but if you want to change the name you want to change this line:
    PHP Code:
    $imagepath $imagename
    There are a couple other concerns. The path you are saving to the database is not the same as where you are saving the file. I don't know if you have a symbolic link to that directory or what is going on so I can't really comment on that.

    Another is you will overwrite images with the same name. Perhaps that is what your question is. If you are trying to get unique names to prevent overwriting images then you could append a timestamp or use a hash.

  3. #3
    Join Date
    Aug 2011
    Posts
    60
    PHP Code:
     $target "C:\\wamp\\www\\ITSupportApp\\error_images\\".$your_new_name_var_goes_here
    why are you double slashing the path?

  4. #4
    Join Date
    Sep 2011
    Posts
    15
    If no double slashing, it shows syntax error .... That's why. I am still new to PHP. Thanks to both .... I got it.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    It's often convenient to take the date, for example $tdy=date("ymd_His"), to build a new name for the image file. You can too compare the suffix (function strichr) with the type and take care with jpg and jpeg files...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles