I am working on a PHP project, in which I need to store a path of an image when user select an image from open dialog box from a specified directory. How can I do this? I don't know how to open the Open/Browse dialog box and how to get that path in PHP/javascript. And I want that my other form data don't flush when I open the Open/Browse Dialog.(I want to put image file's path that user has selected in my database, so I can reduce my database size.)
I am working on a PHP project, in which I need to store a path of an image when user select an image from open dialog box from a specified directory. How can I do this? I don't know how to open the Open/Browse dialog box and how to get that path in PHP/javascript. And I want that my other form data don't flush when I open the Open/Browse Dialog.(I want to put image file's path that user has selected in my database, so I can reduce my database size.)
Share Improve this question edited Dec 28, 2012 at 16:44 Dhwani asked Dec 28, 2012 at 14:38 DhwaniDhwani 7,62618 gold badges81 silver badges143 bronze badges 5 |4 Answers
Reset to default 4You can use file uploading forms with html and send the form to your PHP file to handle the file contents. When a file is sent to the server it is stored in a temporary location.
W3Schools has a good tutorial on this, the HTML becomes:
<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>
and the 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"];
}
?>
http://www.w3schools.com/php/php_file_upload.asp
You can put a form element by using <input type="file">
If you only want the path without uploading the file. You can use javascript.
If you post the data to the server file's info will be available to PHP but also the file will be sent to server as well.
Check the Javascript File Api examples here if you want more .. http://www.html5rocks.com/en/tutorials/file/dndfiles/
<input type="file">
no? or i'm something missing?
For dotNetAddict (if they are still interested) and any others similarly interested, try the following link for a good explanation of how to obtain the path to a file....
http://www.w3schools.com/jsref/prop_fileupload_value.asp
c:\dhwani\filename.jpg
, you will not get this path anyway. – Pankit Kapadia Commented Dec 28, 2012 at 15:29userid
s, so that you will not need storing path in database as you can show images with the same. Its easy, dont make it complicated. – Pankit Kapadia Commented Dec 29, 2012 at 17:00