<?php
session_start();
$uploadedFile = $_FILES["file"];
$filename = pathinfo($uploadedFile["name"], PATHINFO_FILENAME); 
$FileExt=  pathinfo($uploadedFile["name"], PATHINFO_EXTENSION);
$folder="uploads";
$folder_root=$_SERVER['HTTP_HOST']."/ftp";

   foreach($_POST as $key => $value):
	   //  echo $key .' : ' . $value.'<br>';
		$value=str_replace("'", "\'", $value);
		$value=str_replace('"', '\"', $value);
		if ($key=='folder'){$folder=$value;}
		if ($key=='filename'){$filename=$value;}
	//	if ($key=='FileExt'){$FileExt=$value;}
	//	if ($key=='IDc'){$IDc=$value;}
   endforeach;
 
$TargetFolder=$folder_root."/".$folder."/" ;
    $filename=$filename;
     $FileExt=$FileExt;
 
if(isset($_SESSION['FTP_FOLDER'])){
	$NameOfFolder=$_SESSION['FTP_FOLDER'];
	$TargetFolder=$_SESSION[$NameOfFolder]."/".$folder;
}
else{
	$TargetFolder= $folder;
}	
 
if(!is_dir($TargetFolder )) {
    mkdir($TargetFolder);
}
 
$TargetName= "$TargetFolder/$filename.$FileExt"; 
 
 
// move_uploaded_file($uploadedFile['tmp_name'], $TargetName);


$url ='https://media.geeksforgeeks.org/wp-content/uploads/gfg-40.png';
    
    // Use basename() function to return the base name of file
    $file_name = basename($url);
    
    // Use file_get_contents() function to get the file
    // from url and use file_put_contents() function to
    // save the file by using base name
    if (file_put_contents($file_name, file_get_contents($url)))
    {
        echo "File downloaded successfully";
    }
    else
    {
        echo "File downloading failed.";
    }
?>

 echo $uploadedFile["name"]." was uploaded  " . $TargetName . " | " . $TargetFolder . "=>".$_SESSION['FTP_FOLDER'];

?>

