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


Messages In This Thread
upload image with php file - by Madammar - 06-17-2020, 05:05 PM
RE: upload image with php file - by rlebeau - 06-17-2020, 07:24 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)