php - Undefined variable not allowing me to insert file into database table column -
i'm uploading audio files directory , storing file name info in database. have done successfully.
next wanted create default image each audio file, use php add audio files title image , tack on unique id file name. have done successfully. learned following tutorial.
example image:
the problem i'm having getting audio files image insert database column on upload. here database image. need file name of newly created audio image go rt_file column. can echo out later.
i hoping on i'm going wrong.
the current error is: undefined variable: rt_file. don't understand why...? not define have defined above insert statement this:
$rt_file = preg_replace('/\s+/', '-', "../ringtones/rtimage/".($ringtone_image[0]['ring_name'])."-".substr(md5(microtime()),rand(0,26),5).".jpg");
i hate post code know it's frowned upon, apologize in advance, i'm not sure problem lies.
if has time through great, if not totally understand.
here code:
<?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 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 uploading $allowed = array( 'audio/mp3', 'audio/mp4', 'audio/mpeg', 'audio/ogg', 'audio/opus', 'audio/webm'); // recognizing 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 allowed. if (in_array($type, $allowed)) { // checking size of audio file. if ($size <= 5242880) { // move original file aswell. move_uploaded_file($temp, $ring_path); /*#############this i'm adding text default image########### ################################################################################*/ // link font file no server $fontname = '../fonts/roboto-regular.ttf'; // controls 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 file exists dont create again serve original if (!file_exists($rt_file)) { } // define base image lay our text on $im = imagecreatefromjpeg("../ringtones/ringtone-image.jpg"); // setup text colours $color['white'] = imagecolorallocate($im, 255, 255, 255); // defines starting height text block $height = 50; $y = imagesy($im) - $height; // loop through array , write text foreach ($ringtone_image $value){ // center text in our image - returns 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 line height next text block //$i = $i+32; } // create 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 script create image $filename = create_image($ringtone_image); /*#############end text 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 problem uploading 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 big!</div> <?php } } else { ?> <div class="add-errors">your have uploaded forbidden extension!</div> <?php } } } ?> <section> <h1>audio upload</h1> <p>use form below 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 field title"> <label for="category_id">choose 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>
try this:
$rt_file = null; // here we're initiate variable null avoid undefined var problem function create_image($ringtone_image) { global $rt_file; // here we're accessing global variable use inside function // rest part }
Comments
Post a Comment