开发者

How to ask input file browser from user in Drupal 7

Hi i need some help with it. I have created a php script which i am running from copy paste into node as php code. I need a file path for a file to process like i am using '\birds\birds.xml'. I dont want to include the 开发者_如何学Cfile path instead i am thinking, How to create a simple small form with browse button and selecting a file than submit with submit button. Save the filepath in the variable.


As far as I understood the browse button is for client side (web-browser)


Client Side Browse Button

There are 2 parts to it:

  1. HTML Upload Form
  2. PHP processing script

HTML [form]

<form enctype="multipart/form-data" action="submit-script.php" method="POST">
    Choose a file to upload: <input name="uploadedfile" type="file" />
    <input type="submit" value="Upload File" />
</form>

PHP [submit-script.php]

$fileName = $_FILES['uploadedfile']['name'];
$target_path = "uploads/";
$target_path = $target_path . basename($fileName); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " uploaded";
} else{
    echo "Unable to upload!";
}

Server Side Browse Button

However if you want to click the browse button and browse the server you will have to implement the browse button yourself with ajax or make the process multi-step.

Ajax-Way

  1. On button click event open up Modal Window with directory browsing
  2. There are many open source Directory browsing scripts on the net. Here are few: simple server file browser, fileNice - fancier solution...
  3. Once you click OK in the modal window, before closing the window set some hidden input on the parent to the selected file (using javascript). E.g. window.parent.selectedFile.value ="selected file"
  4. Once you save in your main form. Capture the "selectedFile" from POST and do what you need to do.

Non-Ajax Way

You will need multiple forms for this (on different pages). 1st form will have the file browser (see step 2 in Ajax-Way), store selected file to hidden fields. Once passed to 2nd form, set the value to another hidden fields... request additional information (if needed, if not you can skip 2nd form). Once submitted, do what you need to do...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