This PHP script uploads and re-names a file to the uploaders' last name, but

michaelnaeseth

New member
there is no period before the ext? I am using a php script (shown below) to allow users to upload files to my web server. For some reason, after it renames the file, it removes the period before the extension. What can I add to the code to get it to upload the file with the period intact?

<?php
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
$explode = explode('.', $_FILES['uploaded']['name']);
$extension = array_pop($explode);
$newname = $_POST['lastname'].$extension;

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'], 'uploads/'.$newname))
{
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>

5 stars will go to the person who can fix this very small issue and lend some advice on why my file exceptions aren't working (php/html/htm)
 
Just change:

$newname = $_POST['lastname'].$extension;

To

$newname = $_POST['lastname'].'.'.$extension;

The extension never carries the dot.

Good luck
 
Back
Top