我创建了一个表单,上传到MySQL表中的文本数据和图像'路径。图像被上传并存储在服务器的文件夹中。它工作得很完美,直到我使用php会话。我在代码开头使用PHP session_start(通过用户名和密码访问),然后它不会将数据导入数据库。它显示错误"致命错误:调用未定义函数resizeImage()"在第84行。
任何建议找出为什么函数resizeImage()在我添加session_start时不起作用?
档案: eis-post2.php
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Upload form</title>
</head>
<body>
<form name='newad' method='post' enctype='multipart/form-data' action=".$_SERVER['PHP_SELF'].">
<table>
<tr><td>title:</td><td><input type='text' name='title'></td></tr>
<tr><td>date</td><td><input type='date' name='date'></td></tr>
<tr><td>descriptionΠεριγραφή:</td><td><input type='text' name='desc'></td></tr>
<tr><td>sourse</td><td><input type='text' name='author'></td></tr>
<tr><td>Main text</td><td><textarea name='maintext' id='maintext' required=''></textarea></td></tr>
<tr><td>Image</td><td><input type='file' name='image'></td></tr>
<tr><td> </td><td><input name='Submit' type='submit' value='Upload image'></td> </tr>
</table>
</form>
</body>
</html>";
define ("MAX_SIZE","500");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
$image=uniqid() . '.jpg';
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*2048)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
else {
$temp=resizeImage($_FILES['image']['tmp_name'],270,230);
$imgfile="images/small/".$image;
imagejpeg ( $temp, $imgfile );
$temp2=resizeImage($_FILES['image']['tmp_name'],480,330);
$imgfile2="images/big/".$image;
imagejpeg ( $temp2, $imgfile2 );
}
}
}
else
{
echo "<h1>Select Image File</h1>";
$errors=1;
}
}
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully! Try again!</h1>";
include('db.php');
mysql_query("SET NAMES 'utf8'");
$title=$_POST['title'];
$date=$_POST['date'];
$desc=$_POST['desc'];
$author=$_POST['author'];
$maintext=$_POST['maintext'];
$query="insert into post( small_img, big_img, title, date, description, post_text, post_author)
values( '$imgfile', '$imgfile2', '$_POST[title]', '$_POST[date]', '$_POST[desc]','$_POST[author]', '$_POST[maintext]' )";
$result=mysql_query($query);
if ($result)
{
echo "Information stored successfully";
}
else
{
echo mysql_error();
}
}
function resizeImage($imgSrc,$thumbnail_width,$thumbnail_height) {
list($width_orig, $height_orig) = getimagesize($imgSrc);
$myImage = imagecreatefromjpeg($imgSrc);
$ratio_orig = $width_orig/$height_orig;
if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
imagedestroy($process);
imagedestroy($myImage);
return $thumb;
}
}
else {
die("you must be logged in!");
}
?>