How to ask input file browser from user in Drupal 7 - php

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 file 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:
HTML Upload Form
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
On button click event open up Modal Window with directory browsing
There are many open source Directory browsing scripts on the net. Here are few: simple server file browser, fileNice - fancier solution...
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"
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...

Related

how to upload multiple files with php to a 000webhost server?

I'm making a simple site using html and php, this site is suposed to let me upload several files at the same time (Selecting them at once in the upload window which opens when I click on a 'select' button), my page already let me upload one file at once, my code is:
HTML:
<form action="php/cargaxml.php" method="POST" role="form" class="uploadForm" enctype="multipart/form-data" >
<input type="file" name = "test" class = "file">
<input type="submit" value="Carga de archivos" id="boton" class = "btn btn-success btn-lg">
</form>
cargaxml.php:
<?php
$target_path = "/public_html/uploads/carga";
copy($_FILES['test']['tmp_name'], $target_path);
echo "File successfully uploaded!";
?>
When I want to upload a file I click on the button:
Upload button
and the browser shows a window to select a file:
Upload window
but when I use ctrl+click to select several files it just unselect the previous file and let me use only one file, other way I've thought is to select a folder which contains several files and upload them in a massive way, but when I select a folder and click on open it just open the folder and doesn't let me to 'upload' the folder.
How can I do to select several files to upload? My site is hosted on 000webhost.com.
I apologise for my English (Isn't so well), thanks in advance:)
In HTML5 you can set the multiple attribute on . This works in browsers supporting HTML5.
<input type="file" name="test" multiple=""/>
For more Information in upload via Ajax https://www.creativefan.com/10-ajax-jquery-file-uploaders/

$_FILES does not accept value [duplicate]

I have a form with text inputs and file inputs; the text fields are being validated. Is there a way to have the form remember which files the user has already selected if they hit submit but need to go back because one of the text fields didn't validate?
You can't "pre-fill" the contents of a file upload field for security reasons. Also, that would mean the file would get re-uploaded every time the form is submitted, which would not be good.
Instead, do this:
Create a file upload field with name file_upload.
On the server-side, process the upload in any case, even if the rest of the form validation fails.
If the form validation failed, but the file was uploaded, insert a hidden input into the form with name file containing the name of the just uploaded file.
Display a user-visible indication that the file is okay. If it's an image, display a thumbnail version of it. If it's any other file, display its filename and/or icon.
If the user chooses to upload a different file in the file_upload field, process the upload and store the new value in file.
Pseudocode:
<?php
$file = null;
if (!empty($_POST['file'])) {
$file = $_POST['file'];
}
if (!empty($_FILES['file_upload'])) {
// process upload, save file somewhere
$file = $nameOfSavedFile;
}
// validate form
?>
<input type="file" name="file_upload" />
<input type="hidden" name="file" value="<?php echo $file; ?>" />
<?php
if (!empty($file)) {
echo "File: $file";
}
?>
Important note
This mechanism can allow any user to claim other user's files as their own, by including a file name that they guessed exists on your server. You will want to ensure that uploaded files are clearly associated with a specific user to avoid this issue.
files input fields are read-only you can't set an initial value for them
You can upload the Files anyway and display the filenames instead of the file select box. To remember the fields, you can use a $_SESSION variable.

Using only PHP and HTML to act like JavaScript

I have an assignment for school, and I'm not sure how the teacher wants us to accomplish a task.
We need to get an uploaded file as a temp file only (index.php)
Output size of file (upload.php)
User can confirm save of file or not (upload.php)
So, I have the majority down, but my problem lies with creating the temp file into a permanent file.
index.php
<html>
<form action="http://mysite.org/~me/upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php
<?php
if (($_FILES["file"]["type"] == "application/vnd.ms-excel"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
}
?>
<form action="" method="POST">
<input type="submit" value"YES please save">
<form>
<?php
if (isset($_POST['submit']))
{
//Code for saving file
echo 'File saved!';
}
?>
Is it possible to go about it this way? My last echo statement does not work, so I'm doubtful the file save would be as well.
Hopefully the following comments can help you with the part you are stuck on.
In case you hadn't realized it already, any files uploaded with PHP are deleted once the PHP request that handled the uploaded file terminates. This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates.
One function of interest to you will be move_uploaded_file() which will move the temporary file from the upload to a permanent location of your choice.
Since the file will be uploaded and then you have to display the size and ask the user to confirm the upload, you will have to move the temp file to a permanent temporary location where it is kept when the user hasn't confirmed they want to keep the upload.
I'm not sure if you have been introduced to sessions yet, but if not, you will probably need some hidden form element that will keep track of what file they uploaded, otherwise you can keep this info in the session.
Then when the person submits the form saying they want to keep the file, you can move it again to a permanent location, or if they say no, then delete the file. The problem is, if they never say yes or no, then the file remains on the system.
Hope that helps.
Yep, this should work. Your if statements will catch the form submission and then echo your string there. A few little errors in your markup:
<input type="submit" value"YES please save">
Should be
<input type="submit" value="YES please save" name="submit">
Your final if statement in PHP is looking for a post variable named 'submit' but your <input type="submit"> tag has no name.
The file is saved to a temporary location when the upload completes. You can access this temporary file with $_FILES['file]['tmp_name'] BUT the file will be removed at the end of the request if you do nothing about it. This means that when the user clicks YES please save button, the file will not be available any more.
This means that you have to save the file to a disk in the first place, when you first call the upload.php file. There is no way to keep the file "in memory" while the user decides whether or not to save the file permanently.

Unset uploaded files in PHP

I have a Form that I am using to receive an uploaded .csv file, parse it and insert the data into my MySQL db on an Apache server. The page first checks to see if there is an uploaded file. If there is, it processes the data, if not the form (below) is displayed.
<form enctype="multipart/form-data" action="uploadfaculty.php" method="POST" id="UploadForm">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
My problem is that currently the user can simply F5 the browser over and over again and because the file is still on the server and in the $_FILES array, it processes it every time.
I've tried:
unlink($_FILES['uploadedfile']['tmp_name']),
unlink($_FILES['uploadedfile']),
unset($_FILES['uploadedfile']['tmp_name']), and
unset($_FILES['uploadedfile'])`
I've even reset the form via Javascript (which I knew would not work, but did it just to eliminate all doubt). All to no avail. I'm sure it's something simple I'm missing...it almost always is. Any thoughts?
It's not unsetting because the post action is stored on the browser's end and being re-uploaded (in a small amount of time as it's only a csv) when they hit F5. Which is essentially the same as them using the form to upload another csv.
You can do this:
if (isset($_POST['csv'])){
$DataProcessed = DataProcessingFunction();
}
if (isset($DataProcessed) && $DataProcessed){
header("Location: /path/to/form/page.php");
exit();
}
This will clear the post data sent in the earlier request. Refreshing will not resubmit the form.
You can header redirect them to that upload page after processing to prevent the post data from continually going in via a refresh. But the temporary file should be cleared once the processing is done. PHP does not keep the file unless you use the move_uploaded_file function.

how to post form details including the url of a file uploaded to a csv file with php

Please could someone help with the following:
Using PHP I want to be able to post the details entered into a form to a csv file. This is quite straight forward on its own, however one of the fields in the form needs to upload a file, so the csv file needs to contain a link to where the file is saved.
Thanks
OK, it sounds to me like you have a form, one of the fields is an upload- and you want to submit the form then create a CSV from the form fields (with the upload simply showing the file location) AND upload the file?
If that is the case, handle the file upload as per normal, so the form should have (eg):
<form enctype="multipart/form-data" action="csvbuilder.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="uploadedfile" type="file" />
//other fields
<input type="submit" value="Upload File" />
</form>
Then in the target script of the form (csvbuilder.php):
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
To then reference the file in the CSV, you should simply echo:
"http://www.domain.com/uploads/".basename( $_FILES['uploadedfile']['name']);
The best you can do is simply have the location as per above, CSVs by default dont support links (though some programs like Excel may 'interpret' links and make them clickable if you wrap them in markup)
Files uploaded through PHP are by default destroyed after the PHP script has executed, so you will need to move the uploaded file to a pre-designated folder to save it.
You can use the function move_uploaded_file() to do this.
Whatever you give as the destination to move_uploaded_file() can then be put in to your CSV file.
When you upload the file you will need to use the function move_uploaded_file to put the file onto the server. So just use the same argument in that function as you do in the CSV.

Categories