未定义的变量,不允许我将文件插入数据库表列

时间:2014-06-09 22:02:51

标签: php mysql sql

我将音频文件上传到目录并将文件名信息存储在数据库中。我成功地做到了这一点。

接下来我想为每个音频文件创建一个默认图像,方法是使用PHP将音频文件Title添加到图像中,然后添加文件名的唯一ID。我成功完成了这个。我通过following this tutorial.

了解到了这一点

示例图片:

enter image description here

我遇到的问题是在上传时将音频文件图像插入数据库列。 这是一个数据库图像。我需要新创建的音频图像的文件名进入 rt_file 列。所以我可以稍后再说。

enter image description here

我希望在我出错的地方得到一些帮助。

我得到的当前错误是:未定义的变量:rt_file 。 哪个我不明白为什么......?这是不是像我在上面的insert语句中定义的那样定义它:

$rt_file = preg_replace('/\s+/', '-', "../ringtones/rtimage/".($ringtone_image[0]['ring_name'])."-".substr(md5(microtime()),rand(0,26),5).".jpg");

我真的很想发布这么多代码,我知道它不赞成,所以我提前道歉,但我不确定问题出在哪里。

如果有人有时间仔细研究一下那会很棒,如果不是我完全理解的话。

这是我的代码:

<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/core/database.php");


$ringDir= "../ringtones/";

if(!is_dir($ringDir)){

    mkdir('../ringtones/', 0775);
}



