I am developing a website using the Model-View-Controller structure. In my html, I have a form that allows the user to browse and select a file, and submit the file by clicking the button which then calls the controller code:
if(upload_quotes($_FILES['userfile'])){
$msg = "Successful file upload";
$directory_list = array();
// get contents of directory here
// $directory_list = get_directory_contents($directory_list);
include '../view/processFileUpload.php';
}else{
$msg = "Unknown error occurred. Error: ".print_r($_FILES);
include '../view/errorPage.php';
}
The first line: upload_quotes() is a function located in the model code. The function returns true if successful upload and false if not successful. I have commented out the function call that gets the directory listing because there are errors in it as well. The 2 model code snippets:
function upload_quotes($_FILES){
$uploadfile = '../../../TestSiteDataFiles/Quotes/'.$_FILES['userfile']['name'];
if(move_uploaded_file($_FILES['tmp_name'], $uploadfile)){
return true;
}else{
return false;
}
}
function get_directory_contents($directory_list){
$current_dir = '../../../TestSiteDataFiles/Quotes/';
$dir = opendir($current_dir);
//reads and outputs the directory
while(false !== ($file = readdir($dir)))
//strip out the two entries of . and ..
if($file != "." && $file !=".."){
array_push($directory_list, $file);
}
closedir($dir);
return $directory_list;
}
The processFileUpload.php and errorPage.php files output the $msg variable accordingly however it never outputs as successful. I've spent hours researching what i've done wrong and my limited knowledge of php isn't any help. The view page outputs "Unknown error occured. Error: 1. And the errors that pop up on the browser are:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\xampp\htdocs\Test Site\model\model.php on line 17
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php487A.tmp' to '../../../TestSiteDataFiles/Images/' in C:\xampp\htdocs\Test Site\model\model.php on line 17 Array ( [name] => Managerial Accounting Davers Connect.txt [type] => text/plain [tmp_name] => C:\xampp\tmp\php487A.tmp [error] => 0 [size] => 55 )
It appears to me as the model code (upload_quotes()) is the source of the error since it returns as false everytime. the get_directory_contents() function never has a chance of executing but it does not output the correct results either.
I appreciate any and all suggestions and thank you for your inputs.
upload_quotes()does NOT return true if there was a successful upload. It returns true if a move occured. You shouldn't be trying to move an uploaded file until you've verified it actually exists, which is why there's$_FILES['userfile']['error']. If that's0(aka UPLOAD_ERR_OK), THEN you've got a successful upload... and as deceze points out, most likely have a compromised server to boot. - Marc B