Recent

Author Topic: FPHTTPClient - how to send a flie from disk to server?  (Read 4107 times)

Alexandr F

  • New Member
  • *
  • Posts: 26
FPHTTPClient - how to send a flie from disk to server?
« on: March 25, 2019, 12:59:38 pm »
I want to send file from disk to server.

I try to use

TFPHttpClient.FileFormPost('localhost/upload.php?filename=aaa.bin','aaa.bin');

But php-script on server does not recognize any request parameters?

Any examples of POST request exist for TFPHTTPCLIENT?


GetMem

  • Hero Member
  • *****
  • Posts: 3452
Re: FPHTTPClient - how to send a flie from disk to server?
« Reply #1 on: March 25, 2019, 01:54:41 pm »
Please take a look at the attached project. Few notes:
1. Copy upload.php to your server
2. Create a folder(server side) named "test". For more details search for 'test/' inside upload.php
3. Don't forge to change the values inside bUploadClick procedure

Alexandr F

  • New Member
  • *
  • Posts: 26
Re: FPHTTPClient - how to send a flie from disk to server?
« Reply #2 on: March 25, 2019, 02:46:48 pm »
GetMem,

Thank You very much!

local-vision

  • New Member
  • *
  • Posts: 46
Re: FPHTTPClient - how to send a flie from disk to server?
« Reply #3 on: June 29, 2021, 03:39:48 am »
Dear GetMem,

Hope you are listening.

I want to first want to express my gratitude for providing your important solution. Unfortunately I have not been able to get it to work.

Your input would be very greatly valued here.

I have a text file named: test1.bin. It is the application folder. This is seems to get picked up as the progress bar goes full green and the file size is listed. But I get Boolean False result from the UploadFile function.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.bUploadClick(Sender: TObject);
  2. var
  3.   ErrMsg, FL: String;
  4. begin
  5.   FL:=Application.Location+'test1.bin';
  6.  
  7.   if UploadFile('http://mydomain/upload.php', 'bin', FL, 'test1.bin', ErrMsg) then
  8.     ShowMessage('Successfully uploaded!')
  9.   else
  10.     ShowMessage('Cannot upload file: ' + sLineBreak + ErrMsg);
  11. end;
  12.  

I have an apache server setup with var/www/mydomain/

Within that I have the upload.php file and a folder named: test/

I have appended the hosts file to point to mydomain and tested it. So for instance http://mydomain/index.html works.

the PHP code is:

Code: PHP  [Select][+][-]
  1.  
  2. <?php
  3.   if (isset($_FILES['bin']['name']) && !empty($_FILES['bin']['name']))
  4.   {
  5.     $tmp_name_zip = $_FILES['bin']['tmp_name'];
  6.  
  7.     $location = 'test/';
  8.     if (move_uploaded_file($tmp_name_zip, $location . $_FILES['bin']['name']))      
  9.       echo 'zipok';
  10.   }
  11. ?>
  12.  
  13.  


Any ideas what I might be doing wrong? Questions, comments? Please let me know.

Many thanks.

 

sstvmaster

  • Sr. Member
  • ****
  • Posts: 258
Re: FPHTTPClient - how to send a flie from disk to server?
« Reply #4 on: June 30, 2021, 08:30:14 am »
...
I have a text file named: test1.bin. It is the application folder. This is seems to get picked up as the progress bar goes full green and the file size is listed. But I get Boolean False result from the UploadFile function.
...

If you are on Linux, you must set the write rights for the folder "test", use "chmod 744 var/www/mydomain/test". 744 is the minimum requirement, 755 is also ok.

I tested this on my server and it works.
« Last Edit: June 30, 2021, 08:32:10 am by sstvmaster »
Windows 10 (64 bit)
Lazarus: 2.0.12 / Trunk x32

local-vision

  • New Member
  • *
  • Posts: 46
Re: FPHTTPClient - how to send a flie from disk to server?
« Reply #5 on: June 30, 2021, 03:39:46 pm »
Hi sstvmaster,

Many thanks for your input!

(Indeed, forgot to mention this is a LAMP server (Linux, Apache, MySQL, PHP))

Your are correct. The problem had to do with permissions/rights.

When setting up my server I set the ownership to me (user):

Code: Bash  [Select][+][-]
  1.  sudo chown -R $USER:$USER /var/www/mydomain

What I needed to do was set the owner to Apache itself which has the name www-data. So this would be:

Code: Bash  [Select][+][-]
  1.  sudo chown -R www-data:www-data /var/www/mydomain

then also:

Code: Bash  [Select][+][-]
  1.  sudo chmod -R 755 /var/www/mydomain

In addition you need to make sure that PHP which handles the operation server side allows file uploads. This can be found in the PHP ini file (it usually does allow this as this is the default setting).

In your "php.ini" file, search for the file_uploads directive, it must read:

Code: PHP  [Select][+][-]
  1.  file_uploads = On

To get some feedback on things you must also check your error and access logs. These can be found:

/var/log/apache2

Here is the PHP file with some added stuff. The first 3 statement provide some error checking. PHP is not that intuitive for a Pascal coder. $_FILES var which includes 'name' is actually an array that include things like size, type, error tmp_name. I have added this to the PHP file. I have added basename which ExtractsFileName. Notice that bin relates to field name in the client side pascal code.

Code: PHP  [Select][+][-]
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5.  
  6.   if (isset($_FILES['bin']['name']) && !empty($_FILES['bin']['name']))
  7.   {
  8.     $tmp_name_zip = $_FILES['bin']['tmp_name'];
  9.  
  10.     $location = 'test/';
  11.     if (move_uploaded_file($tmp_name_zip, $location . basename($_FILES['bin']['name'])))
  12.       echo 'zipok<br>';
  13.     echo ("size " . $_FILES['bin']['size']);
  14.     echo ("<br>");
  15.     echo ("type " . $_FILES['bin']['type']);
  16.     echo ("<br>");
  17.     echo ("error " . $_FILES['bin']['error']);
  18.     echo ("<br>");
  19.     echo ("tmp_name " . $_FILES['bin']['tmp_name']);
  20.     echo ("<br>");
  21.     echo ("name (client provided name) " . $_FILES['bin']['name']);
  22.     echo ("<br>");
  23.   } else {
  24.         echo "something wrong with the filename";
  25.   }
  26. ?>  
  27.  

Again special thanks to sstvmaster and GetMem for providing me with the means to get things working.
 


 

TinyPortal © 2005-2018