BlueDolphin
BlueDolphin

Reputation: 35

file upload download php

I am writing a script for file upload to a MySQL db. I get an error saying : Notice: Undefined variable: code in C:\wamp\www\application\letters.php on line 82 This is the word code which is in **. Anyone that can spot the mistake please let me know.

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

Upvotes: 1

Views: 207

Answers (5)

Harpreet
Harpreet

Reputation: 719

Here is the snippet of code you must use in your script for uploading files:

<?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");
    }
}
?>

Check and see if it works for you.

Upvotes: 0

Harpreet
Harpreet

Reputation: 719

This is notice which warns you regarding undefined variables.

The execution of program does not affect by notice. To fix this you can initialize the $code variable somewhere in the starting of the script like $code = '';

Upvotes: 0

pietroalbini
pietroalbini

Reputation: 336

If you want to read a variable outside a for loop you must define it before.

Upvotes: 0

user1317647
user1317647

Reputation:

You must define $code before adding more into to it by $code .= '' (put $code = ''; before loop)

Upvotes: 0

rjv
rjv

Reputation: 6766

Add a $code=''; outside the for loop.Variables are to be declared in such cases,ie concatenation operation, before they are used.

Upvotes: 1

Related Questions