Automatically choose file and upload to desired server. How to do this? - php

Hello I needed code for automatically chose file and upload it to desired link. How to do that?
html code:
<html>
<head><title>Uploading</title></head>
<body>
<form method="post" enctype="multipart/form-data" action="uploadFile.php">
<input type="file" name="file" id="file">
<input type="submit" value="Submit">
</form>
</body>
</html>]
In above code at this line
<input type="file" name="file" id="file">
this path is fixed and can't be changed as user tries. so when user click on "Submit" button the file has to upload.
How to do this?

TL;DR: What you are trying to do is absolutely impossible - for a good reason.
If this was possible, you could create a hidden upload field pointing to a file containing valuable data (e.g. the browser's cookie database) and submit the form using JavaScript (or make the user submit it without knowing about that upload) and copy any file the user has access to.

Related

Same form on page; only one works

I am trying to debug my front-end of a HTML/PHP web face and have run into an issue. Currently, the user is able to upload a file, which is then sent to a separate upload function (to process and upload their file to an AWS instance), and then redirects the user to another page.
On the page, I have two options for the user to click- the first of which unhides an option for the user to select and upload their file. This option uploads to the AWS instance and functions normally. The second option unhides some text about download/upload instructions, along with an option for the user to select and upload their file.
Both options do functionally the same thing, with only difference being the second option unhides some text in addition to revealing exactly the same button. Only the first option posts and uploads like normal; the second doesn't upload to the bucket and just returns 'zero' in plain text to the screen. Has anyone experienced anything like this and/or has any advice on how to fix it? Thanks!
Below are the two code snippets for the upload forms:
<form id="Site" method="POST" enctype="multipart/form-data">
<div id="file_input"><input id="files" type="file" name="file"/></div>
<label for="files">SELECT ZIP FILE TO UPLOAD</label>
<br>
<input type="submit" value="submit" name="submitSite">
</form>
and the one that doesn't work (exactly the same?)
<p> Download Instructions Go Here</p><br>
<form id="Site" method="POST" enctype="multipart/form-data">
<div id="file_input"><input id="files" type="file" name="file"/></div>
<label for="files">SELECT ZIP FILE TO UPLOAD</label>
<br>
<input type="submit" value="submit" name="submitSite">
</form>
And the POST function itself is:
<?php
session_start();
$_SESSION['Username'] = '';
$userID = $userID; // change this to be set in database
//set progress
$_SESSION['progress'] = 'upload_section';
'/survey_interface_shared_libraries/progress_emails/store_progress.php';
if (isset($_POST['submitSite'])) { // upload file
Please let me know if you need any further context!

HTML form - keep reference to uploaded file for 2 button options

I have an html form and 2 php files, all of which work well. In the form I created a preview button that references a php file that shows the uploaded cvs in an html table as a preview. The submit button inserts it into a database.
I would like to have the form contain one button that goes to the preview page and once the info is confirmed, there would be another button that references the submitted file, but I can't seem to get that to work.
Due to the environment I'm trying to get this to work without javascript or ajax if possible, as another user gave me a javascript option.
Here is the html form:
<form method="post" action="confirm.php" enctype="multipart/form-data">
<label>Upload File</label>
<input type="file" name="file">
<input type="submit" name="submit" value="Confirm" >
</form>
<form method="post" action="upload.php" enctype="multipart/form-data">
<label>Upload File</label>
<input type="file" name="file">
<input type="submit" name="submit" value="Submit">
</form>
Each Php file corresponds as well:
if(isset($_POST['submit']))
Is there any way to go from upload to preview to submit and keep reference to $file?

PHP Single button file upload through internal php function

As part of a class I've been asked to create a simple file upload system that uploads files in a single button to a folder called ./files. I've been able to do this and I have been able to keep it all contained in one a3.php file with this:
<form action="a3.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submitFile">
</form>
Which opens up a file dialog and only requires the user to press two buttons
I use the following function to run the upload function.
if(isset($_POST['submitFile']))
{
uploadFile();
}
The uploadFile() function simply uses move_uploaded_file to move to file to the ./files folder. Currently without any questions asked.
My issue is that my attempts to make the buttons for the file upload a single one have been stymied. I have attempted to use the following code to resolve the issue:
<form name="frm" action="a3 - Copy.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" onchange="frm.submit();">
</form>
Which, while it refreshes the page as if an action is being performed, appears to do nothing. I have attempted to look into how exactly .submit() works but haven't gotten any closer to understanding any intricacies it may have while a part of onchange.
I've looked around and I've found code that can upload in a single button, and I've found code that can upload using an internal php function, but none that does both.
You can try using a button but not displaying it. When the file input is changed, using javascript trigger a click on the hidden button, like in the code below:
<form name="frm" action="a3 - Copy.php" method="post" enctype="multipart/form-data">
<input type="submit" name="submitFile" style="display:none" id="submit">
<input type="file" name="uploadFile" onchange="document.getElementById('submit').click()">
</form>
<?php
if(isset($_POST['submitFile']))
{
uploadFile();
}
?>
You're basically looking for an ajax upload. You can do this using javascript (on modern browser), and the easiest thing would probably be using a library like jquery. Otherwise, you can do it using an iframe.
Here are a few similar questions with example scripts-
Using HTML5 file uploads with AJAX and jQuery
jQuery Ajax File Upload

How to retain the input file type post data

How can I redirect a page with a file POST data on it? Something like
Page1.php
<html>
<form action="page2.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Send"/>
</form>
</html>
Page2.php
<html>
<form action="page3.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/> ### get the value of file from page1.php and send to page 3
<input type="submit" value="Send"/>
</form>
</html>
Is it possible?
Not that way... the file is uploaded when the user submits the form. You could then base64 encode that file and embed it in the HTML as a hidden field to be submitted on the second form, but that's very inefficient.
You should handle the file upload on the first submission. Save the file somewhere or stick it in your database, whichever works better for the files you're receiving. If you save the file, I'd recommend generating a unique name for it (like a UUID). Then use the filename or the primary key from the DB as a value in a hidden field in the second form, which you can use to find the file you already received when the user submits the second form.

Get remote file path location

I am working with PHP and would like to get a remote file path location so that I can read file contents.
I would like the user to direct to a particular file, which can be located anywhere in the computer, so that it can be processed by the script.
Thanks
You can offer a user the ability to locate a file on their local computer and submit it to you via a web form, like..
<form id="myForm" enctype="multipart/form-data" action="/formHandler.php" method="post" >
<label for="fileUpload">File to Upload:</label>
<input name="fileUpload" id="fileUpload" type="file" /><br />
<input name="submit" id="submit" type="submit" value="Upload Now"></form>
Then you can process it on your side with PHP, or whatever you have on the server end. Since PHP is one of your tags, you can learn more on how to access, and work with, on the server end from the PHP reference site:
http://www.php.net/manual/en/features.file-upload.post-method.php
I hope I understood your question correctly..

Categories