1

我正在使用模型-视图-控制器结构开发一个网站。在我的 html 中,我有一个表单,允许用户浏览和选择文件,然后通过单击按钮提交文件,然后调用控制器代码:

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';
}

第一行:upload_quotes() 是位于模型代码中的函数。如果上传成功,该函数返回 true,如果不成功,则返回 false。我已经注释掉了获取目录列表的函数调用,因为它也有错误。2个模型代码片段:

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;
}

processFileUpload.php 和 errorPage.php 文件相应地输出 $msg 变量,但它从不输出成功。我花了几个小时研究我做错了什么,而我对 php 的有限知识没有任何帮助。查看页面输出“发生未知错误。错误:1。浏览器上弹出的错误是:

警告:move_uploaded_file() [function.move-uploaded-file]:copy() 函数的第二个参数不能是第 17 行 C:\xampp\htdocs\Test Site\model\model.php 中的目录

警告:move_uploaded_file() [function.move-uploaded-file]:无法将 'C:\xampp\tmp\php487A.tmp' 移动到 C:\ 中的 '../../../TestSiteDataFiles/Images/' 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 [错误] => 0 [大小] => 55 )

在我看来,模型代码 ( upload_quotes()) 是错误的根源,因为它每次都返回为 false。该get_directory_contents()函数永远不会有机会执行,但它也不会输出正确的结果。

我感谢任何和所有建议,并感谢您的投入。

4

2 回答 2

3

通过$_FILES作为参数传递给您的函数,您在实际的$_FILES超全局中混淆了文件信息的来源。由于您$_FILES['userfile']['name']在函数中访问,但传递$_FILES['userfile']给函数,因此未定义名称键,并且您会收到目录错误。

使用用户输入文件名在文件系统上存储文件是非常危险的。相反,最好创建一个唯一的文件名。from 的原始输入文件名$_FILES[]['name']对于存储在数据库中并与磁盘上的文件关联作为要显示的元数据很有用,但它不应该用于存储在磁盘上。

// 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;
    }
}

更新

get_directory_contents()函数失败,因为数组$directory_list定义超出范围。在调用函数之前无需将其定义为数组。改为在里面做:

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;
}

这只是一个真正的问题,因为您使用array_push()而不是[]数组附加表示法。 array_push()必须将现有数组作为其第一个参数,并且它不会自己创建一个。

// Would have worked since the array would get initialized if it didn't exist in scope already.
$directory_list[] = $file;
于 2012-04-10T02:18:45.410 回答
0

我想你会发现你的上传函数中的变量名不匹配。

一开始你有$_FILES['userfile']['name'],但在move_uploaded_file你使用的功能中$_FILES['tmp_name']

也许检查变量是否包含您的期望。您可能会发现他们正在评估一个空字符串,当与您正在设置的路径连接时,会导致设置一个目录。

编辑:实际上,您还传递了一个调用$_FILES到您的upload_quotes函数中的变量,但是$_FILES它是一个超级全局变量,因此在所有范围内(即任何地方)都可用。重命名 的参数upload_quotes(以及相应的任何相关变量)可能会解决您的问题。

于 2012-04-10T02:16:34.960 回答