How do I rename and copy a file on the server?

Joined
Aug 22, 2025
Messages
11
Reaction score
0
Hello.

May I enquire how I go about renaming a file which has just been uploaded?

The folder where the uploaded file is located is: /modules/video/upload
And I'd like to rename and move it to /library/files

I'm unable to get rename to work, however it uploads ok:-

Code:
<?php
if(session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
$vid = $_SESSION['ctre_last_id'];
$id = $_SESSION['user_id'];




// appdemoupload.php
// Show all errors in dev
ini_set('display_errors', 1);
error_reporting(E_ALL);


// ---- SETTINGS ----
$inputName = 'photo'; // must match your form field name
$uploadDir = __DIR__ . '/upload'; // change if needed
$maxBytes = 100 * 1024 * 1024; // 100 MB
$extensions = ['mp4']; // allow only mp4
$mimeAllow = ['video/mp4']; // strict MIME


// ---- ENSURE UPLOADS FOLDER ----
if (!is_dir($uploadDir)) {
if (!mkdir($uploadDir, 0755, true)) {
exit('Error: Cannot create uploads directory.');
}
}
if (!is_writable($uploadDir)) {
exit('Error: Uploads directory is not writable.');
}


// ---- CHECK FILE ARRAY ----
if (!isset($_FILES[$inputName])) {
exit('Error: No file field named "'.$inputName.'" found.');
}
$f = $_FILES[$inputName];


// ---- HANDLE PHP UPLOAD ERRORS ----
$phpErrMap = [
UPLOAD_ERR_OK => 'OK',
UPLOAD_ERR_INI_SIZE => 'File exceeds upload_max_filesize (php.ini).',
UPLOAD_ERR_FORM_SIZE => 'File exceeds MAX_FILE_SIZE (HTML form).',
UPLOAD_ERR_PARTIAL => 'File only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder on server.',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk (permissions).',
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the upload.'
];
if ($f['error'] !== UPLOAD_ERR_OK) {
$msg = $phpErrMap[$f['error']] ?? ('Unknown upload error code: '.$f['error']);
exit('Error: '.$msg);
}


// ---- SIZE CHECK ----
if ($f['size'] > $maxBytes) {
exit('Error: File is larger than 10 MB.');
}
if ($f['size'] === 0) {
exit('Error: Empty file.');
}


// ---- EXTENSION CHECK ----
$ext = strtolower(pathinfo($f['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $extensions, true)) {
exit('Error: Only .mp4 files are allowed.');
}


// ---- MIME CHECK (server-side) ----
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($f['tmp_name']);
if ($mime === false) {
exit('Error: Could not determine file MIME type.');
}
if (!in_array($mime, $mimeAllow, true)) {
exit('Error: Invalid MIME type (got '.$mime.'). Only video/mp4 allowed.');
}


// ---- SAFE DEST FILENAME ----
$base = bin2hex(random_bytes(8)); // random basename
$target = $uploadDir . '/' . $base . '.' . $ext;
$filename = $base . '.' . $ext;
// ---- MOVE FILE ----
if (!is_uploaded_file($f['tmp_name'])) {
exit('Error: Security check failed (not an uploaded file).');
}
if (!move_uploaded_file($f['tmp_name'], $target)) {
exit('Error: Failed to move uploaded file (permissions/path).');
}


// (Optional) save $target or just the filename to DB here.


// ---- SUCCESS ----
echo 'Success: File uploaded as ' . htmlspecialchars(basename($target));


$newfly = "vid".$id."_".$vid.$base . '.' . $ext;

rename($target,'/library/files/'.$newfly);
?>

Also, could I be cheeky, :)

I'd like to copy an image (jpg) file from one directory to another, but I can't get copy to work, may I ask if the snippet of code is correct?

Code:
//Create temporary profile image
    $entryfle = $last_id.".jpg";
    // Store the path of source file
    $source = '/graphics/';
    // Store the path of destination file
    $destination = '/profile/image/';


    copy($source."noimage.jpg", $destination."noimage.jpg");
    rename("/profile/image/noimage.jpg", "/profile/image/". $entryfle);
   //last_id is taken from the insert part of the database

Thank You.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,342
Messages
2,571,407
Members
48,797
Latest member
kenne2025

Latest Threads

Top