Wednesday, 25 November 2015

PHP File Upload

Hi All,
If you want to upload one image using PHP, here is the smallest code. This consists of two files.
a) One file for selection of upload image and

b) other is the php code to upload the file

a) Upload image file

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

b) PHP code to upload the file to your own folder in the  name 'hup.jpg' [No conditions checked]

<?php
$target_dir = "uploads/";
$target_file=$target_dir .'hup.jpg';
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file) or die('Error'.mysql_error());
header('location:home.php');
?>

1 comment: