3

I have a php file that I believe is succeeding and taking two arguments, a file name and a path, and am trying to call it with the following code.

<html>
<body>
<form action="/var/www/upload_file.php foo.php docs/School/" 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>

where foo.php and docs/School/ are my two arguments. However, when I click submit, it says:

"The requested URL /var/www/upload_file.php foo.php docs/School/ was not found on this server."

I know this kind of passing works with the exec command in php, but i'm just unsure of formatting for this in html.

3 Answers 3

5

That's not how you send things over the web. Your form should look like this:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <!--Note the relative path above; not a direct path to a server location-->
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

Then in your upload_file.php page, you can reference these values using $_POST:

<?php
    $file = $_POST['file']; // Whatever the "name" was in the HTML element
?>

edit:

Just noticed that the <input/> had a "file" type. Use $_FILES for that.

Sign up to request clarification or add additional context in comments.

3 Comments

FYI, file uploads are collected into the $_FILES super global, not $_POST
Gah...thought I'd fixed that in time, but guess not ;)
I jumped the gun on my answer. I totally missed the fact that the OP was trying to upload a file.
1

You should pass the parameters in from hidden fields and modify the php accordingly. The "action" is from the perspective of the user - putting "/var/www/upload_file.php foo.php docs/School/" into your browser will not work, so likewise this will not.

1 Comment

This is what I was looking for. I did't know about the hidden type. Thanks!
0

You need to use an MVC framework which will route the GET requests to the appropriate controller and method and pass in your parameter(s) You also need to use segmented URLs:

'/controller/method/param1/param2'.

3 Comments

Definitely don't ever "need" to use a framework... But after trying things both ways many, many times... I've come to realize that it absolutely helps beyond measure :)
The point I was trying to make -- and this was before I realized the OP was just trying to upload an image -- is that if you want incoming HTTP requests to your domain to route to 'myClass/myMethod/myParam' you need to implement MVC architecture. (ie. it's not going to route that way "automatically"). Most people use open-source MVC frameworks as opposed to setting up their own custom front-controller, but by all means you can "roll your own" MVC framework if you know how to.
Ohhh... I don't think that the OP meant /var/www... as a URL... Rather, /var/www/... is the standard file location for a default Apache installation on Linux. The OP was trying to access the filesystem through the web with arguments, the same way he'd execute something on the command line...which we both know doesn't work :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.