.   
if (isset($_FILES['file'])) {

    if (empty($_FILES['file']['name'])) {

            ?>
<div class="add-errors">Please add an audio file!</div>
<?php 

    }   else {

        $name       =   $_FILES['file']['name'];
        $filename   =   preg_replace("/\.[^.]+$/", "", $name);
        $temp       =   $_FILES['file']['tmp_name'];
        $type       =   $_FILES['file']['type'];
        $size       =   $_FILES['file']['size'];
        $ext        =   strtolower(end(explode('.', $name)));

  $upload   =   substr(md5(microtime()),rand(0,26),5);

        // Restrictions for uploading
        $allowed    =   array(
        'audio/mp3',
        'audio/mp4',
        'audio/mpeg',
        'audio/ogg',
        'audio/opus',
        'audio/webm');

        // Recognizing the extension
        switch($type){

            case 'audio/mp3':
                    $ext= '.mp3';
            break;

            case 'audio/mp4':
                    $ext= '.mp4';
            break;

            case 'audio/mpeg':
                    $ext= '.mpeg';
            break;

            case 'audio/ogg':
                    $ext= '.ogg';
            break;

            case 'audio/opus':
                    $ext= '.opus';
            break;

            case 'audio/webm':
                    $ext= '.webm';
            break;

        }

        // upload variables
        $ring_path      =   $ringDir.$filename."-".$upload.$ext;





        // check if extension is allowed.
        if (in_array($type, $allowed)) {

            // Checking the size of the audio file.
                if ($size <= 5242880) {

                        // Move the original file aswell.
                        move_uploaded_file($temp, $ring_path);



/*#############THIS IS WHERE I'M ADDING THE TEXT TO THE DEFAULT IMAGE###########
################################################################################*/

// link to the font file no the server
$fontname = '../fonts/Roboto-Regular.ttf';
// controls the spacing between text
$i=30;
//JPG image quality 0-100
$quality = 100;

function create_image($ringtone_image){

        global $fontname;   
        global $quality;
        $rt_file = preg_replace('/\s+/', '-', "../ringtones/rtimage/".($ringtone_image[0]['ring_name'])."-".substr(md5(microtime()),rand(0,26),5).".jpg");  

     //if the file already exists dont create it again just serve up the original   
    if (!file_exists($rt_file)) {   
    }

            // define the base image that we lay our text on
            $im = imagecreatefromjpeg("../ringtones/ringtone-image.jpg");

            // setup the text colours
            $color['white'] = imagecolorallocate($im, 255, 255, 255);

            // this defines the starting height for the text block
            $height = 50;
            $y = imagesy($im) - $height;

        // loop through the array and write the text    
        foreach ($ringtone_image as $value){
            // center the text in our image - returns the x value
            $x = center_text($value['ring_name'], $value['font-size']);

            imagettftext($im, $value['font-size'], 0, $x, $y, $color[$value['color']], $fontname,$value['ring_name']);
            // add 32px to the line height for the next text block
            //$i = $i+32;   

        }
            // create the image
            imagejpeg($im, $rt_file, $quality);

    //}

        return $rt_file;    
}

function center_text($string, $font_size){

            global $fontname;

            $image_width = 550;
            $dimensions = imagettfbbox($font_size, 0, $fontname, $string);

            return ceil(($image_width - $dimensions[4]) / 2);               
}



    $ringtone_image = array(

        array(
            'ring_name'=> 'Add Ringtone Title', 
            'font-size'=>'16',
            'color'=>'white'),

    );



    $ringtone_image = array(

        array(
            'ring_name'=> $_POST['ring_name'], 
            'font-size'=>'16',
            'color'=>'white'),


    );      



// run the script to create the image
$filename = create_image($ringtone_image);

/*#############END TEXT TO DEFAULT IMAGE###########
###################################################*/


                        try {

                            $ring_id            =   Input::get('ring_id');
                            $creation           =   date('Y-m-d H:i:s');
                            $ring_name          =   Input::get('ring_name');
                            $category           =   Input::get('category_id');
                            $ring_path          =   $ring_path;
                            $rt_file            =   $rt_file;   


                            $insertdata = DB::getInstance()->insert('ringtones', array(
                                'ring_id'            => $ring_id,
                                'creation'           => $creation,
                                'ring_name'          => $ring_name,
                                'category_id'        => $category,
                                'ring_path'          => str_replace("../ringtones/", "", $ring_path),
                                'rt_file'           => str_replace("../ringtones/rtimages/", "", $rt_file),
                            ));


                            if(!$insertdata) {

                                ?>
<div class="add-errors">There was a problem uploading the ringtone!</div>
<?php 

                            } else {

                                ?>
<div class="add-message">Your ringtone has been uploaded! <a href="add-ringtone.php">Add another</a> or <a href="../index.php">Return Home</a> </div>
<?php 
                            }

                        } catch(Exception $e) {
                            ?>
<div class="add-errors">
        <?php die($e->getMessage()); ?>
</div>
<?php 
                        }

                } else {
                    ?>
<div class="add-errors">Your ringtone size is too big!</div>
<?php
                }

            }

         else {
            ?>
<div class="add-errors">Your have uploaded a forbidden extension!</div>
<?php

}

}

}

?>
<section>
        <h1>Audio Upload</h1>
        <p>Use the form below to upload new ringtone</p>
        <form action="" method="post" enctype="multipart/form-data">
                <label for="ring_name">Ringtone Title</label>
                <input type="text" name="ring_name" maxlength="42" placeholder="Give this field a title">
                <label for="category_id">Choose a category</label>
                <select name="category_id">
                        <option value="">Please Choose</option>
                        <option value="1">Option 1</option>
                        <option value="2">Option 2</option>
                        <option value="3">Option 3</option>
                        <option value="4">Option 4</option>
                        <option value="5">Option 5</option>
                        <option value="6">Option 6</option>
                </select>
                <label for="file">Ringtone</label>
                <input type="file" name="image" >
                <p id="size">Maximum file size of 5MB</p>
                <input type="submit" value="Upload">
        </form>
</section>

1 个答案:

答案 0 :(得分:1)

试试这个:

$rt_file = null; // here we're initiate the variable with null to avoid undefined var problem

function create_image($ringtone_image) {
    global $rt_file; // here we're accessing global variable to use it inside function
    // rest part
}