Forum Moderators: coopster
I have put together a file upload script. It creates a random name for the uploaded file. I checked the database after uploading a test file and it is insrting the orginal filename into the database not the new random one :(
Can anyone possibly help?
[PHP]<?
// upload directory
$uploadDir = "$FileDir";
$MaxFileSize = $_FILES['userfile']['size'];
if ($MaxFileSize > 2097152) {
// if greater than 2MB - send nice rejection message }
echo "Sorry, your file is $size the maximum filesize allowed is $max_file_size bytes";
exit;
}
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO files (name, size, type, date, description) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$date', '$description')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br><b>The file was successfuly uploaded.</b><br><br>";
mysql_close($cid);
}
?>[/PHP]
[webmasterworld.com...]
much appreciated
Change:
$query = "INSERT INTO files (name, size, type, date, description) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$date', '$description')";
To:
$query = "INSERT INTO files (name, size, type, date, description) ".
"VALUES ('$randName', '$fileSize', '$fileType', '$date', '$description')";
$query = "INSERT INTO files (name, newname, size, type, date, description) ".
"VALUES ('$name', '$randName.$fileType', '$fileSize', '$fileType', '$date', '$description')";
Code above produces:
c7f26d224538af0376a7883ad8f931d8.text/plain
How would I get it to insert it as: c7f26d224538af0376a7883ad8f931d8.txt
Also, the name/$name variable is turning up blank in the database, any ideas?
However, the insert in the database is still producing:
5bcbf2571f330a1abf5c53f7038e18c7.text/plain
I am trying to get it to insert it as:
5bcbf2571f330a1abf5c53f7038e18c7.txt
<?
// upload directory
$uploadDir = "$FileDir";
$MaxFileSize = $_FILES['userfile']['size'];
if ($MaxFileSize > 2097152) {
// if greater than 2MB - send nice rejection message }
echo "Sorry, your file is $size the maximum filesize allowed is $max_file_size bytes";
exit;
}
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// get the file extension first
$ext = explode(".",$fileName);
$ext = strtolower($ext[1]);
// generate the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO files (name, newname, size, type, date, description) ".
"VALUES ('$fileName', '$randName.$fileType', '$fileSize', '$fileType', '$date', '$description')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br><b>The file was successfuly uploaded.</b><br><br>";
mysql_close($cid);
}
?>
You are concatenating the file name with the file type, which is why you are getting the insert with the mime type attached on the end.
$randName.$fileType
Have you tried changing that to:
$randName.$ext
$query = "INSERT INTO files (name, newname, size, type, date, description) ".
"VALUES ('$fileName', '$randName.$ext', '$fileSize', '$fileType', '$date', '$description')";
dc