Moon Moon - 1 year ago 93
PHP Question

How to upload & Save Files with Desired name

i am using this code to upload files(images to a folder)

<form action='' method='POST' enctype='multipart/form-data'>
<input type='file' name='userFile'><br>
<input type='submit' name='upload_btn' value='upload'>

$target_Path = "images/";
$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path );

when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

i have tried replacing THIS

$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );


$target_Path = $target_Path.basename( "myFile.png" );

BUT it's not working

Answer Source

You can try this,

$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext; 

$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download