Author |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Tue 30 Jun '20 15:32 Post subject: HTTP-POST FILE UPLOADS |
|
|
I am trying to set up Apache2 on my Ubuntu server to have the ability to post client files to a directory on the server.
So far the “Choose File” function works.
Once the client chooses a file on his machine the file name appears next to the “Choose File button to confirm the client’s choice.
Once the file is chosen the client clicks on the “Upload Button”.
Here is my problem.
When the “Upload Button” is pressed the server returns:
Not Found
The requested URL was not found on this server.
The steps I have taken thus far is to:
1. created a target path folder and given permissions to all users (chmod 777)
2. installed a post handler script
Code: |
<?php
$target_path = "/home/<username>/webbpost/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo "Source=" . $_FILES['uploadedfile']['name'] . "<br />";
echo "Destination=" . $destination_path . "<br />";
echo "Target path=" . $target_path . "<br />";
echo "Size=" . $_FILES['uploadedfile']['size'] . "<br />";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
|
3. created the html form
Code: |
<html>
<head></head>
<body>
<h4> File uploads </h4>
<form enctype="multipart/form-data" action="/usr/lib/cgi-bin/upload.php"
method="post">
<p>
Select File:
<input type="file" size="35" name="uploadedfile" />
<input type="submit" name="Upload" value="Upload" />
</p>
</form>
</body>
</html>
|
Does anyone see what I have done wrong so far ?
Is there some additional steps I need to take ?
I am very grateful for any help or suggestions...
Last edited by MikeD254 on Wed 01 Jul '20 3:57; edited 1 time in total |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Tue 30 Jun '20 22:55 Post subject: |
|
|
Is the path /cgi-bin/upload.php correct? Why in cgi-bin folder? How did you configure PHP in your webserver? |
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Wed 01 Jul '20 4:05 Post subject: |
|
|
My mistake. I posted the wrong version of the html form.
I have edited my post show the correct version with the path /usr/lib/cgi-bin/upload.php .
Thanks for taking time to look this over |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Thu 02 Jul '20 9:06 Post subject: |
|
|
The php file should not be in the cgi-bin folder, but in the same folder / document root as your html file is.
What also helps is in the top of the php file
Code: |
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
|
|
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Thu 02 Jul '20 17:03 Post subject: |
|
|
Thanks.
Added the code you suggested to the php file.
Moved the php file to document root folder.
Edited the html file to to reflect the path change.
Restarted Apache2 server.
Still getting same result (The requested URL was not found on this server.), with no other error messages displayed.
I am stumped...? |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Thu 02 Jul '20 21:25 Post subject: |
|
|
Both file are now in the same folder? What is in the apache error log or access log about that? |
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Fri 03 Jul '20 0:08 Post subject: |
|
|
Nothing in the error log about it.
Here is the access log:
192.168.5.140 - - [02/Jul/2020:17:59:34 -0400] "GET /upld.html HTTP/1.1" 200 542 "http://userver/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.119 Safari/537.36"
192.168.5.140 - - [02/Jul/2020:17:59:43 -0400] "POST /var/www/html/upload.php HTTP/1.1" 404 486 "http://userver/upld.html" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.119 Safari/537.36" |
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Fri 03 Jul '20 0:11 Post subject: |
|
|
Yes, the html file and the upload.php are now in:
/var/www/html |
|
Back to top |
|
mraddi
Joined: 27 Jun 2016 Posts: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Fri 03 Jul '20 6:55 Post subject: |
|
|
Hello MikeD254,
please use "/upload.php" as form's "action"-parameter (<form method="post" action="/upload.php"...) and not the complete physical path "/var/www/html/upload.php".
The path you see in Apache's access.log is appended to the document-root, which is in your case "/var/www/html" resulting in Apache trying to read the file "/var/www/html/var/www/html/upload.php"...
Best regards
Matthias |
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Fri 03 Jul '20 22:36 Post subject: |
|
|
Success...!!
Now the file chosen uploads to the web server.
Only left with one question.
After the file uploads the server displays this :
Source=ExtractPage2.jpg
Notice: Undefined variable: destination_path in /var/www/html/upload.php on line 9
Destination=
Target path=/home/mike/webbpost/ExtractPage2.jpg
Size=12047
The file ExtractPage2.jpg has been uploaded
Where can I correct the "undefined variable" on line 9? |
|
Back to top |
|
mraddi
Joined: 27 Jun 2016 Posts: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Sat 04 Jul '20 6:14 Post subject: |
|
|
Hello,
the errormessage is correct as the variable is not assigned a value before it is used on line 9 with
echo "Destination=" . $destination_path . "<br />";
So you can either assign a value to $destination_path in the lines before or simply delete line 9
Best regards
Matthias |
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Sat 04 Jul '20 16:07 Post subject: |
|
|
Many thanks to James Bond, and Matthias for taking time to help. All is now working as it should, and I have learned a lot. One more question, what is the difference between "Target_path" and "Destination_path" ?
Thanks again for all your help and guidance.
Mike |
|
Back to top |
|
mraddi
Joined: 27 Jun 2016 Posts: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Sat 04 Jul '20 16:24 Post subject: |
|
|
Hello Mike,
I don't know the difference - from PHP's point of view both are just variables.
$target_path is set by your code with "$target_path = ...".
$destination_path is never set to a value and therefore reading its value results in the notice about undefined variable.
Best regards
Matthias |
|
Back to top |
|
MikeD254
Joined: 30 Jun 2020 Posts: 9 Location: USA, Melbourne
|
Posted: Sun 05 Jul '20 19:24 Post subject: |
|
|
Thanks again...
Mike |
|
Back to top |
|