PHP code: How do i make the php script rename a file upload based on a last...

michaelnaeseth

New member
...name form input field? I am currently using the code below to allow users to upload files to my website. How can i change the php script so that it renames the files to the uploaders' last name??

Here is the script i am using now:

<?php
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if ($uploaded_size > 1000000)
{
echo "Your file is too large. Please re-size your image and retry. (Max file size = 1 MB)<br>";
$ok=0;
}

if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

if ($uploaded_type =="text/html")
{
echo "No HTML files<br>";
$ok=0;
}

if ($uploaded_type =="text/htm")
{
echo "No HTM files<br>";
$ok=0;
}

if ($uploaded_type =="text/xml")
{
echo "No XML files<br>";
$ok=0;
}

if ($ok==0)
{
Echo "An error occured during the upload, your file was not uploaded";
}

else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>

If you know how to modify that code to rename the file to the uploaders last name, please let me know.
David, your answer helps alot, but I do need to be able to keep the extension on the file, can someone explain how to add code to keep the file extension on the end of the filename?
 
Back
Top