首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件上传下载php

文件上传下载php
EN

Stack Overflow用户
提问于 2013-04-08 02:32:45
回答 5查看 194关注 0票数 1

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

代码语言:javascript
复制
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>";
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-04-08 02:37:38

在通过$code .= ''向其中添加更多内容之前,必须先定义$code (将$code = '';放在循环之前)

票数 0
EN

Stack Overflow用户

发布于 2013-04-08 02:36:46

在这种情况下,在for loop.Variables之外添加一个$code='';是要声明的,即连接操作,然后再使用它们。

票数 1
EN

Stack Overflow用户

发布于 2013-04-08 02:38:08

如果你想在for循环之外读取一个变量,你必须在之前定义它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15866171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档