2012-12-21 36 views
1

我已經創建了一個簡單的PHP消息傳遞系統,供用戶互相發送和接收消息。我想嘗試做的事情是添加一個可選的圖像輸入字段,以便用戶可以選擇圖像並將其作爲消息的一部分發送出去?發送一個圖像(可選)與消息主題和正文?

該照片需要存儲在我的服務器上的目錄文件夾中,並且圖像名稱需要與其餘消息字段(即content,subject,image_name)一起存儲在mysql表中。

有人可以告訴我怎樣才能調整我的代碼上傳圖片的形式嗎?

<?php ob_start(); ?> 
    <?php 

// CONNECT TO THE DATABASE 
    require('includes/_config/connection.php'); 
// LOAD FUNCTIONS 
    require('includes/functions.php'); 
// GET IP ADDRESS 
    $ip_address = $_SERVER['REMOTE_ADDR']; 


?> 


    <?php require_once("includes/sessionframe.php"); 
?> 


    <?php 


    confirm_logged_in(); 




    if (isset ($_GET['to'])) { 
    $user_to_id = $_GET['to']; 

} 


?> 
    <?php 
//We check if the form has been sent 
if(isset($_POST['subject'], $_POST['message_content'])) 
{ 
    $subject = $_POST['subject']; 
    $content = $_POST['message_content']; 
    $image = $POST ['image']; 

     //We remove slashes depending on the configuration 
     if(get_magic_quotes_gpc()) 
     { 
       $subject = stripslashes($subject); 
       $content = stripslashes($content); 
       $image = stripslashes($image); 

     } 


     //We check if all the fields are filled 
     if($_POST['subject']!='' and $_POST['message_content']!='') 
     { 
      $sql = "INSERT INTO ptb_messages (id, from_user_id, to_user_id, subject, content, image) VALUES (NULL, '".$_SESSION['user_id']."', '".$user_to_id."', '".$subject."', '".$content."', '".$image."');"; 
      mysql_query($sql, $connection); 

      echo "<div class=\"infobox2\">The message has successfully been sent.</div>"; 
     } 
} 


if(!isset($_POST['subject'], $_POST['message_content'])) 

if (empty($_POST['subject'])){ 
     $errors[] = 'The subject cannot be empty.'; 

    if (empty($_POST['body'])){ 
     $errors[] = 'The body cannot be empty.'; 

    } 
    } 

{ 
?> 


<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> 
    <div class="subject"> 
    <input name="subject" type="text" id="subject" placeholder="Subject"> 

    <input type="file" name="image" id="image"> 

    <textarea name="message_content" id="message_content" cols="50" placeholder="Message" rows="8" style="resize:none; height: 100px;"></textarea> 

    <input type="image" src="assets/img/icons/loginarrow1.png" name="send_button" id="send_button" value="Send"> 

</form> 

<?php } ?> 

<?php ob_end_flush() ?> 
+1

作爲一個提示,您必須將以下屬性添加到您的表單:enctype =「multipart/form-data」 – SaidbakR

回答

-1

我只是做了谷歌一些挖掘,發現這個代碼。我認爲這應該做你想做的事,但當然你必須適應你的實例。

<?php 
    $allowedExts = array("jpg", "jpeg", "gif", "png"); 
    $extension = end(explode(".", $_FILES["file"]["name"])); 
    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/png") 
    || ($_FILES["file"]["type"] == "image/pjpeg")) 
     && ($_FILES["file"]["size"] < 20000) 
     && in_array($extension, $allowedExts)){ 
      if ($_FILES["file"]["error"] > 0){ 
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
      } else{ 
       echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
       echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
       echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
       echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 
      if (file_exists("upload/" . $_FILES["file"]["name"])){ 
      echo $_FILES["file"]["name"] . " already exists. "; 
      } else{ 
       move_uploaded_file($_FILES["file"]["tmp_name"], 
       "upload/" . $_FILES["file"]["name"]); 
       echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
       } 
      } 
    } else { 
     echo "Invalid file"; 
} 
?>