dtqysxw4659 2014-10-16 04:15
浏览 52

将图像文件保存在数据库和图像文件夹中

i'm trying to save image in database and images folder in same time, the image it's saving in images folder but not in database can someone please help i don't know what im doing wrong.

add.php

<?php
if (!defined('WEB_ROOT')) {
    exit;
}




$errorMessage = (isset($_GET['error']) && $_GET['error'] != '') ? $_GET['error'] : '&nbsp;';
?> 
<script>
function getXMLHTTP() { //fuction to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }



    function getCategory(strURL) {      

        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('catdiv').innerHTML=req.responseText;                       
                    } else {
                        alert("There was a problem while using XMLHTTP:
" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }

    }
</script>
<div class="content-box-header">
              <h3>Add New Restaurant</h3>
</div>              
<form action="processTopping.php?action=add" method="post" enctype="multipart/form-data" name="frmAddTopping" id="frmAddTopping">
 <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">


  <tr>
    <td width="150" class="label">Restaurant  Name</td>
    <td class="content"><input name="txtName" class="text-input large-input" type="text" id="txtName" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Email</td>
    <td class="content"><input name="txtEmail" class="text-input large-input" type="text" id="txtEmail" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Phone</td>
    <td class="content"><input name="txtPhone" class="text-input large-input" type="text" id="txtPhone" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Address</td>
    <td class="content"><input name="txtAddress" class="text-input large-input" type="text" id="txtAddress" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Cuisine</td>
    <td class="content"><input name="txtCuisine" class="text-input large-input" type="text" id="txtCuisine" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Mini Order</td>
    <td class="content"><input name="txtMini_order" class="text-input large-input" type="text" id="txtMini_order" /></td>
  </tr>
<tr>
    <td class="label">Restaurant  Suburbs</td>
    <td class="content"><input name="txtSuburbs" class="text-input large-input" type="text" id="txtSuburbs" /></td>
  </tr>
  <tr>
    <td width="150" class="label">Restaurant  Menu</td>
    <td class="content"><input name="txtMenu" class="text-input large-input" type="text" id="txtMenu" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Reviews</td>
    <td class="content"><input name="txtReviews" class="text-input large-input" type="text" id="txtReviews" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Logo</td>
    <td class="content"><input name="txtLogo" class="text-input large-input" type="text" id="txtLogo" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Takeaway Hours</td>
    <td class="content"><input name="txtTakeawayHours" class="text-input large-input" type="text" id="txtTakeawayHours" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Delivery Hours</td>
    <td class="content"><input name="txtDeliveryHours" class="text-input large-input" type="text" id="txtDeliveryHours" /></td>
  </tr>
  <tr>
    <td class="label">Restaurant  Map</td>
    <td class="content"><input name="txtMap" class="text-input large-input" type="text" id="txtMap" /></td>
  </tr>
<tr>
    <td class="label">Restaurant  Promotions</td>
    <td class="content"><input name="txtPromotions" class="text-input large-input" type="text" id="txtPromotions" /></td>
  </tr>

 <!-- <tr> 
   <td width="150" class="label">Image</td>
   <td class="content"> <input name="fleImage" type="file" id="fleImage" class="box">    </td>
  </tr>-->
  <tr>
    <td class="label" valign="middle">Images</td>
    <td  class="label">
        <div id="main">
          <table width="230" cellpadding="0">
            <tr>
              <td width="232" align="right">

              <input type="file" name="fleImage[]" /></td>
            </tr>
          </table>
        </div></td>
  </tr>

 </table>
 <p align="center"> 
  <input name="btnAddTopping" type="button" id="btnAddTopping" value="Add Restaurant" onClick="checkAddToppingForm();" class="button">
  &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php';" class="button">  
 </p>
</form>

processTopping.php

<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
error_reporting(E_ALL);
checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {

    case 'add' :
        addTopping();
        break;

    case 'modify' :
        modifyTopping();
        break;

    case 'delete' :
        deleteTopping();
        break;


    default :
        // if action is not defined or unknown
        // move to main Topping page
        header('Location: index.php');
}



function addTopping()
{
        $name = $_POST['txtName'];
        $email = $_POST['txtEmail'];
        $phone = $_POST['txtPhone'];
        $address = $_POST['txtAddress'];
        $cuisine = $_POST['txtCuisine'];
        $mini_order = $_POST['txtMini_order'];
                $suburbs = $_POST['txtSuburbs'];
        $menu = $_POST['txtMenu'];
        $reviews = $_POST['txtReviews'];
        $logo = $_POST['txtLogo'];
        $takeaway_hours = $_POST['txtTakeawayHours'];
        $delivery_hours = $_POST['txtDeliveryHours'];
        $map = $_POST['txtMap'];
                $promotions = $_POST['txtPromotions'];

        $sql   = "INSERT INTO restaurant_info(name, email, phone, address, cuisine, mini_order, suburbs, menu, reviews, logo, takeaway_hours, delivery_hours, map, promotions)
        VALUES ('$name','$email','$phone','$address','$cuisine','$mini_order','$suburbs','$menu','$reviews','$logo','$takeaway_hours','$delivery_hours','$map','$promotions')";     
        dbQuery($sql);
        header('Location: index.php');  

}

    //image
    $uploadDir = '../../images/';
    for($i=0;$i<count($_FILES["fleImage"]["name"]);$i++)
    {
    if($_FILES["fleImage"]["tmp_name"][$i] != '')
    {
        $image = $_FILES["fleImage"];
        if (trim($image['tmp_name'][$i]) != '') {
        $ext = substr(strrchr($image['name'][$i], "."), 1); //$extensions[$image['type']];

        // generate a random new file name to avoid name conflict
        $imagePath = md5(rand() * time()) . ".$ext";

        list($width, $height, $type, $attr) = getimagesize($image['tmp_name'][$i]); 

        // make sure the image width does not exceed the
        // maximum allowed width
        if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
            $result    = createThumbnail($image['tmp_name'][$i], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
            $imagePath = $result;
        } else {
            $result = move_uploaded_file($image['tmp_name'][$i], $uploadDir . $imagePath);
        }   

        if ($result) {
            // create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";
            $result = createFixThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH,THUMBNAIL_HEIGHT);

            // create thumbnail failed, delete the image
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }   
        } else {
            // the product cannot be upload / resized
            $imagePath = $thumbnailPath = '';
        }


    }

        mysql_query("INSERT INTO restaurant_info(Rest_Logo)
        VALUES ('$Rest_Logo')"; 
    }   
    }




/*
    Modify a Topping
*/
function modifyTopping()
{
    $toppingId   = (int)$_POST['hidToppingId']; 
    $name = $_POST['txtName'];
    $email = $_POST['txtEmail'];
    $phone = $_POST['txtPhone'];
    $address = $_POST['txtAddress'];
    $cuisine = $_POST['txtCuisine'];
    $mini_order = $_POST['txtMini_order'];
        $suburbs = $_POST['txtSuburbs'];
    $menu = $_POST['txtMenu'];
    $reviews = $_POST['txtReviews'];
    $logo = $_POST['txtLogo'];
    $takeaway_hours = $_POST['txtTakeawayHours'];
    $delivery_hours = $_POST['txtDeliveryHours'];
    $map = $_POST['txtMap'];
        $promotions = $_POST['txtPromotions'];
    $sql   = "UPDATE restaurant_info 
              SET  name='$name',  email='$email',  phone='$phone',  address='$address',  cuisine='$cuisine',  mini_order='$mini_order',  suburbs='$suburbs', menu='$menu',  reviews='$reviews',  logo='$logo',  takeaway_hours='$takeaway_hours',  delivery_hours='$delivery_hours',  map='$map',  promotions='$promotions'
              WHERE rest_id = $toppingId";
    dbQuery($sql);
    header('Location: index.php');  

}

/*
    Remove a topping
*/
function deleteTopping()
{
    if (isset($_GET['toppingId']) && (int)$_GET['toppingId'] > 0) {
        $toppingId = (int)$_GET['toppingId'];
    } else {
        header('Location: index.php');
    }


    $sql = "DELETE FROM restaurant_info 
            WHERE rest_id = $toppingId";
    dbQuery($sql);

    header('Location: index.php');
}
?>
  • 写回答

2条回答 默认 最新

  • douyuanliao8815 2014-10-16 07:22
    关注

    To save the file in database, you need to fetch content of file & save that in DB. You should choose datatype as longblob.

    This can help you: http://forums.mysql.com/read.php?20,17671,27914

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计