false, 'status' => 'No URL provided']; } if (!filter_var($url, FILTER_VALIDATE_URL)) { return ['reachable' => false, 'status' => 'Invalid URL format']; } $ch = curl_init($url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($httpCode >= 200 && $httpCode < 400) { return ['reachable' => true, 'status' => "URL is accessible (HTTP $httpCode)"]; } else { return ['reachable' => false, 'status' => "URL is not accessible: " . ($error ? $error : "HTTP $httpCode")]; } } $urlReachability = []; if (!empty($_GET['url'])) { $urlReachability = checkUrlReachability($_GET['url']); } ?> " . htmlspecialchars($urlReachability['status']) . ""; } ?> [ "method"=>"HEAD", "header"=>"Authorization: Basic ".base64_encode("$username:$password"), "ignore_errors"=>true ]]; $ctxCheck = stream_context_create($optsCheck); @file_get_contents($url,false,$ctxCheck); if (isset($http_response_header)) { foreach($http_response_header as $line){ if (preg_match('#HTTP/\d+\.\d+\s+(\d+)#', $line, $m)){ $code = intval($m[1]); if($code >= 200 && $code < 300){ $debug[] = "WebDAV upload canceled: '$remoteFile' already exists."; return false; } break; } } } // 2. Upload $data = file_get_contents($localFile); $opts = ["http"=>[ "method"=>"PUT", "header"=>"Authorization: Basic ".base64_encode("$username:$password")."\r\nContent-Length: ".strlen($data), "content"=>$data, "ignore_errors"=>true ]]; $ctx = stream_context_create($opts); $res = @file_get_contents($url,false,$ctx); if ($res === false) { $debug[] = "WebDAV upload failed: Cannot reach server or unauthorized."; return false; } if (isset($http_response_header) && is_array($http_response_header)) { $statusLine = $http_response_header[0]; preg_match('#HTTP/\d+\.\d+\s+(\d+)#', $statusLine, $matches); $code = isset($matches[1]) ? intval($matches[1]) : 0; if ($code >= 200 && $code < 300) { $debug[] = "WebDAV upload successful: $remoteFile (HTTP $code)"; return true; } else { $debug[] = "WebDAV upload failed: HTTP $code - " . $statusLine; return false; } } $debug[] = "WebDAV upload unknown result."; return false; } function buildUrl($protocol,$server,$path) { $scheme=($protocol==='webdav')?'https':$protocol; return "$scheme://$server$path"; } function listDirectory($protocol, $server, $username, $password, $path, &$debug) { $files=[]; if ($protocol==='ftp') { $conn=@ftp_connect($server); if($conn&&@ftp_login($conn,$username,$password)){ ftp_pasv($conn,true); if(@ftp_chdir($conn,$path)){ $raw=ftp_rawlist($conn,"."); foreach($raw as $line){ $parts=preg_split("/\s+/",$line,9); if(count($parts)===9){ $files[]=['name'=>$parts[8],'type'=>$parts[0][0]==='d'?'dir':'file']; } } } ftp_close($conn); } } elseif($protocol==='sftp') { $conn=@ssh2_connect($server); if($conn&&@ssh2_auth_password($conn,$username,$password)){ $sftp=ssh2_sftp($conn); $dh=@opendir("ssh2.sftp://$sftp$path"); if($dh){while(($f=readdir($dh))!==false){if($f==='.'||$f==='..')continue;$fp="ssh2.sftp://$sftp$path$f";$files[]=['name'=>$f,'type'=>is_dir($fp)?'dir':'file'];}closedir($dh);} } } elseif($protocol==='webdav') { $url=buildUrl($protocol,$server,$path); $opts=["http"=>["method"=>"PROPFIND","header"=>"Authorization: Basic ".base64_encode("$username:$password")."\r\nDepth: 1","content"=>""]]; $ctx=stream_context_create($opts); $res=@file_get_contents($url,false,$ctx); if($res!==false){preg_match_all('/(.*?)<\/d:href>/i',$res,$m);foreach($m[1] as $href){$nm=basename(parse_url($href,PHP_URL_PATH));if($nm&&$nm!==basename($path)){$files[]=['name'=>$nm,'type'=>(substr($href,-1)==='/')?'dir':'file'];}}} } return $files; } // ---------------- FORM VALUES ----------------- $username = getQuery('username'); $password = getQuery('password'); $server = getQuery('server'); $path = getQuery('path', '/'); $protocol = getQuery('protocol', 'ftp'); $uploadType = getQuery('upload_type', 'url'); $anonymous = getQuery('anonymous', '') ? true : false; if ($anonymous) { $username = 'anonymous'; $password = 'anonymous@domain.com'; } $isDir = substr($path, -1) === '/'; $files = []; $debug = []; // ---------------- HANDLE UPLOAD ----------------- if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($uploadType==='file' && !empty($_FILES['upload_file']['tmp_name'])) { $file = $_FILES['upload_file']; $remoteFile = basename($file['name']); $tmpFile = $file['tmp_name']; if($protocol==='ftp') ftpUpload($server,$username,$password,$path,$tmpFile,$remoteFile,$debug); elseif($protocol==='sftp') sftpUpload($server,$username,$password,$path,$tmpFile,$remoteFile,$debug); elseif($protocol==='webdav') webdavUpload($server,$username,$password,$path,$tmpFile,$remoteFile,$debug); } elseif($uploadType==='url' && !empty($_POST['upload_url'])) { $urlFile = $_POST['upload_url']; $remoteFile = basename(parse_url($urlFile, PHP_URL_PATH)); $tmpFile = tempnam(sys_get_temp_dir(), 'up'); if(file_put_contents($tmpFile, file_get_contents($urlFile))===false){ $debug[] = "Download from URL failed: $urlFile"; } else { if($protocol==='ftp') ftpUpload($server,$username,$password,$path,$tmpFile,$remoteFile,$debug); elseif($protocol==='sftp') sftpUpload($server,$username,$password,$path,$tmpFile,$remoteFile,$debug); elseif($protocol==='webdav') webdavUpload($server,$username,$password,$path,$tmpFile,$remoteFile,$debug); } unlink($tmpFile); } if(!empty($debug)){ echo "
"; foreach($debug as $d) echo htmlspecialchars($d) . "
"; echo "
"; } } // ----------------- HTML FORM ------------------ echo '
Upload type:





Username:
Password:
Server:
Path:
Protocol:


Select file to upload:

Enter URL to upload:

'; // ---------------- Directory listing ------------------- if ($server && $isDir) { $files = listDirectory($protocol, $server, $username, $password, $path, $debug); if ($path !== '/') { $parent = rtrim($path, '/'); $parent = substr($parent, 0, strrpos($parent, '/') + 1); echo '.. (Parent directory)
'; } echo ""; } ?>