我無法在我的主體代碼和函數之間共享變量值。無法分享PHP變量
對於這個例子,我想上傳一個圖像到數據庫,其值與正在使用的地方的ID相匹配。
我現在搶使用$_REQUEST
的URL使用該場所的ID,並在嘗試寫一個變量「中」,然後我發到createthumnail
功能(包含在functions2.php),但'中'由於某種原因,價值不會轉移。
要重新創建:
- 轉到:http://www.students.bl.rdi.co.uk/stu26984/index.php
- 登錄用:用戶名:測試密碼: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>
你嘗試在你的函數中打印'$ mid'嗎?它裏面有什麼? – Cfreak 2013-03-24 22:45:16
您可能需要查看文件擴展名['pathinfo($ file,PATHINFO_EXTENSION)'](http://php.net/manual/en/function.pathinfo.php)。比使用'preg_match'清潔得多很多 – 2013-03-24 22:46:48
我在這個頁面上輸入了一個回顯,值是'3' http://www.students.bl.rdi.co.uk/stu26984/viewplace.php?place_id=% 203 – 2013-03-24 22:48:17