2014-11-06 101 views
0

我想添加一個圖片上傳字段到我的PHP聯繫表格。我有contact.html其中發佈到contact.php。我將所有數據寫入errors.txt文件。用PHP上傳圖片到.txt文件

我希望能夠將圖像上傳到服務器上的「上傳」文件夾,同時將其註冊到errors.txt文件中。

我已標記上傳的部分與工作評論。請指導我。

contact.html

<html> 
<head> 
</head> 

<body> 
<form name="form1" method="post" enctype="multipart/form-data" action="contact.php"> 

<h1>Basic Info</h1> 

Username: <input type="text" name="user">  
<br>Email: <input type="text" name="mail"> 
<option value="intermediate">Intermediate</option> <option value="advanced">Advanced</optio></select> 

    <!-- Image Upload Code - NOT WORKING --> 
    Select image to upload: 
    <input type="file" name="fileToUpload" id="fileToUpload"> 

<br> <input type="submit" name="submit" value="submit"> 

</form> 
</body> 

</html> 

contact.php

<?php 
if(isset($_POST['submit'])) 
{ 
    $username = $_POST['user']; 

    $email = $_POST['mail']; 

    $experience = $_POST['exp']; 

    // IMAGE UPLOAD CODE - NOT WORKING 

    $target_file = "uploads/" . basename ($_FILES["fileToUpload"]["name"]); 

    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); 


    //the data 

    $data = "$username | $email | $experience | $target_file\n"; 


    //open the file and choose the mode 

    $fh = fopen("errors.txt", "a"); 

    fwrite($fh, $data); 


    //close the file 

    fclose($fh); 


    print "User Submitted"; 
} 
?> 
+2

'action =「signup.php」'好像是提交到另一個腳本。 – 2014-11-06 16:37:10

+0

好趕時間timothy – 2014-11-06 16:41:19

+0

@TimothyHa - 這是我在發佈時在這裏發佈的類型。原來的問題仍然存在。 – toiteam 2014-11-06 16:41:41

回答

1

也許你的HTML格式不正確!缺失打開<select>標記?

這會導致您的輸入type="file"被瀏覽器忽略。 試試這個:

Username: <input type="text" name="user">  
 
<br>Email: <input type="text" name="mail"> 
 
<select name=exp> 
 
<option value="intermediate">Intermediate</option> 
 
<option value="advanced">Advanced</option> 
 
</select> 
 
Select image to upload: 
 
<input type="file" name="fileToUpload" id="fileToUpload"> 
 
<br> <input type="submit" name="submit" value="submit">

+0

謝謝,但它沒有解決問題 - 被瀏覽器忽略。 – toiteam 2014-11-06 16:54:18

+0

再試一次。它給出了錯誤。 – toiteam 2014-11-06 16:59:21

+0

你把代碼放在你的'

'標籤之間嗎? – Arsonik 2014-11-06 17:00:31

1

你上傳的文件夾名稱是上傳或上傳??。在你的查詢中,你說「上傳」文件夾。

 $target_file = "uploads/" . basename ($_FILES["fileToUpload"]["name"]); 
+0

它是上傳,@Indra。謝謝你的幫助...我在聽。 – toiteam 2014-11-06 17:09:28

+0

仍然收到錯誤? – 2014-11-06 17:10:07