Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Dateiupload direkt zum PHP-Script (https://www.delphipraxis.net/215745-dateiupload-direkt-zum-php-script.html)

Hobbycoder 30. Aug 2024 09:58

Dateiupload direkt zum PHP-Script
 
Hi,
ich möchte unterschiedliche Dateien auf meinen Webserver hochladen in unterschiedliche Verzeichnisse (nicht zwingend). Dabei soll die Datei auf dem Server einen anderen Namen bekommen als der lokale Dateiname. Aber ich scheitere bereits am Upload.

Was ich bisher habe:
Delphi:
Delphi-Quellcode:
procedure TThreadUpload.Execute;
var
  http: TIdHTTP;
  ss: TStringStream;
  s: string;
  I: Integer;
  DS: TIdMultiPartFormDataStream;
  Failed: Boolean;
begin
  http:=TIdHTTP.Create(nil);
  ss:=TStringStream.Create;
  Failed:=False;
  try
    try
      for I := 0 to FFileList.Count-1 do
      begin
        s:='http://'+FServername+'/UploadInstallFiles.php';
        s:=TIdURI.URLEncode(s);
        DS:=TIdMultiPartFormDataStream.Create;
        try
          DS.AddFile('fileToUpload', FFileList[i].DestinationFilePath+FFileList[i].DestinationFileName, 'application/octet-stream');
          http.Request.ContentType:='multipart/form-data; boundary='+DS.Boundary;
          http.Request.AcceptEncoding:='';
          http.Request.Accept:='';
          http.Request.Host:='';
          http.Request.UserAgent:='';
          http.HandleRedirects := True;
          http.Put(s, DS, ss);
        finally
          DS.Free;
        end;
        DoRequestResponse(SS.DataString);
        if http.ResponseCode=200 then
        begin
          s:=ss.DataString;
          if Pos('has been uploaded', s)>0 then
          begin
            DoSuccess(FFileList[i].DestinationFilePath+FFileList[i].DestinationFileName, FUploadFileType);
          end else begin
            DoFailed(FUploadFileType);
            Failed:=True;
          end;
        end else begin
          DoFailed(FUploadFileType);
          Failed:=True;
        end;
        if Failed then Break;
      end;
    except
      DoFailed(FUploadFileType);
    end;
  finally
    http.Free;
    ss.Free;
    DoFinished(FUploadFileType, Failed);
  end;
end;
PHP:
Code:
<?php
$target_dir = "Install/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
print_r($_FILES);

if(isset($_POST["submit"])) {
   $uploadOk = 0;
}

if (file_exists($target_file)) {
  unlink($target_file);
}

if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
} else {
  echo 'Filename: '.$_FILES["fileToUpload"]["tmp_name"].'<br/>';
  echo 'Targetfilename: '.$target_file.'<br/>';
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>
Vom Server kommt 'Array'#$A'('#$A')'#$A'Filename: <br/>Name: Targetfilename: Install/<br/>Sorry, there was an error uploading your file.'

Ich bin ein bisschen Ratlos, woran's liegt. Irgendwas ist da wohl in dem MultipartFormdata noch nicht korrekt eingestellt.
Außerdem müsste ich ja noch ein weitere Parameter mit dem Dateinamen auf den Webserver übergeben, denn die Dateien sollen ja dort einen anderen haben. Das habe ich aber erst mal nach hinten geschoben, weil ja der Fileupload erst mal funktionieren muss.

Vielleicht hat einer die Muße sich das mal anzuschauen.

Hobbycoder 30. Aug 2024 10:30

AW: Dateiupload direkt zum PHP-Script
 
Ersten Fehler gefunden: Wenn man POST auswerten will, sollte man auch POST abschicken :oops:

Jetzt kommt schon mal ein

'Array'#$A'('#$A' [fileToUpload] => Array'#$A' ('#$A' [name] => echo.exe'#$A' [full_path] => echo.exe'#$A' [type] => application/octet-stream'#$A' [tmp_name] => /tmp/phpa0XEjU'#$A' [error] => 0'#$A' [size] => 8192'#$A' )'#$A#$A')'#$A'Filename: /tmp/phpa0XEjU<br/>Name: Targetfilename: Install/echo.exe<br/>Sorry, there was an error uploading your file.'

zurück.

noisy_master 30. Aug 2024 13:37

AW: Dateiupload direkt zum PHP-Script
 
Siehe hier:
https://stackoverflow.com/questions/...ver-delphi-php
Gruß

Hobbycoder 30. Aug 2024 13:59

AW: Dateiupload direkt zum PHP-Script
 
Läuft ja jetzt. Mein Fehler war ja, dass ich in Delphi versehendlich noch http.put stehen hatte und das immer übersehen habe.
Natürlich ging's dann mit den weiteren Parameter ohne Probleme.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz