用PHP上传图片到.txt文件


Uploading Image with PHP to .txt File

我想在我的PHP联系人表单中添加一个图像上传字段。我有contact.html,它发布到contact.PHP。我将所有数据写入errors.txt文件。

我希望能够上传图像并将其移动到服务器上的"上传"文件夹,同时在errors.txt文件中注册。

我已经在上传的部分标记了NOT WORKING评论。请引导我。

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";
}
?>

也许您的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"> 

您的Upload文件夹名称是Upload还是uploads??。在您的查询中,您说"上传"文件夹。

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