使用PHP上传图像到.txt文件

时间:2014-11-06 16:35:06

标签: php html forms

我想在我的PHP联系表单中添加一个图片上传字段。我的 contact.html 会发布到 contact.php 。我将所有数据写入 errors.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 个答案:

答案 0 :(得分: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"> 

答案 1 :(得分:1)

您的上传文件夹名称是上传还是上传?在您的查询中,您说“上传”文件夹。

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