I am trying to upload a zip file to FTP server using Apache HttpClient.
My java code is like this:
public void doFileUpload(String Localfile, String myurl) {
HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.build();
HttpPost httpPost = new HttpPost(myurl);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addBinaryBody("file", new File(Localfile));
final HttpEntity entity = entityBuilder.build();
System.out.println(entity.getContentLength());
httpPost.setEntity(entity);
try {
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.toString());
System.out.println("Success");
} catch (IOException ex) {
System.out.println("Failed"+ex);
Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
}
In my php File :
if (!empty($_FILES["file"]["name"])) {
$dat = time();
$file_name=$_FILES["file"]["name"];
$temp_name=$_FILES["file"]["tmp_name"];
$target_path = $file_name."-".$dat;
if (is_uploaded_file($temp_name)) {
echo "File ". $target_path ." uploaded successfully.
";
move_uploaded_file ($temp_name, $target_path);
} else {
echo "Possible file upload attack: ";
echo "filename '". $temp_name . "'.";
print_r($_FILES);
}
}
This works fine if I upload a file to the folder where my php file exists. But In my condition, user will upload their files to their own folder. Example: User A will upload a file to myftp/upload/A/
or user B will upload a file to myftp/upload/B/
The target folder is defined from the user end. I can create folder for all the users manually and place a php file in every folder. But how can I do this with a single php file ?