文件上传下载php

时间:2013-04-07 18:32:45

标签: php mysql

我正在编写一个文件上传到MySQL数据库的脚本。 我收到一个错误说: Notice: Undefined variable: code in C:\wamp\www\application\letters.php on line 82 这是**中的单词代码。任何能发现错误的人都请告诉我。

if ($name)
    if ($title && $description)
        {       
            $date = date("d m Y");
            $charset ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            //length of value to generate
            $length = 15;

            //create variable and run through and randomly select fromcharset.
            for ($i = 0; $i <= $length; $i++)
            {   
                //position to start at. rand function.
                $rand = rand() % strlen($charset);
                $tmp = substr($charset, $rand, 1);

                //append onto code
                **$code .= $tmp;**
            }
            //checking for existence of code which is generated. 
            $query= mysql_query("SELECT code FROM letter_details WHERE code = '$code'");

            //if code is found
            $numrows = mysql_num_rows($query);

            // if that code exists, generate code again
            while ($numrows != 0)
            {
                for ($i=0; $i <= $length; $i++)
                {
                    //position to start at. rand function.
                    $rand = rand() % strlen($charset);
                    $tmp = substr($charset, $rand, 1);

                    //append onto code
                    $code .=$tmp;
                }

                //checking for existence of code which is generated.
                $query= mysql_query("SELECT code FROM letter_details WHERE code = '$code'");

                //if code is found
                $numrows = mysql_num_rows($query);
            }

            //create directory
            mkdir("files/$code");

            //put file into it
            move_uploaded_file($tmpname, "files/$code/"."$name".$ext);
            $query = mysql_query("INSERT INTO letter_details VALUES ('$letter_id', $title','$code','$description','$student_info_id', '$staff_info_id', '$date')");
            echo "Your file '$title' was Succesfully uploaded.<br><br><a href='download.php?file=$code'>Download</a>";

5 个答案:

答案 0 :(得分:1)

$code='';循环之外添加for。在这种情况下,变量将被声明,即连接操作,然后再使用它们。

答案 1 :(得分:0)

您必须先定义$code,然后再按$code .= ''向其添加更多内容(在循环前添加$code = '';

答案 2 :(得分:0)

如果要读取for循环之外的变量,必须先定义它。

答案 3 :(得分:0)

这是关于未定义变量的警告。

程序的执行不受通知的影响。 要解决此问题,您可以在脚本的开头某处初始化$ code变量,如$code = '';

答案 4 :(得分:0)

以下是您必须在脚本中用于上传文件的代码段:

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

检查并查看它是否适合您。