Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
A file <input> is not exactly new. Slap a <form> around it and submit it. (The form needs to be submitted via HTTP POST and must have enctype="multipart/form-data".) –  Carsten Jun 12 '13 at 11:23
    
In HTML5, you don't have to close the <input> tag, so you can remove the final backslash. (This also goes for <img>, <hr>, <br> and the likes.) –  Marijke Luttekes Jun 12 '13 at 11:24

2 Answers 2

up vote 0 down vote accepted

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)

share|improve this answer
    
All I need is in that link, thanks. –  Imri Persiado Jun 12 '13 at 11:36
    
You're quite welcome :) –  Imane Fateh Jun 12 '13 at 13:31

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"];
  }
?> 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.