How to add a simple image upload to a form?

I have an html form. I want to add a simple image upload function to it, and it will send the image to a php page called "next.php". Any suggestions on how to do this?

+3
source share
2 answers

Create an HTML form, for example:

<html>
<body>
<form action="next.php" method="post" enctype="multipart/form-data">
<label for="image">Image:</label>
<input type="image" name="image" id="image" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Your file "next.php" can handle an image like this:

<?php

if ($_FILES["file"]["error"] > 0)
    echo "Error: " . $_FILES["image"]["error"] . "<br />";
else
{
    echo "Upload: " . $_FILES["image"]["name"] . "<br />";
    echo "Type: " . $_FILES["image"]["type"] . "<br />";
    echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["image"]["tmp_name"];
}

, jpg, gif, png . 2 3 (, , ) . ImageMagick , filesystem .

+3

- input , / ( "next.php" script), :

<form action="path/to/next.php" method="post" enctype="multipart/form-data">
    <fieldset>
        <input type="file" name="picture" id="picture" />
    </fieldset>
</form>
+1

Source: https://habr.com/ru/post/1779844/


All Articles