Is it possible to send additional parameters when uploading a file in PHP?
Asked Answered
A

2

14

I am initializing file upload using the following HTML:

<form enctype="multipart/form-data" action="PHPScripts/upload.php" method="POST">
    <input type="file" id="browseButton" name="image" onchange="this.form.submit();" />
</form>

Upload.php script looks like this:

<?php
$file = $_FILES["image"];
$filepath = $file["name"];
$filetmp = $file["tmp_name"];
$filesize = $file["size"];
$filename = basename($filepath);
$filetype = substr($filename, strrpos($filename, ".") + 1);
...
?>

I need to pass one more parameter to my php script, but I don't know how. HTTP method is POST (as can be seen in the code above), but where should I put the parameter? Is that even possible? Thanks for clarifying this to me.

Avina answered 15/2, 2011 at 12:47 Comment(0)
M
19

Just add another input element of your choice. No additional magic required.

 <input type="hidden" name="info" value="Test">

...

$info = $_POST["info"];
Mahan answered 15/2, 2011 at 12:49 Comment(8)
Is hidden secure? I can edit the info of hidden field and submit the formYlangylang
@Who yes, it's possible to edit the info. You can never trust it if it comes from client side. What are you trying to do exactly?Mahan
If the info can be edited how hidden fields are useful? I am not getting from hidden field what I am using forYlangylang
@WhoAmI anything can be manipulated on the client side. Always. hidden just doesn't show a control to the user, but there is no way to have an unchangeable parameter coming from client side. If you need to pass data that must not be altered by the client, use sessions insteadMahan
Thanks @Mahan I am always in doubt whether to use hidden or not I am still not in the favour of hidden Session is good in the case.Ylangylang
@WhoAmI so, use whatever else. What's the problem? If you have any question - start you own, but what's your point here, dude?Northernmost
@Who it's unclear what the OP wants to do, so hidden may or may not be the solution - we can't tell reallyMahan
use hidden and check it on the server side to make sure its what you want. fin!Outride
R
0

Just put one element inside the same form where the file input is?

Rogue answered 15/2, 2011 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.