Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:18:25+00:00 2026-06-01T20:18:25+00:00

I am developing a website using the Model-View-Controller structure. In my html, I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:18:26+00:00Added an answer on June 1, 2026 at 8:18 pm

    By passing $_FILES to your function as a parameter, you have confused the source of your file info in the actual $_FILES superglobal. 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.

    // Use a different variable name. I've replaced it with $fileinfo
    function upload_quotes($fileinfo){
    
        // Don't use the original filename to store it. Create one instead.
        $fname = uniqid();
        $finfo = pathinfo($fileinfo['name']);
        // Append the user's file extension to a random filename
        $fname .= "." . $finfo['extension'];
    
        $uploadfile = '../../../TestSiteDataFiles/Quotes/'.$fname;
    
        // Don't attempt to move the file unless its error container is empty
        if(empty($fileinfo['error']) && move_uploaded_file($fileinfo['tmp_name'], $uploadfile)){
            return true;
        }else{
            return false;
        }
    }
    

    Update

    The get_directory_contents() function fails because the array $directory_list is defined out of scope. There’s no need to define it as an array before calling the function. Do it inside instead:

    function get_directory_contents($directory_list){
        $current_dir = '../../../TestSiteDataFiles/Quotes/';
        $dir = opendir($current_dir);
    
        // Define $directory_list as an array IN HERE
        $directory_list = array();
    
        //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;
    }
    

    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.

    // Would have worked since the array would get initialized if it didn't exist in scope already.
    $directory_list[] = $file;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a website using zend framework. i have a search form with
I am developing a website using Codeigniter that will have different subdomains for different
I am developing a website using MVC 3, I have a web setup project
I'm developing an online website (using Django and Mysql). I have a Tests table
I am developing a website using ASP.Net MVC 1.0. Can i host that website
i am developing a website using asp.net 3.5.i have created a cascading dropdownlist for
I've recently been developing a website using asp.net webforms that uses in proc sessions
I'm developing a dynamic website using jQuery and I have found several jQuery plugins
I am developing a website using Google App Engine and Django 1.0 (app-engine-patch) A
I'm developing a website using the Django framework, and I need to retrieve Jabber

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.