I am using the following code to upload files limited to zip or rar, upon upload I have it set to rename the uploaded files to the id of the record placed in the database adding the file extension onto that.
Problem is that it leaves the file extension off ie
1
2
etc
etc
no file extension on the uploaded file.
also when it inserts the data (filename) into the database the file extension is marked as ie
1.x-zip-compressed
2.x-zip-compressed
I am wanting to have a page that allows people to download the files using links and need the file extensions to be ie
1.rar
2.zip
etc
etc
depending on the file type uploaded.
code:
Code: Select all
$file_dir = "files";
$counter = 0;
$allowed_file_types = array(
'application/x-zip-compressed' => 'x-zip-compressed');
$file = $_FILES['file'];
while($counter <= count($file))
{
if($file['size'][$counter] > 0)
{
if(!array_key_exists($file['type'][$counter], $allowed_file_types))
{
echo 'File '.($counter+1).' is not a accepted file type!<br />';
}else{
@mysql_query("INSERT INTO filename (filename) VALUES ('0')");
$new_id = mysql_insert_id();
$filetype = $file['type'][$counter];
$extention = $allowed_file_types[$filetype];
$filename = $new_id.".".$extention;
@mysql_query("UPDATE filename SET modelid='".$_GET['modelid']."', filename='".$filename."', userid='".$_SESSION['userid']."' WHERE fileid='".$new_id."'");
$newfile = $file_dir.'/'.$filename[$counter];
move_uploaded_file($_FILES['file']['tmp_name'][$counter], $newfile);
}
}
$counter++;
}How to solve this?
Thanks