Alright, I am under the impression that this may have been caused by my own incompetence with PHP and ask that somebody help in figuring out what may have caused it. PLEASE.
Here is what happens when somebody wants to upload a file.
Enter 'upload.php' -- lists all files uploaded, gives option to add new file, remove certain file, or remove all files (max 5 files, max 1 mb uploaded).
If user chooses to add new file, they enter 'upload_add.php' -- asks for file path allows them to upload.
After file added, redirect to 'upload_test.php' -- this file handles all three options: adding files, deleting one file, deleting all files.
I save all files in a directory on my server called _user_upload in a username-specific subdirectory. All filenames are stored in the MySQL database
I will put the code for upload_test.php here (minus superfluous material like filesize checks and whatnot), code 1 relates to adding a file, code 2 relates to deleting one file, and code 3 relates to deleting all files for specific user.
<?php
$path_prefix='../../'; //set root directory for template reference
$code=$_GET['code']; //code 1 is upload one file, code 2 is remove single file, code 3 is remove all files
$fileid=$_GET['f']; //file number
$_SESSION['username']=strtolower($_SESSION['username']); //lowercase username
if($code==1) //upload one file
{
//Replace spaces with underscores, and no characters allowed, numbers and letters only, periods and underscores acceptable, everything lowercase
$form_data_name = ereg_replace('[ ]+', '_', strtolower($_FILES['userfile']['name']));
$form_data_name = ereg_replace('[^a-zA-Z0-9._]', '', $form_data_name);
$form_data_size = $_FILES['userfile']['size'];
//get file extension in next two lines
$fileInfo = pathinfo($_FILES['userfile']['name']);
$form_data_extension = $fileInfo['extension'];
//if file doesn't match one of the extensions listed below, redirect and give err code 5
$allowExtensions = array('html', 'htm', 'doc', 'png', 'rtf', 'jpg', 'jpeg', 'gif', 'pdf', 'ppt', 'psd', 'txt', 'xls'); //add the ones you require here
$fileInfo = pathinfo($_FILES['uploadname']['name']);
if(!in_array($form_data_extension, $allowExtensions))
{
header('Location: upload_add.php?code=5&f='.$fileid);
exit;
}
//if file already exists in slot, delete old and upload new file
$ress=mysql_query("SELECT * FROM upload WHERE u_username='".$_SESSION['username']."'"); //fetch upload information
$row=mysql_fetch_array($ress);
$old_file_name=$row["u_file".$fileid."_name"];
if($old_file_name)
{
unlink("../../_user_upload/".$_SESSION['username']."/".$old_file_name);
}
//check if user-specific directory exists, if not, make directory
if(!file_exists($path_prefix."_user_upload/".$_SESSION['username']."/"))
{
mkdir($path_prefix."_user_upload/".$_SESSION['username']);
chmod($path_prefix."_user_upload/".$_SESSION['username'], 0774); //chmod it, read/write for user, group.
}
//now that file is not malicious and has passed all tests, move it from tmp to upload directory
$targetdir = $path_prefix."_user_upload/".$_SESSION['username']."/";
move_uploaded_file($_FILES['userfile']['tmp_name'], $targetdir.$form_data_name);
$query = "UPDATE upload
SET u_file".$fileid."_name = '".$form_data_name."',
u_file".$fileid."_date = '".date("m.d.Y")."'
WHERE
u_username = '".$_SESSION['username']."'";
mysql_query($query) or die(mysql_error());
chmod($targetdir.$form_data_name, 0754); //chmod it, read/write for user, group, and world.
header('Location: upload.php?code=1&f='.$fileid); //if file uploaded successfully, redirect and give success code 1
exit;
}
if($code==2) //remove single file
{
$ress=mysql_query("SELECT * FROM upload WHERE u_username='".$_SESSION['username']."'"); //fetch upload information
$row=mysql_fetch_array($ress);
$file_name=$row["u_file".$fileid."_name"];
//delete file:
if(file_exists($path_prefix."_user_upload/".$_SESSION['username']."/".$file_name))
{
unlink($path_prefix."_user_upload/".$_SESSION['username']."/".$file_name);
}
$query = "UPDATE upload
SET u_file".$fileid."_name = '',
u_file".$fileid."_date = '00.00.0000'
WHERE
u_username = '".$_SESSION['username']."'";
mysql_query($query) or die(mysql_error());
header('Location: upload.php?code=2&f='.$fileid); //if remove single file successful, redirect and give success code 2
exit;
}
if($code==3) //remove all files
{
//remove user directory...function found at http://aidan.dotgeek.org/lib/?file=function.rmdirr.php
function rmdirr($dirname)
{
// Sanity check
if (!file_exists($dirname))
{
return false;
}
// Simple delete for a file
if (is_file($dirname) || is_link($dirname))
{
if (!file_exists($dirname))
{
return true;
}
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read())
{
// Skip pointers
if ($entry == '.' || $entry == '..')
{
continue;
}
// Recurse
rmdirr("$dirname/$entry");
}
// Clean up
$dir->close();
return rmdir($dirname);
}
if(rmdirr($path_prefix."_user_upload/".$_SESSION['username']))
{
$query = "UPDATE upload
SET u_file1_name = '',
u_file1_date = '00.00.0000',
u_file2_name = '',
u_file2_date = '00.00.0000',
u_file3_name = '',
u_file3_date = '00.00.0000',
u_file4_name = '',
u_file4_date = '00.00.0000',
u_file5_name = '',
u_file5_date = '00.00.0000'
WHERE
u_username='".$_SESSION["username"]."'";
mysql_query($query) or die(mysql_error());
}
header('Location: upload.php?code=3'); //if remove all files successful, redirect and give success code 3
exit;
}
?>
I really hope that isn't too much, but I would really like a few people to look at it and see if it's safe/secure, if chmod permissions are alright, if this is the best way to implement, etc.
Thanks a ton.
-influx