Imri Persiado
Imri Persiado

Reputation: 47

Uploading files to the server using HTML5

I would like to know how can I upload a file after selecting it using HTML5 in that way:

<input type="file" .... />

The line above gives you the option to SELECT a file, now I need to upload it to the server. Since I don't know the answer I'll tag both php and Javascript.

Upvotes: 0

Views: 3183

Answers (2)

Sumit Bijvani
Sumit Bijvani

Reputation: 8179

Here is the code of Upload file in PHP

index.php

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

upload_file.php

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

Upvotes: 1

Imane Fateh
Imane Fateh

Reputation: 2406

You need an upload script like this one :

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

Check this link (http://www.w3schools.com/php/php_file_upload.asp)

Upvotes: 0

Related Questions