|
File(s)/Folder(s) Uploaded
|
| Filepath Name |
File Size |
//===========================================================================================================
//Prerequisite Requirement... =
//===========================================================================================================
$WebServerFileSystem = "UNIX/LINUX"; //Option are 1) UNIX/LINUX, 2) DOS/WINDOWS, ....
$SavedPath = "/home/website/emarket/secure/WebHelp/";
//===========================================================================================================
//===========================================================================================================
$file = $_FILES['userfile'];
$FileTotalCount = count($file['name']);
//===========================================================================================================
//Script to check the client's O/S does the files/folders get uploaded from... =
//===========================================================================================================
if (strrpos(urldecode($file['name'][0]),"\\") === false) { //Note: strrpos() in PHP 4.0b3 and newer, note our use of ===, simply == would not work as expected because the 1st character's position was the 0th (first) character...
$ClientDOSorUnixFileSytemQQ = "/"; //This is UNIX/LINUX filesystem...
} else {
//Problem #1: What's the Apple's filepath before MAC O/S version 10? We know that MAC O/S version 10 and up is an UNIX O/S, a clone of BSD of some sort....
$ClientDOSorUnixFileSytemQQ = "\\"; //This is DOS/Windows filesystem...
}
//===========================================================================================================
//===========================================================================================================
/* *********************************************************************** */
/* Script to strip out part of the filepath, down to the minimum allowed */
/* filepaths regardless of what O/S does files/folders come from... */
/* *********************************************************************** */
/* */
/* Example #1: /abc/def/jkl/mno/filename1.txt */
/* /abc/def/jkl/mno/filename2.exe */
/* /abc/def/jkl/mno/folder1/filename3.pdf */
/* */
/* is altered to this */
/* */
/* filename1.txt */
/* filename2.exe */
/* folder1/filename3.pdf */
/* */
/* Example #2: /abc/def/folder1/filename1.txt */
/* /abc/def/folder2/filename2.pdf */
/* */
/* is altered to this */
/* */
/* folder1/filename1.txt */
/* folder2/filename2.pdf */
/* */
/* Example #3: /abc/def/mno/folder1/filename1.exe */
/* */
/* is altered to this */
/* */
/* filename1.exe (folder1 is stripped out if only one file)... */
/* */
/* *********************************************************************** */
/* *********************************************************************** */
//===========================================================================================================
//Script to find the common filepath that will need to be strip out later on... =
//===========================================================================================================
if ($FileTotalCount == 1) { //Only 1 file was uploaded...
$pos = strrpos(urldecode($file['name'][0]),$ClientDOSorUnixFileSytemQQ);
} else { //2 or more files/folders were uploaded...
//Problem #2 -- strrpos() only work in PHP 5.0 and up where the 3rd parameter "offset" can be used, so do strpos() instead for our websites with PHP 4.0.6, PHP 4.2.1, PHP 4.2.3 and PHP 4.3.6, etc...
$UploadFileCount1 = 0;
while ($UploadFileCount1 != $FileTotalCount) {
//=======================================================
//Script to find the total numbers of filepath slashes...
//=======================================================
if ($UploadFileCount1 == 0) {
$SubStrCount = substr_count(urldecode($file['name'][0]),$ClientDOSorUnixFileSytemQQ);
} else {
if ($SubStrCount == 0) { //Stop the loop if there's no directory slash in the filepath...
break;
} else {
$SubStrTempCount = substr_count(urldecode($file['name'][$UploadFileCount1]),$ClientDOSorUnixFileSytemQQ);
if ($SubStrTempCount < $SubStrCount) { //Fewer number of directories is what we want to strip out, out of the full filepath...
$SubStrCount = $SubStrTempCount;
}
}
}
//=======================================================
$UploadFileCount1++;
}
$UploadFileLoopContinue = "true";
while ($UploadFileLoopContinue != "false") {
$StrPos = 0;
//===========================================================
//Script to find the common filepath for all files/folders...
//===========================================================
for ($UploadFileCount2=0;$UploadFileCount2 != $FileTotalCount;$UploadFileCount2++) {
if ($UploadFileCount2 == 0) {
for ($SubStrLoopCount=0;$SubStrLoopCount != $SubStrCount;$SubStrLoopCount++) {
$StrPos = strpos(urldecode($file['name'][0]),$ClientDOSorUnixFileSytemQQ,$StrPos);
$StrPos += 1;
}
$FilePath = substr(urldecode($file['name'][0]),0,$StrPos);
} else {
if ($FilePath != substr(urldecode($file['name'][$UploadFileCount2]),0,$StrPos)) {
$SubStrCount--; //If filepaths aren't in common then reduce the total number of director(ies)...
break; //Kill the loop and restart the loop via the if-statement below...
}
}
}
if ($UploadFileCount2 == $FileTotalCount) {
$UploadFileLoopContinue = "false";
break;
}
//===========================================================
}
$FilePathLen = strlen($FilePath);
}
//===========================================================================================================
//===========================================================================================================
//Script to start placing file(s)/folder(s) on the webserver... =
//===========================================================================================================
if ($FileTotalCount == 1) { //Only 1 file was uploaded...
if (!($pos === false)) { //Note: strrpos() in PHP 4.0b3 and newer, note our use of ===, simply == would not work as expected because the 1st character's position was the 0th (first) character...
$file['name'][0] = substr(urldecode($file['name'][0]),($pos+1),strlen(urldecode($file['name'][0])));
}
//Filename with no filepath, so place it here. Overwrite it if it exist on the webserver...
move_uploaded_file($file['tmp_name'][0],$SavedPath.$file['name'][0]);
//Can use the if-statement to check the file extension and set a different file permission based on it...
chmod($SavedPath.$file['name'][0],0644);
echo "";
echo " | ".$file['name'][0]." | ";
echo " ".$file['size'][0]." | ";
echo "
";
} else { //2 or more files/folders were uploaded...
for ($UploadFileCount3=0;$UploadFileCount3 != $FileTotalCount;$UploadFileCount3++) {
//=======================================================================================================================
//Script to strip out the unwanted filepath and do the filepath conversion based on the client's O/S and Webserver O/S...
//=======================================================================================================================
$file['name'][$UploadFileCount3] = substr(urldecode($file['name'][$UploadFileCount3]),$FilePathLen,strlen(urldecode($file['name'][$UploadFileCount3]))); //Better not to use the str_replace() since it's a possibility that $FilePath may match part of the filename...
//If the webserver is UNIX/LINUX then do this...
if ($WebServerFileSystem == "UNIX/LINUX") { //Convert DOS filepath to UNIX/LINUX filepath...
$file['name'][$UploadFileCount3] = str_replace("\\","/",$file['name'][$UploadFileCount3]);
$WebServerDOSorUnixFileSytemQQ = "/";
} else if ($WebServerFileSystem == "DOS/WINDOWS") {
$file['name'][$UploadFileCount3] = str_replace("/","\\",$file['name'][$UploadFileCount3]);
$WebServerDOSorUnixFileSytemQQ = "\\";
}
//=======================================================================================================================
//======================================================================
//Script to create directory folders and place files on the webserver...
//======================================================================
$pos = strrpos($file['name'][$UploadFileCount3],$WebServerDOSorUnixFileSytemQQ); //PHP function is strrpos(), not strpos()...
if ($WebServerFileSystem == "UNIX/LINUX") {
//==========================================================================================================================
//Example code of chmod() that worked through PHP, not the same as using shell script through the dumb terminal or telnet...
//==========================================================================================================================
/*
chmod("/somedir/somefile", 644); //Note: decimal; probably incorrect
chmod("/somedir/somefile", "u+rw,go+r"); //Note: string; incorrect
chmod("/somedir/somefile", 0644); //Note: octal; correct value of mode
*/
//==========================================================================================================================
//===================================================================================================================================
//Another example code for mkdir() that worked through PHP, not the same as using shell script through the dumb terminal or telnet...
//Again, chmod() feature is built-in to the mkdir()...
//===================================================================================================================================
/*
mkdir("/somedir/somefile", 755); //Note: decimal; probably incorrect
mkdir("/somedir/somefile", "u+rwx,go+rx"); //Note: string; incorrect
mkdir("/somedir/somefile", 0755); //Note: octal; correct value of mode
*/
//===================================================================================================================================
if (!($pos === false)) { //Note: strrpos() in PHP 4.0b3 and newer, note our use of ===, simply == would not work as expected because the 1st character's position was the 0th (first) character...
//Filename with filepath, so attempt to create director(ies) if there isn't one(s) on the webserver
$UploadDirectoryPath1 = dirname($file['name'][$UploadFileCount3]);
$UploadFileName = basename($file['name'][$UploadFileCount3]);
$UploadDirectoryArray = array(); //To wipe out the old data...
$UploadDirectoryArray = explode($WebServerDOSorUnixFileSytemQQ,$UploadDirectoryPath1);
$UploadDirectoryArrayCount = count($UploadDirectoryArray);
if ($UploadDirectoryArrayCount == 1) {
clearstatcache();
$ZzTempDirPath = $UploadDirectoryArray[0];
if (is_dir($SavedPath.$ZzTempDirPath) != true) {
mkdir($SavedPath.$ZzTempDirPath,0755);
}
} else {
for ($UploadDirCount1=0;$UploadDirCount1<$UploadDirectoryArrayCount;$UploadDirCount1++) {
clearstatcache();
//echo $UploadDirCount1;
if ($UploadDirCount1 == 0) {
$ZzTempDirPath = $UploadDirectoryArray[0];
} else {
$ZzTempDirPath .= "/".$UploadDirectoryArray[$UploadDirCount1];
}
//echo $ZzTempDirPath." ***";
if (is_dir($SavedPath.$ZzTempDirPath) != true) {
mkdir($SavedPath.$ZzTempDirPath,0755);
}
}
}
//Filename with no filepath, so place it here. Overwrite it if it exist on the webserver...
move_uploaded_file($file['tmp_name'][$UploadFileCount3],$SavedPath.$ZzTempDirPath."/".$UploadFileName);
//Can use the if-statement to check the file extension and set a different file permission based on it...
chmod($SavedPath.$ZzTempDirPath."/".$UploadFileName,0644);
} else {
//Filename with no filepath, so place it here. Overwrite it if it exist on the webserver...
move_uploaded_file($file['tmp_name'][$UploadFileCount3],$SavedPath.$file['name'][$UploadFileCount3]);
//Can use the if-statement to check the file extension and set a different file permission based on it...
chmod($SavedPath.$file['name'][$UploadFileCount3],0644);
}
echo "";
echo " | ".$file['name'][$UploadFileCount3]." | ";
echo " ".$file['size'][$UploadFileCount3]." | ";
echo "
";
} else if ($WebServerFileSystem == "DOS/WINDOWS") {
//Need to write some codes for DOS/WIndows filesystem since they don't use chown, chmod, etc...
}
//======================================================================
}
}
//===========================================================================================================
if(!isset($SavedPath)||$SavedPath=="")
{
echo "| |
";
echo "| Files have not been saved, please edit uploader_response.php to match your configuration |
";
}
?>