[請益] 圖片上傳到資料庫有路徑
小弟 找到一篇教學
是使用DW的PHP方式來達成
可以把圖片上傳 也加說明文字
用途可以拿來做產品商品的展示說明
http://antlite.com/blog/2008/12/dreameaver-php-upload-images/
但是照著做後 還是失敗了
圖片會有一個上傳的等待過程
並沒有上傳到FTP的任何資料夾中
所有文字資料都會進到資料庫
但就是圖片路徑仍然是預設圖片路徑
以下貼上要新增資料那一頁的語法
有點長 請各位多多指教..感恩感恩
-----------------
<?php require_once('../Connections/newssql.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
if ($_POST['case_picurl']!=="120.jpg") {
$upload_file=$_FILES['upload']['tmp_name'];
$upload_file_name=$_FILES['upload']['name'];
if($upload_file){
$file_size_max = 2000*1000;// 1M限制最大容量(bytes)
$store_dir = "../upload";// 圖片儲存的位置
$accept_overwrite = 1;//是否允覆蓋相同文件
// 檢查文件大小
if ($upload_file_size > $file_size_max) {
echo "對不起,你的檔案容量大於規定";
exit;
}
// 檢查讀取文件
if (file_exists($store_dir . $upload_file_name) && !$accept_overwrite) {
Echo "存在相同名稱文件,請修改檔案名稱後再上傳。";
exit;
}
//複製文件到指定目錄
if (!move_uploaded_file($upload_file,$store_dir.$upload_file_name)) {
echo "複製文件失敗";
exit;
}
}
$Erroe=$_FILES['upload']['error'];
switch($Erroe) {
case 0:
break;
case 1:
Echo "上傳的檔案超過了 php.ini 中 upload_max_filesize 的值.";
break;
case 2:
Echo "上傳的檔案超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。";
break;
case 3:
Echo "文件只有部分被上傳";
break;
case 4:
Echo "沒有文件被上傳";
break;
}
}
$insertSQL = sprintf("INSERT INTO casecenter (case_type, case_date, case_subject, case_picurl, case_name, case_location, case_age, case_loss, case_occ, case_content, case_editor, case_ok) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['case_type'], "text"),
GetSQLValueString($_POST['case_date'], "date"),
GetSQLValueString($_POST['case_subject'], "text"),
GetSQLValueString($_POST['case_picurl'], "text"),
GetSQLValueString($_POST['case_name'], "text"),
GetSQLValueString($_POST['case_location'], "text"),
GetSQLValueString($_POST['case_age'], "int"),
GetSQLValueString($_POST['case_loss'], "int"),
GetSQLValueString($_POST['case_occ'], "text"),
GetSQLValueString($_POST['case_content'], "text"),
GetSQLValueString($_POST['case_editor'], "text"),
GetSQLValueString($_POST['case_ok'], "text"));
mysql_select_db($database_newssql, $newssql);
$Result1 = mysql_query($insertSQL, $newssql) or die(mysql_error());
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增產品</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" />
<link rel="stylesheet" type="text/css" href="../css/main.css" />
<script type="text/javascript" src="../js/main.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
<script src="" rel="nofollow">http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="" rel="nofollow">http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<script>
$(function() {
$( "#case_date" ).datepicker({dateFormat: 'yy-mm-dd'});
});
</script>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
</head>
<div id="admincontent">
<div id="detailcontent">
<div class="admintitle">新增案例分享</div>
<div class="contentbox">
<form method="post" name="form1" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">類別</td>
<td><select type="text" name="case_type" >
<option value="專案" selected="selected">專案</option>
<option value="治療">治療</option>
</select></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">日期</td>
<td><input type="text" name="case_date" value="<?php echo date("Y-m-d");?>" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">標題</td>
<td><input type="text" name="case_subject" value="" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">暱稱</td>
<td><input type="text" name="case_name" value="" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">地區</td>
<td><input type="text" name="case_location" value="" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">年齡</td>
<td><input type="text" name="case_age" value="" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">減重(公斤)</td>
<td><input type="text" name="case_loss" value="" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">職業</td>
<td><input type="text" name="case_occ" value="" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><div id="imgview"></div>插入縮圖</td>
<td><input name="upload" type="file" id="upload" size="50" onChange="checkandsetvalue();" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">編輯者</td>
<td><input type="text" name="case_editor" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">文章內容</td>
<td><textarea type="text" name="case_content" value="" cols="80" rows="15"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">審核內容</td>
<td><input name="case_ok" id="case_ok" type="radio" value="No" checked="checked" />
隱藏
<input type="radio" name="case_ok" id="case_ok2" value="Yes" />
顯示</td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input name="submit" type="submit" value="插入記錄" onClick="check();return document.checkValue;"></td>
</tr>
</table>
<input type="hidden" name="case_id" value="">
<input type="hidden" name="case_picurl" value="120.jpg" id="hiddenpost">
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</div>
</div>
<div class="clear"></div>
</div>
</body>
</html>
<?php
mysql_free_result($Reccase);
mysql_free_result($ReccaseSelect);
?>
<script type="text/javascript">
function checkandsetvalue(){
var x = document.getElementById("upload"); //取得 upload 的 Dom
var p = document.getElementById("hiddenpost");
var y = document.getElementById("imgview");
if(!x || !x.value){
y.src = "../upload/120.jpg";
p.value = "120.jpg";
return;
}
var patn = /\.jpg$|\.jpeg$|\.png$|\.gif$/i; //限制副檔名
if(patn.test(x.value)){
var filepath = x.value;
y.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = x.value;
if(filepath.lastIndexOf("\\")>0){
filepath = filepath.substring(filepath.lastIndexOf("\\")+1,filepath.length);
}
p.value = filepath; //將id 為hiddenpost 隱藏欄位的值替换為上傳文件。文件。
}else{
alert("請選擇.jpg.jpeg .png .gif格式檔案。");
}
}
function check(){
var error = '';
var x = document.getElementById("upload");
var p = document.getElementById("hiddenpost");
if(!x || !x.value){
p.value = "120.jpg";
return;
}
var patn = /\.jpg$|\.jpeg$|\.png$|\.gif$/i; //限制副檔名
if(!patn.test(x.value)){
error = "檔案格式錯誤!";
alert(error);
}
document.checkValue = (error == '');
}
</script>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.215.10
→
07/01 07:56, , 1F
07/01 07:56, 1F
→
07/01 23:18, , 2F
07/01 23:18, 2F
→
08/25 19:40, , 3F
08/25 19:40, 3F
→
08/25 19:40, , 4F
08/25 19:40, 4F
→
08/25 19:41, , 5F
08/25 19:41, 5F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章
4
18
6
16