I have an upload form as displayed below
Code: Select all
<?php
include('design/header.php');
require('connect.php');
$id = $_SESSION['id'];
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['uploaded']['name']) ;
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
function str_rand($length = 8, $seeds = 'alphanum')
{
// Possible seeds
$seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
$seedings['numeric'] = '0123456789';
$seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
$seedings['hexidec'] = '0123456789abcdef';
// Choose seed
if (isset($seedings[$seeds]))
{
$seeds = $seedings[$seeds];
}
// Seed generator
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
mt_srand($seed);
// Generate
$str = '';
$seeds_count = strlen($seeds);
for ($i = 0; $length > $i; $i++)
{
$str .= $seeds{mt_rand(0, $seeds_count - 1)};
}
return $str;
}
//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = str_rand(12, 'ABCDEFGHJKLMNPQRSTUVWXYZ0123456789').".";
//This assigns the subdirectory you want to save into... make sure it exists!
$target = "store/";
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
$sql = "INSERT INTO `users`
(`userid`,`location`)
VALUES ('".$id."','".$ran2.$ext."')";
$res = mysql_query($sql) or die(mysql_error());
echo "The file has been uploaded as ".$ran2.$ext;
}
else
{
echo "<br /><font size='6' face='arial' color='#33cc00'>Picture Upload</font>";
}
?>
<table width='30%'><tr><td>
<form enctype="multipart/form-data" action="upload.php" method="POST"><br />
Please choose a file: <input name="uploaded" type="file" /><br /><br />
Make this my default picture<input name="radiobutton" type="radio" value="radiobutton" />Yes <input name="radiobutton" type="radio" value="radiobutton" checked="checked" />
No<br />
<input type="submit" value="Upload" />
</td>
</tr>
</form>
<?
include("design/footer.php");
?>Code: Select all
$sql = "INSERT INTO `users`
(`userid`,`location`)
VALUES ('".$id."','".$ran2.$ext."')";Code: Select all
$sql = "INSERT INTO `users`
(`userid`,`location`,`defaultimage`)
VALUES ('".$id."','".$ran2.$ext."','1')";