PHP photo upload problem?

  • Thread starter Thread starter jack
  • Start date Start date
J

jack

Guest
I made a form some time ago for users to upload images to the site, and each image would go to the main file manager, as the info for the pic and who uploaded it would go to an sql db.
It works great other than when users visit the page, a blank set of data for the user who visited the page is sent to the sql db.
I have it so the upload pic is randomized and unique and the file extention is set afterwards.

So if I were to upload something like 123.jpg, it would show up in the sql as ahw3a348rh.jpg [or something like that]

now if I visit the page to upload the pic, i get something like ashdy3uih.

as you can see there's no extention.. it just added some random unique randomization deal..
so yeah, how do i get that to stop, my code follows:



<form enctype="multipart/form-data" action="uploadpic.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000">
<font color='#d5074c'>Please choose a file: </font>
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" id="upload" value=" Upload ">
</form>

<?php
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($_FILES['userfile']['name']) ;
$ran = rand () ;
$ran2 = $ran.".";
$target = "images/users/";
$target2 = $target . $ran2.$ext;


$fileName = $_FILES['userfile']['name'];
$userName = $_FILES['userfile']['user'];

$query = "INSERT INTO upload (name, user ) ".
"VALUES ('$ran2$ext', '$user')";
mysql_query($query) or die('Error, query failed');

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target2))


{
echo "<font color=white size=2>Your Image Was Uploaded Successfully.</font>";
}
else
{
echo "";
}
?>
 
Back
Top