www.webdeveloper.com
Results 1 to 4 of 4

Thread: Renaming uploaded image

  1. #1
    Join Date
    May 2014
    Posts
    26

    Cool Renaming uploaded image

    Hello webdevelopers,

    I have a code in here which upload a photo to serve as the profile pic of a certain student. My problem now is how to rename the uploaded image to the AUTO COLLATE UNIQUE STUDENT NUMBER so that i can call it specifically as his/her profile picture with the variable $student_id.

    PHP Code:
    </div>
        <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']/10243'.''') .'</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;
    }
    ?>                       
    </body>
    </html>
    Any modification will be highly appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    Just change this line so that you assign the desired name:
    PHP Code:
    $uploadpath $uploadpath basename$_FILES['fileup']['name']); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2014
    Posts
    26
    My desired image name is the student_id of the one who is uploading the photo.
    i have tried replacing it with $student_id but it says the variable is not defined.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,501
    Well if it is telling you that it is not defined, then that means that the variable has not been declared or a value been set, therefore you need to put code in to set that variable.

    I also suggest that you study the file upload threads at the head of the forum, you will find a couple of sticky subjects to do with image uploading, you will find them very useful and informative.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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