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.
By passing
$_FILESto your function as a parameter, you have confused the source of your file info in the actual$_FILESsuperglobal. Since you access$_FILES['userfile']['name']in the function, but pass$_FILES['userfile']to the function, the name key is not defined, and you get the directory error.It is incredibly dangerous to use the user input filename to store a file on your filesystem. Instead, it is better to create a filename that will be unique. The original input filename from
$_FILES[]['name']is useful to store in a database and associate with a file on disk as metadata to display, but it shouldn’t be used to store on disk.Update
The
get_directory_contents()function fails because the array$directory_listis defined out of scope. There’s no need to define it as an array before calling the function. Do it inside instead:This was only really a problem because you used
array_push()instead of the[]array append notation.array_push()must take an existing array as its first param, and it won’t create one itself.