Hello Daniweb,

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

<?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;
}
?>

Dani AI

Generated

A simple, reliable solution is to build the destination filename from the student ID and the uploaded file’s extension, then pass that full filename to move_uploaded_file. That writes the file with the desired name in one operation (no post-rename needed). Use the session value you already have (e.g. $_SESSION['user_id']) as the canonical $studentId, validate the extension/MIME, ensure the upload directory exists and is writable, and record the resulting filename in the student record in the database.

Example (conceptual PHP snippet — adapt to your project):

$studentId = intval($_SESSION['user_id']);
$uploadDir = __DIR__ . '/upload';
if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);

$ext = strtolower(pathinfo($_FILES['fileup']['name'], PATHINFO_EXTENSION));
$allowed = ['jpg','jpeg','png','gif','bmp','psd'];
if (!in_array($ext, $allowed)) { /* reject */ }

$target = $uploadDir . '/' . $studentId . '.' . $ext;
if (move_uploaded_file($_FILES['fileup']['tmp_name'], $target)) {
    // save basename($target) into student_information (use mysqli/PDO + prepared statements)
}

Practical notes and troubleshooting:

  • Check $_FILES['fileup']['error'], is_writable($uploadDir), and PHP limits (upload_max_filesize, post_max_size) if uploads fail.
  • Validate file content with finfo_file() or getimagesize() to avoid trusting extensions alone.
  • Decide overwrite behavior: use plain {id}.{ext} to replace old photos, or append a timestamp/random suffix to avoid collisions (and store the filename in DB).
  • Replace deprecated mysql_* calls with mysqli or PDO and use prepared statements when updating the student row with the stored filename.
  • On shared hosting or for privacy, consider either randomizing filenames or storing images outside webroot and serving them through a script that checks permissions.

Tie-ins to thread: was right to suggest changing the destination passed to move_uploaded_file; ’s point about using the session user id is correct — prefer writing directly to the final filename rather than doing a separate rename.

Recommended Answers

All 3 Replies

move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath change $uploadpath

This upload path?

$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']); or this?

$uploadpath = 'upload/';

I doubt how i could change it to rename the file to the current student who is uploading the photo using their student id.

Please advise

1.) Are you on a shared server or your own server? This is issues for both basename and path directory.

2.) In your header redirect use header as an absolute path in your redirect, not relative.

3.)In your $result = mysql_query("SELECT * FROM student_information where student_id='{$_SESSION['user_id']}'",$link_id); go ahead since you already have the user id assigned as session then create a variable for that session name instead, then you can simply use that variable name when you rename the file, if that's all you want, like; 44.jpg or 27.png. What is this $link_id?

4.) I looked around and see you posted this elsewhere, which is smart, though I also am unsure if your wrote this (understand it) because there is a lot of set variables and other things that don't make sense, why its' there or used.

5.) In addition to #4 above are you wanting to set the picture as the same variable that was auto created per the user or are you trying to auto create something else. Not really clear here and thus also the logic would not make to much sense unless you create function or rand when user registered and set to your db and then concate that with user id in this case your student id or $link_id.

6.) You need php rename function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.