-1

This code works in a good way, but I want to do a rename of the video before uploading Can you modify it for me?**

if (isset($_POST['but_upload'])) {
    $name = $_FILES['file']['name'];
    $namefilm = $_POST['namefilm'];
    $year = $_POST['year'];
    $wirte = $_POST['wirte'];
    $dep = $_POST['dep'];
    $about = $_POST['about'];
    $time = time();
    $target_dir = "../videos/";
    $target_file = $target_dir.$_FILES["file"]["name"];
    $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
    if ($name == ""
        or $namefilm == ""
        or $year == ""
        or $wirte == ""
        or $dep == ""
        or $about == ""
        or $target_file == ""
        or $file == "") {
        echo 'erorr';
    } else {
        $videoFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        $extensions_arr = array("mp4", "avi", "3gp", "mov", "mpeg");
        if (in_array($videoFileType, $extensions_arr)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
                $query = "INSERT INTO videos (name, location, namefilm, image, dep, wirte, yaer, time, about) VALUES('" . $name . "', '" . $target_file . "', '" . $namefilm . "', '" . $file . "', '" . $dep . "', '" . $wirte . "', '" . $year . "', '" . $time. "', '" . $about . "')";
                mysqli_query($con, $query);
                echo 'done';
            }
        } else {
            echo 'erorr';
        }
    }
}

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
  • 3
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add any data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or data *of any kind* directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Oct 29 '19 at 22:20
  • 1
    Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is largely an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Oct 29 '19 at 22:20
  • ⟼This code could benefit greatly if you adopted an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and apply it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it. – tadman Oct 29 '19 at 22:20
  • 1
    What rule do you want to use to rename the file? You get the file name from `$_FILES["file"]["name"]`...that value could be anything, so then what do you want to change it to? You didn't explain how you want the computer to choose the new name, so we can't help you really. But basically you can do the renaming just before you create the finished `$target_file` variable. – ADyson Oct 29 '19 at 22:28
  • P.S. if you really mean you want to rename it before the file is sent to PHP at all, then that has nothing to do with the code you've posted. The user uploading the file would have to rename it before selecting it in the form. – ADyson Oct 29 '19 at 22:30
  • This file is not a public upload it is special and there are specific people to upload only I want to change the name no more – Mustafa Al-Tai Oct 29 '19 at 22:32
  • 1
    Possible duplicate of [How to rename uploaded file before saving it into a directory?](https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory). – N'Bayramberdiyev Oct 29 '19 at 22:45
  • "This file is not a public upload" ...how is that relevant? The process is the same in any case. And... "I want to change the name" ...yes, but **what do you want to change the file name to?**. You're saying there should be some code to change the name...so the computer must change the name. Therefore, how should the computer decide what the new name must be? If you don't know that, then you won't be able to write the code to achieve it. – ADyson Oct 29 '19 at 22:54

1 Answers1

1

u have to change ur $target_file = $target_dir.$_FILES["file"]["name"];

to

$vidName = "newname.mp4";
$target_file = $target_dir.$vidName;

in case u have multiple videos to upload, they can't have the same name so u can change the filename to a unique id that is randomly generated by php, here:

$vidName = uniqid() . ".mp4";
$target_file = $target_dir.$vidName;
  • Hard-coding the filename is fine for uploading one file... but when someone tries to upload another file later, there's an obvious problem because you can't have two files with the same name in the same folder. – ADyson Oct 30 '19 at 00:12
  • Then why not show an example of something like that which actually works rather than a simplistic example which will cause problems? – ADyson Oct 30 '19 at 00:16