Okay I have a html upload form here, What is does is displays a browse button so users can upload files to my site, what I want them to be able to do is completly rename the file that they are uploading once they upload it to my site, but always add the extensions .mp4, so say I upload a video named sample123.mp4 and I rename the video to funny. I want the file to be saved in my directory as funny.mp4. But be saved into my database table as funny.

<?PHP
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
<title>Untitled Document</title>
<meta charset="utf-8"/>
</head>
<body>
<p></p>
<form action="upload_file.php" method="post" enctype="multipart/form-data"><label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br />Name the Video: <input type="text" name="videoname" /> <br /> <input type="submit" name="submit" value="Submit" />


</form>
</body>
</body>
</html>

This is my php form which uploads the videos,

<?php


session_start();
require_once("./include/fg_membersite.php");
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin()){
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
$allowedExts = array("mp4", "WebM", "ogg");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < 90000000000000000000000000000000000) && in_array($extension, $allowedExts)){
    if ($_FILES["file"]["error"] > 0){
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }else{
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
        if (file_exists("upload/" . $_FILES["file"]["name"])){
            echo $_FILES["file"]["name"] . " already exists. ";
        }else{
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "uploads/upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "uploads/upload/" . $_FILES["file"]["name"];
        }
    }
    $con = mysql_connect("host.com","username","password");
    if (!$con){
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("dbname", $con);
    $name = mysql_real_escape_string($_FILES["file"]["name"]);
    $user = $_SESSION['email_of_user'];
    $videoname = mysql_real_escape_string($_POST['videoname']);
    $r = mysql_query("INSERT INTO `videos` SET `videoname` = '$videoname', `email` = '$user', `name`= '$name'");
    if(mysql_affected_rows()){
        echo "1 record added";
    }else{
        echo "problem";
    }
}else{
    echo "Invalid file";
}


?>

Okay, well it looks like you're most of the way there already.

At the moment, you're uploading the file and using the original name of the uploaded file - that's the $_FILES['file']['name']; part.

Instead, what you need to do is replace that with the new file name. E.g.

$file_name = 'funny'; // New unique file name
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/upload/{$file_name}.mp4");

Or if you move line 36 to just after line 12, you could do something like:

$video_name = mysql_real_escape_string($_POST['videoname']);
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/upload/{$video_name}.{$extension}");

Thanks this is great!

Hello, I'll like to use the uploads & rename scripts on a wordpress site, can u help with that. Has used in this website. www.Gidi360.com/vc

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.