我在我的代码主体和函数之间共享变量值时遇到问题。
对于此示例,我想将图像上传到数据库,其值与正在使用的地方的 ID 匹配。
我目前从使用的 URL 中获取正在使用的位置的 ID,$_REQUEST
并尝试将其写入变量“mid”,然后我将其发送到createthumnail
函数(包含在functions2.php中),但“mid”值不会传输由于某些原因。
要重新创建:
- 访问:http: //www.students.bl.rdi.co.uk/stu26984/index.php
- 登录方式: 用户名:Test 密码:test123
- 去我的地方
- 点击 WalkaboutSF 链接
- 尝试通过“选择文件”按钮上传文件
以下是functions2.php的全文
<?php
function createThumbnail2($filename, $mid) {
include 'base.php';
require 'config.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $filename);
// $cookieUserx=$_SESSION['Username'];
// $checkCustidx=mysql_query("SELECT custid AS id from customers WHERE custUsername='".$cookieUserx."';");
// $rx=mysql_fetch_array($checkCustidx);
// $custidx=$rx['id'];
// $updateimage = mysql_query("UPDATE photos SET name = '".$filename."';")or die ('SQL Error: ' . mysql_error());
$updateimage = mysql_query("INSERT INTO photos (NAME, markerid) VALUES('".$filename."', '".$mid."');")or die ('SQL Error: ' . mysql_error());
if($updateimage)
{
header("Location: /WalkaboutSF/viewplace.php?place_id=%20".$mid."");
}
else
{
echo "<h1>Error</h1>";
}
$tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
echo $tn;
}
?>
这里也是调用它的代码,来自:
http://www.students.bl.rdi.co.uk/stu26984/viewplace.php?place_id=%20133
<div id="insertphoto">
<?php
require 'config.php';
require 'functions2.php';
$place_id = $_REQUEST['place_id'];
$view_place2 = mysql_query("SELECT * FROM markers WHERE id = '$place_id'");
$place2 = mysql_fetch_array($view_place2);
$mid= $place2['id'];
echo $mid;
if(isset($_FILES['fupload'])) {
if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {
$filename = $_FILES['fupload']['name'];
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
createThumbnail2($filename, $mid);
}
}
?>
<h2>Add Photo</h2>
<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
<input type="file" name="fupload" />
<input type="submit" value="Go!" />
</form>
</div>