Renaming uploaded photo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanspeare
    New Member
    • May 2014
    • 20

    #1

    Renaming uploaded photo

    Hello bytes,

    I have manage to create codes that could upload a image that is purposely to serve as profile picture of a certain student. My problem is how to rename it to aN AUTO COLLATE UNIQUE ID NUMBER so that i can display it specifically to a certain student account which bears the unique id number. The variable is $student_id.

    Code:
    <?php
        session_start();
        $session_id = $_SESSION['user_id'];
        if($session_id == null){
           header("location:Student_Edit.php");
           die();
        }
        include 'Connect.php';
        $flag = "";
        $result = mysql_query("SELECT * FROM student_information where student_id='{$_SESSION['user_id']}'",$link_id);;
        $data = mysql_fetch_array($result);
    ?>     
    <div class="style3" >
        <?php
        $uploadpath = 'upload/';     
        $max_size = 2000;         
        $alwidth = 900;           
        $alheight = 800;          
        $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png','psd');       
        if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
        $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);      
        $sepext = explode('.', strtolower($_FILES['fileup']['name']));
        $type = end($sepext);      
        list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     
        $err = '';         
        if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
        if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
        if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;
        if($err == '') {
        if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
          echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
          echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
          echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
          if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
          echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';
        }
        else echo '<b>Unable to upload the file.</b>';
      }
      else echo $err;
    }
    ?>
    Please modify my codes to rename the uploaded file to the student id of the student who is uploading it.
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    maybe you can try this one.
    Code:
    $FILENAME 		= 	$FILENAME;//file name of your uploaded file.
    	$newName		=	'image_'.date("Ymdg:i a").'_'.$FILENAME;//get the name of the image uploaded and add some dates etc. to rename your uploaded image.
    	
    	$temp 			= 	"uploads/temp_img/".$FILENAME;//temporary folder for uploaded file,
    	$profile 		= 	"uploads/profile/".$newName;//the folder where to move the image
    
    	$move_upload 	= 	rename($temp,$profile);
    	
    	if($move_upload)
    	{
    		echo 'Success';
    	}
    	else
    	{
    		echo 'Mali.';
    	}

    Comment

    • hanspeare
      New Member
      • May 2014
      • 20

      #3
      thank you exequiel..

      Comment

      Working...