gpt4 book ai didi

php - undefined variable 不允许我将文件插入数据库表列

转载 作者:行者123 更新时间:2023-11-29 12:55:25 26 4
gpt4 key购买 nike

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

接下来,我想为每个音频文件创建一个默认图像,使用 PHP 将音频文件标题添加到图像中,然后在文件名上附加一个唯一的 id。我已经成功地做到了这一点。我是通过following this tutorial.了解到这一点的

示例图片:

enter image description here

我遇到的问题是在上传时将音频文件图像插入到数据库列中。这是数据库图像。我需要将新创建的音频图像的文件名放入 rt_file 列中。这样我就可以稍后再重复它。

enter image description here

我希望就我出错的地方获得一些帮助。

我得到的当前错误是: undefined variable :rt_file。我不明白为什么...?这不是像我在插入语句上面定义的那样定义它吗:

$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>

最佳答案

试试这个:

$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
}

关于php - undefined variable 不允许我将文件插入数据库表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24129797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com