Question

I need to implement an OS file chooser with php but I don't know how to go about doing this. Can somebody guide me in the right direction?

Was it helpful?

Solution

This is a basic file choosing form.

<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>

This is the basic php to handle the form.

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

You can read more here.

http://www.w3schools.com/php/php_file_upload.asp

OTHER TIPS

If you're using php to drive a desktop application, use the method of your UI toolkit, such as GTKFileChooser.

If you're using php to serve webpages, note that the server can only send HTML and JavaScript to the client, and not natively open webpages. Use an <input> element in your HTML code:

<form enctype="multipart/form-data" action="your-script.php" method="POST">
<input type="file" name="file" />
<input type="submit" />
</form>

You can submit the form automatically from JavaScript with the submit method. If you place the whole form in an iframe, you can do so in the background.

For details on the input field, consult the HTML5 standard. For information on how to handle the file once the form is submitted, have a look at the php manual.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top