2016-08-23 46 views
2

我想添加文件上傳到我當前的代碼。我有很好的形式,不希望改變它。我一直在搜索谷歌和YouTube,無法找到我可以添加到我的當前代碼的代碼。當我所得到的錯誤是錯誤時,這是​​如此令人沮喪。與文件上傳聯繫表格

<?php 

if ($_POST['submit']) { 

    if (!$_POST['username']) { 
    $error = "<br>- Please enter your name! \r\n"; 
    } 

    if (!$_POST['email']) { 
    $error .= "<br>- Please enter your email! \r\n"; 
    } 

    if (!$_POST['message']) { 
    $error .= "<br>- Please enter your message! \r\n"; 
    } 

    if ($error) { 
    $result = "<div class='alert alert-danger' role='alert'>Whoops, there is an error. Please correct the following: $error</div>"; 
    } else { 
    mail("[email protected]", "Message from contact form", "From: ".$_POST['username']." 

     Reply to: ".$_POST['email']." 

     Message: ".$_POST['message']); 
    { 
     $result = "<div class='alert alert-success text-center' role='alert'> 
     <p>Thank you for your message. We will contact you socket_get_option(socket, level, optname)</p> 
     </div>"; 
    } 
    } 

} 

?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
     <title>Title</title> 

     <!-- Bootstrap --> 
     <link rel="stylesheet" href="css/bootstrap.min.css"> 
     <link rel="stylesheet" href="css/style.css"> 

     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
     <!--[if lt IE 9]> 
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
     <![endif]--> 
    </head> 
<body> 

    <section> 
     <div class="container"> 
      <div class="row"> 
       <div class="col-xs-12 col-sm-4 col-sm-offset-4"> 
       <h1 class="text-center">Contact us</h1> 
         <?php echo $result;?> 
        <form action="material.php" method="post" role="form"> 

         <div class="form-group input-group input-group-lg"> 
          <span class="input-group-addon"> 
           <span class="glyphicon glyphicon-user"></span> 
          </span> 
          <input type="text" name="username" class="form-control"> 
         </div> 

         <div class="form-group input-group input-group-lg"> 
          <span class="input-group-addon"> 
           <span class="glyphicon glyphicon-envelope"></span> 
          </span> 
          <input type="email" name="email" class="form-control"> 
         </div> 

         <div class="form-group"> 
          <textarea name="message" id="message" rows="5" placeholder="Message box"></textarea> 
         </div> 

         <p>Send us your photo</p> 

         <div> 
          <input type="file" name="file" enctype="multipart/form-data"> 
         </div> 

         <div class="form-group input-group btn-margin"> 
          <span class="input-group-addon"> 
           <span class="glyphicon glyphicon-send"></span> 
          </span> 
          <input type="submit" name="submit" class="form-control btn btn-primary" value="Submit request"> 
         </div> 


        </form> 
       </div> 
      </div> 
     </div> 
    </section> 

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 
</body> 
</html> 
+0

那麼,就是你正在努力處理是給你「在錯誤的錯誤」的文件上傳的代碼?我沒有看到那裏試圖處理文件的東西。 –

回答

1

您的代碼根本沒有處理文件上傳。有很多關於如何處理在PHP中保存文件上傳的資源。也許如果你更新你的代碼來嘗試處理文件上傳,然後說出你得到某人的錯誤類型可以幫助你。取決於各種事情,您可能會得到任何數量的錯誤。

您是否具有讀取/寫入權限以嘗試將文件保存到的目錄?

您是否知道php.ini「upload_max_filesize」設置,並且您的文件比最大上傳小?

這裏是一個有用的網站,解釋的基礎知識:https://davidwalsh.name/basic-file-uploading-php

0

你已經有前端完成,與

<form action = "material.php"> 

    <input type="file" name="file" enctype="multipart/form-data"> 

您所提交的文件名,會自動從$ _FILES [」文件'] ['tmp_name']數組,因爲輸入字段的名稱是「file」,並且tmp_name是PHP自己添加的默認項。

因此,在您的後端添加以下內容以捕獲文件上傳。

The move_uploaded_file是什麼技巧!

<?php 
    $upload_dir = "/var/www/misyte.com/uploads/"; 
    $filename = "uploaded_file.txt"; // Rename it to whatever file format.. 
    $destination = $upload_dir . "/" . $filename; 

    if (move_uploaded_file($_FILES['file']['tmp_name'], $destination)) 
    { 
     // If this here succeeds, the file has been moved to destination folder 
    } 
?> 

作爲獎勵,我包括我寫了一個功能,這將有助於你在拆分的分機上收到的文件名「」點和你上傳的文件重命名你的心臟的慾望:

<?php 
     function ExplodeFileAtExtention($Filename) 
     { 
      $len = strlen($Filename); 
      for ($i = 0; $i < $len; $i++) 
      { 
       $curidx = $len-($i+1); 
       if ($Filename[$curidx] == '.') 
       { 
        return array(substr($Filename, 0, $curidx), substr($Filename, $curidx + 1)); 
       } 
      } 
     } 
    ?> 

它存在的唯一理由,就是有時文件名包含超過100個字符,如「some.file.name.txt」,如果你只需將它分成第一個點字符,它仍然不會返回擴展名。

因此,舉例來說:

$split = ExplodeFileAtExtention("some.file.name.txt"); 

$filename = $split[0]; // "some.file.name" 
$ext  = $split[1]; // "txt" 

這樣,您就可以重構(重命名)上載的文件,並保持擴展名,例如:

$filename= time() . "." . $ext; // "1471979296.txt" 

如果你把它插入到第一代碼sniplet,腳本將上傳並存儲文件:

/var/www/misyte.com/uploads/1471979296.txt 

這是很好的,因爲你可能想某種模式對所有上傳的文件,而不是將它們存儲爲

"This Awesome Window95~ filename___I__got__from my Program Files folder!!!.txt"