logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: HTTP-POST FILE UPLOADS
Author
MikeD254



Joined: 30 Jun 2020
Posts: 9
Location: USA, Melbourne

PostPosted: Tue 30 Jun '20 15:32    Post subject: HTTP-POST FILE UPLOADS Reply with quote

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: 7373
Location: Germany, Next to Hamburg

PostPosted: Tue 30 Jun '20 22:55    Post subject: Reply with quote

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

PostPosted: Wed 01 Jul '20 4:05    Post subject: Reply with quote

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: 7373
Location: Germany, Next to Hamburg

PostPosted: Thu 02 Jul '20 9:06    Post subject: Reply with quote

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

PostPosted: Thu 02 Jul '20 17:03    Post subject: Reply with quote

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: 7373
Location: Germany, Next to Hamburg

PostPosted: Thu 02 Jul '20 21:25    Post subject: Reply with quote

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

PostPosted: Fri 03 Jul '20 0:08    Post subject: Reply with quote

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

PostPosted: Fri 03 Jul '20 0:11    Post subject: Reply with quote

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

PostPosted: Fri 03 Jul '20 6:55    Post subject: Reply with quote

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 Smile
Back to top
MikeD254



Joined: 30 Jun 2020
Posts: 9
Location: USA, Melbourne

PostPosted: Fri 03 Jul '20 22:36    Post subject: Reply with quote

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

PostPosted: Sat 04 Jul '20 6:14    Post subject: Reply with quote

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 Very Happy

Best regards
Matthias
Back to top
MikeD254



Joined: 30 Jun 2020
Posts: 9
Location: USA, Melbourne

PostPosted: Sat 04 Jul '20 16:07    Post subject: Reply with quote

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

PostPosted: Sat 04 Jul '20 16:24    Post subject: Reply with quote

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 Very Happy
Back to top
MikeD254



Joined: 30 Jun 2020
Posts: 9
Location: USA, Melbourne

PostPosted: Sun 05 Jul '20 19:24    Post subject: Reply with quote

Thanks again...

Mike
Back to top


Reply to topic   Topic: HTTP-POST FILE UPLOADS View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner