Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
upload image with php file
#1
i have this uploading php script 

Code:
<?php

/* Check if file already exists in location file save */
$fdirectory  = "uploads/";
if (!file_exists($fdirectory)) {
die('You Need To Create uploads Folder before start uploading..<br> <b>Dont Forget To Give Premission 777 To the Folder</b>');
}


if (isset($_POST['upload'])) {

$imgsize = $_FILES['image']['size'];
$image_name = $_FILES['image']['name'];
$image_tempname = $_FILES['image']['tmp_name'];
$image_type = getimagesize($image_tempname);
$image_ext = explode('.',$image_name);
$ext = strtolower(end($image_ext));
$FileError = $_FILES['image']['error'];
$allow_ext = array('png','jpg','gif','jpeg','bmp');
$allow_type = array('image/png','image/gif','image/jpeg','image/bmp');

if (in_array($ext, $allow_ext) && in_array($image_type['mime'], $allow_type)) {
    
if ($FileError === 0) {
    
if ($imgsize < 300000) {

list($width, $height, $mime) = $image_type;

if ($width>0 && $height>0) {

#do Upload
$NewImageName = uniqid('', true).".".$ext;
$moveImg = $fdirectory.$NewImageName;

$upload = move_uploaded_file($image_tempname, $moveImg);

if ($upload) {
header("Location: imgup.php?img=".$moveImg ."");
exit;    
} else {
header("Location: imgup.php?res=failed");
exit;    
} # end check if uploaded

} else {
header("Location: imgup.php?res=error");
exit;    
}# image width check

} else {
header("Location: imgup.php?res=large");
exit;    
    
} # File size check end
    
    
} else {#FileError

header("Location: imgup.php?res=error");
exit;        

}

} else { # end of sec check

header("Location: imgup.php?res=error");
exit;    
    
}

/* isset check end*/
}
?>


and i am using httpclient to post the file to  php like following


Code:
HttpClient := THTTPClient.Create;
try

HttpClient.AllowCookies := True;
HttpClient.UserAgent := sUserAgent;
HttpClient.HandleRedirects := True;
HttpClient.MaxRedirects := 3;
HTTPClient.ConnectionTimeout := 0;
HTTPClient.ResponseTimeout   := 0;


PostData := TIdMultipartFormDataStream.Create;
try

try
PostData.AddFile('image', FFileToUpload);
HttpResponse := HttpClient.Post(FdataURL, PostData);
FoutData := HttpResponse.ContentAsString();
except
end;

finally
PostData.Free;
end;




finally
HttpClient.Free;
end;


but the image doesnt seems to get uploaded and i get an empty response 

what i am doing wrong ?

in html it works fine 
Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
 
<div class="form-group file-area">
 
    <input type="file"  accept="image/*" name="image" id="images">
  </div>
 
 
  <div class="form-group">
    <button type="submit" name="upload">Upload image</button>
  </div>
 
</form>
Reply
#2
(06-17-2020, 05:05 PM)Madammar Wrote: i am using httpclient to post the file to  php

Since you are using Indy anyway, why are you using Embarcadero's THTTPClient and not Indy's TIdHTTP? Or, for that matter, why are you using Indy's TIdMultipartFormDataStream and not Embarcadero's TMultipartFormData? You really should not be mixing libraries like you are.

(06-17-2020, 05:05 PM)Madammar Wrote: the image doesnt seems to get uploaded and i get an empty response 

THTTPClient.Post() has no concept of TIdMultipartFormDataStream.  It just sees the stream as any other TStream object and posts its data as-is.  Which won't work in your case, because you are not setting the THTTPClient.ContentType property to specify the media type of the stream (multipart/form-data) or the MIME boundary that the stream uses between its MIME parts.  So the PHP script won't be able to parse the stream data correctly.

TIdHTTP.Post(), on the other hand, recognizes TIdMultipartFormDataStream and will set its own Request.ContentType property accordingly for you. So does THTTPClient.Post() with TMultipartFormData.

(06-17-2020, 05:05 PM)Madammar Wrote: what i am doing wrong ?

At the very least, if you are going to continue using TIdMultipartFormDataStream then you need to add the following line before calling THTTPClient.Post():

Code:
HttpClient.ContentType := PostData.RequestContentType;

Otherwise, use TMultipartFormData instead of TIdMultipartFormDataStream, or use TIdHTTP instead of THTTPClient.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)