areen shah
areen shah

Reputation: 1

how to rename file name while uploading

I have a file with 2 input fields one for file name (user will type) and the second one for choosing file what I want to upload the file to a directory with the name user typed. down is the code I'm using guys please help me how to change the file name into what user typed.

<?php

$filename = $_POST["file"]
$upload   = $_FILES['userfile'];


$target_path = "upload/";

$target_path .= $upload["name"];

$newname = "anything";

if(move_uploaded_file($upload["tmp_name"], $target_path))
    {
        echo "uploaded successfully";
    }
?>

Upvotes: 0

Views: 2734

Answers (1)

Richard Pianka
Richard Pianka

Reputation: 3356

Change $target_path .= $upload["name"]; to something like $target_path .= $filename;.

edit: For the record, I have to say that letting people upload files (and choose the extension) to your web server raises some serious security concerns. I would suggest at least disabling the ability to execute scripts in your target folder.

Upvotes: 4

Related Questions