How do I edit this so it renames the file?

Status
Not open for further replies.

Unidentified

Living the Developer Life...
Jun 19, 2012
144
20
I would like to know how to change this so on upload it changes the files name please


PHP:
<?php
include 'core/init.php';
protect_page ();
$id = $_SESSION['id'];
?>
<?php
if (logged_in() === true) {
    $session_user_id = $_SESSION['id'];
    $user_data = user_data($session_user_id, 'profile_picture', 'first_name', 'last_name', 'status', 'location', 'education', 'home_town'); 
}
$data = array();
?>
<?php
define ('MAX_FILE_SIZE', 1024 * 50);
if (array_key_exists('upload', $_POST)) {
  define('UPLOAD_DIR', "avatars/".$id."/");
  $file = str_replace(' ', '_', $_FILES['image']['name']);
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  if (in_array($_FILES['image']['type'], $permitted)
      && $_FILES['image']['size'] > 0
      && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    switch($_FILES['image']['error']) {
      case 0:
        if (!file_exists(UPLOAD_DIR . $file)) {
          $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR . $file);
        } else {
          $result = 'A file of the same name already exists.';
        }
        if ($success) {
          $result = "$file uploaded successfully.";
        } else {
          $result = "Error uploading $file. Please try again.";
        }
        break;
      case 3:
      case 6:
      case 7:
      case 8:
        $result = "Error uploading $file. Please try again.";
        break;
      case 4:
        $result = "You didn't select a file to be uploaded.";
    }
  } else {
    $result = "$file is either too big or not an image.";
  }
}
?>
 

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
You could try this :L

PHP:
<?php
include 'core/init.php';
protect_page ();
$id = $_SESSION['id'];
?>
<?php
if (logged_in() === true) {
    $session_user_id = $_SESSION['id'];
    $user_data = user_data($session_user_id, 'profile_picture', 'first_name', 'last_name', 'status', 'location', 'education', 'home_town');
}
$data = array();
?>
<?php
define ('MAX_FILE_SIZE', 1024 * 50);
if (array_key_exists('upload', $_POST)) {
  define('UPLOAD_DIR', "avatars/".$id."/");
  $file = str_replace(' ', '_', $_FILES['image']['name']);
  $randFilename = uniqid("img_");
$newFilename = $randFilename.".".$oldFileExt;
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  if (in_array($_FILES['image']['type'], $permitted)
      && $_FILES['image']['size'] > 0
      && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    switch($_FILES['image']['error']) {
      case 0:
        if (!file_exists(UPLOAD_DIR . $file)) {
       
          $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR . $newFilename);
        } else {
          $result = 'A file of the same name already exists.';
        }
        if ($success) {
          $result = "$file uploaded successfully.";
        } else {
          $result = "Error uploading $file. Please try again.";
        }
        break;
      case 3:
      case 6:
      case 7:
      case 8:
        $result = "Error uploading $file. Please try again.";
        break;
      case 4:
        $result = "You didn't select a file to be uploaded.";
    }
  } else {
    $result = "$file is either too big or not an image.";
  }
}
?>
 
Status
Not open for further replies.

Users who are viewing this thread

Top