Skip to content

Commit 6b5d099

Browse files
committed
v14
1 parent fdf4a6d commit 6b5d099

File tree

3 files changed

+122
-31
lines changed

3 files changed

+122
-31
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# **Upload File from URL to Web Server v.13**
1+
# **Upload File from URL to Web Server v.14**
22

33
![screenshot.png](screenshot.png)
4+
![screenshot-2.png](screenshot-2.png)
45

56
A simple and efficient solution for uploading files directly from a URL to your web server (e.g., CPanel). This tool is perfect for quick tasks where you need to download and store files on your hosting environment with minimal effort.
67

7-
**Latest Publish Date:** 2024/08/18 (1403/05/28)
8+
**Latest Publish Date:** 2025-01-08 | 1403-10-19
89

910
## Features
1011

12+
- **WordPress-Ready:** Upload Latest WordPress and UnZip its archive and move content to root.
1113
- **One-Click Upload:** Effortlessly upload files from a URL to your hosting storage.
1214
- **Self-Destruct Feature:** The script can self-destruct after use, enhancing security.
1315
- **Auto File-Name Filler:** Automatically suggests a file name based on the URL.
@@ -45,13 +47,15 @@ This tool is provided as-is, and is intended for personal use only. The author t
4547

4648
## Changelog:
4749

50+
- Version 14: Added WordPress-Ready mode, UnZip Archive and move its content to root
51+
- Version 14: Added Self Destruct link to finish screen after upload done
4852
- Version 13: Added Self Destruct feature
49-
- Version 12: Added New features and stability.
53+
- Version 12: Added New features and stability
5054
- Version 08: Added Compatibility with NON-SSL and DirectAdmin hosting
5155
- Version 07: Added Auto file-name filler and Back to Root button
5256
- Version 06: Added Timer, showing elapsed and total time
5357
- Version 05: More stability on large files processing
5458
- Version 04: Fixed get file size error
5559
- Version 03: Added Some styling and visuals
5660
- Version 02: Added Real-time progress while uploading
57-
- Version 01: Initiate release, at 2020/11/15 - 1399/08/25
61+
- Version 01: Initiate release, at 2020/11/15 - 1399/08/25

screenshot-2.png

333 KB
Loading

upload.php

Lines changed: 114 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: Amirhossein Hosseinpour <https://amirhp.com>
55
* @Date Created: 2020/11/15 19:31:15
66
* @Last modified by: amirhp-com <its@amirhp.com>
7-
* @Last modified time: 2024/08/18 23:17:34
7+
* @Last modified time: 2025/01/08 14:08:56
88
*/
99

1010
@ini_set("display_errors", 1);
@@ -13,7 +13,7 @@
1313
<html>
1414

1515
<head>
16-
<title>Upload File from URL to WebServer v13</title>
16+
<title>Upload File from URL to WebServer v14</title>
1717
<style>
1818
* {
1919
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
@@ -110,10 +110,10 @@
110110
<link rel="icon" href="https://raw.githubusercontent.com/amirhp-com/upload-file-from-url-to-webserver/main/blackswan.png" sizes="192x192" />
111111
</head>
112112

113-
<body class="uffutw upload-file-from-url-to-webserver v-13">
113+
<body class="uffutw upload-file-from-url-to-webserver v-14">
114114
<h1 class="aw">
115115
<div style="background: url('https://raw.githubusercontent.com/amirhp-com/upload-file-from-url-to-webserver/main/blackswan.png') no-repeat center/contain;padding-top: 5rem;margin-bottom: 0.5rem;"></div>
116-
<span class="dw">BlackSwan Upload File from URL to Web Server v.13</span><br>URL-Address to WebServer</h1>
116+
<span class="dw">BlackSwan Upload File from URL to Web Server v.14</span><br>URL-Address to WebServer</h1>
117117
<?php
118118
if (isset($_GET["delete"]) && "true" == $_GET["delete"]) {
119119
unlink(__FILE__);
@@ -128,22 +128,24 @@
128128
$url = $_POST["url"];
129129
$name = $_POST["name"];
130130
$folder = $_POST["folder"] ?? "";
131-
$folder = !empty($folder) ? rtrim($folder, '/\\') . "/" : "";
131+
$folder = !empty($folder) ? rtrim(ltrim($folder, '/\\'), '/\\') : "";
132132
ob_implicit_flush(true);
133133
ob_start();
134134
$server = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]";
135135
echo "<script>
136-
document.title = 'Uploading ...';
136+
document.title = 'Uploading ...';
137137
document.querySelector('h1').innerHTML += `
138138
<div style=\"font-size: 0;margin-top: 3rem;\"><small style=\"font-size: 1rem;\">
139-
Transferring <a href='$url' target='_blank'>Origin URL-Address</a> as <a href='{$server}/{$folder}{$name}' target='_blank'><strong>{$folder}{$name}</strong></a><br>
139+
Transferring <a href='$url' target='_blank'>Origin URL-Address</a> as <a href='{$server}/".(!empty($folder)?"{$folder}/":"")."{$name}' target='_blank'><strong>".(!empty($folder)?"{$folder}/":"")."{$name}</strong></a><br>
140140
<span style=\"margin-top: 1rem;display: block;\">Please wait until process complete or Press ESC key to cancel</span></small></div>
141141
<div id=\"progress\"></div>`;</script>";
142142
$time = microtime(true);
143143
$remote = fopen($url, 'r');
144-
if (!is_dir($dirname)) mkdir($folder, 0777, true);
145-
if (file_exists($folder . $name)) unlink($folder . $name);
146-
$local = fopen($folder . $name, 'w');
144+
if (empty($folder)) { $folder = __DIR__; }
145+
if (!is_dir($folder)) mkdir($folder, 0777, true);
146+
$path_full = $folder . "/" . $name;
147+
if (file_exists($path_full)) @unlink($path_full);
148+
$local = fopen($path_full, 'w');
147149
stream_context_set_default(array('http' => array('method' => 'HEAD')));
148150
$headers = get_headers($url, true);
149151

@@ -152,9 +154,8 @@
152154
ob_end_flush();
153155
ob_flush();
154156
flush();
155-
ob_start();
156157
$ch = curl_init($url);
157-
$fp = fopen($folder . $name, 'wb');
158+
$fp = fopen($path_full, 'wb');
158159
curl_setopt($ch, CURLOPT_FILE, $fp);
159160
curl_setopt($ch, CURLOPT_HEADER, 0);
160161
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
@@ -168,19 +169,14 @@
168169

169170
$headers = array_change_key_case(get_headers($url, 1));
170171
$filesize = $headers['content-length'];
171-
ob_end_flush();
172-
ob_flush();
173-
flush();
174-
ob_start();
172+
ob_end_flush(); ob_flush(); flush(); ob_start();
175173
if ($filesize < 1) {
176174
echo "<pre style='text-align: left; direction: ltr; border:1px solid #1e45cf; padding: 1rem; color: #1e45cf;'>" . print_r($headers, 1) . "</pre>";
177175
echo "<pre style='text-align: left; direction: ltr; border:1px solid #c34f1e; padding: 1rem; color: #c34f1e;'>" . print_r($_SERVER, 1) . "</pre>";
178176
exit;
179177
}
180-
$read_bytes = 0;
181-
$num = 0;
178+
$read_bytes = 0; $num = 0; $progress = 1;
182179
$steps = floor($filesize / 2048) / 300 < 0 ? 3 : floor($filesize / 2048) / 300;
183-
$progress = 1;
184180
echo "<script>
185181
document.title = 'Uploading " . sprintf("%'05.2f%%", $progress) . "';
186182
document.querySelector('#progress').innerHTML = '( " . sprintf("%'05.2f%%", $progress) . "" . human_filesize($filesize) . " )<br><small style=\"text-transform: capitalize;\">Elapsed Time: " . human_timing($time) . "</small>';
@@ -201,21 +197,104 @@
201197
</script>";
202198
}
203199
}
204-
ob_end_flush();
205-
ob_flush();
206-
flush();
207-
ob_start();
200+
ob_end_flush(); ob_flush(); flush(); ob_start();
208201
}
209202
fclose($remote);
210203
fclose($local);
204+
205+
$should_extract = "https://wordpress.org/latest.zip" == $url;
206+
211207
echo "<script>
212208
document.title = 'Transferring Done!';
213-
document.querySelector('#progress').innerHTML = '( 100.00% — " . human_filesize($filesize) . " / " . human_filesize($filesize) . " )<br><small style=\"text-transform: capitalize;\">Total Time: " . human_timing($time) . "</small>';
209+
document.querySelector('#progress').innerHTML = '( 100.00% — " . human_filesize($filesize) . " / " . human_filesize($filesize) . " )';
214210
document.querySelector('body').style.backgroundImage = 'linear-gradient(to right, rgba(0, 223, 56, 0.18) $progress%, white $progress%)';
215-
document.querySelector('h1').innerHTML += '<div style=\"font-size: 0;margin-bottom: 3rem;\"><small style=\"font-size: 1rem;\">✅ Transferring Done! 👍</small></div>';
216-
document.querySelector('h1').innerHTML += '<div style=\"font-size: 0;margin-bottom: 3rem;\"><small style=\"font-size: 1rem;\"><a href=\"?r=" . time() . "\" style=\"background: #0060df;color: white;text-decoration: none; padding: 0.5rem 2rem;border: none;border-radius: 3px;box-shadow: 0 0 8px -3px #00000069;margin: 0 1rem;cursor: pointer;\">Upload another file</a></small></div>';
211+
document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Transferring done successfully in ".human_timing($time).($should_extract ? ", wait ..." : ".")."</small></div>';
217212
</script>";
218-
ob_end_flush();
213+
ob_end_flush(); ob_flush(); flush();
214+
215+
if ($should_extract) {
216+
217+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⏳ PLEASE WAIT, Extracting Wordpress ZIP archive ...</small></div>';</script>";
218+
ob_end_flush(); ob_flush(); flush(); ob_start();
219+
220+
$rootDir = __DIR__;
221+
$zipFile = $path_full;
222+
$tempDir = $rootDir . '/temp/wordpress';
223+
$continue = false;
224+
$zip = new ZipArchive;
225+
if ($zip->open($zipFile) === TRUE) {
226+
$zip->extractTo($rootDir . '/temp');
227+
$zip->close();
228+
$continue = true;
229+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ WordPress ZIP file extracted successfully.</small></div>';</script>";
230+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⏳ PLEASE WAIT, Moving wordpress files & folders to root ...</small></div>';</script>";
231+
ob_end_flush(); ob_flush(); flush(); ob_start();
232+
} else {
233+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⛔️ Error: Failed to extract ZIP file ({$local}).</small></div>';</script>";
234+
ob_end_flush(); ob_flush(); flush(); ob_start();
235+
}
236+
if ($continue) {
237+
238+
if (!is_dir($tempDir)) {
239+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⛔️ Error: 'wordpress' folder not found in the ZIP file.</small></div>';</script>";
240+
ob_end_flush(); ob_flush(); flush(); ob_start();
241+
}
242+
// Step 3: Move files from the 'wordpress' folder to the root directory
243+
$files = scandir($tempDir); $jf = 0; $jfl = 0;
244+
foreach ($files as $file) {
245+
if ($file !== '.' && $file !== '..') {
246+
$source = $tempDir . '/' . $file;
247+
$destination = $rootDir . '/' . $file;
248+
// Move the file or directory
249+
if (is_dir($source)) {
250+
$jfl++; rename($source, $destination);
251+
} else {
252+
$jf++; rename($source, $destination);
253+
}
254+
}
255+
}
256+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ <strong>".number_format($jf)."</strong> Files & <strong>".number_format($jfl)."</strong> Folders moved successfully to the root directory.</small></div>';</script>";
257+
ob_end_flush(); ob_flush(); flush(); ob_start();
258+
259+
// Step 4: Remove the temporary folder
260+
$tempPath = $rootDir . '/temp';
261+
if (is_dir($tempPath)) {
262+
if (rmdir($tempPath)) {
263+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Temporary 'temp' folder removed successfully.</small></div>';</script>";
264+
ob_end_flush(); ob_flush(); flush(); ob_start();
265+
} else {
266+
// Fallback to recursive deletion if the folder isn't empty
267+
rmdir_recursive($tempPath);
268+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Temporary 'temp' folder removed recursively.</small></div>';</script>";
269+
ob_end_flush(); ob_flush(); flush(); ob_start();
270+
}
271+
}else{
272+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⛔️ Could not remove Temporary 'temp' folder.</small></div>';</script>";
273+
ob_end_flush(); ob_flush(); flush(); ob_start();
274+
}
275+
276+
// Step 5: Optionally, delete the ZIP file
277+
if (file_exists($zipFile)) {
278+
@unlink($zipFile);
279+
@unlink($folder . "/license.txt");
280+
@unlink($folder . "/readme.html");
281+
@unlink($folder . "/xmlrpc.php");
282+
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Uploaded ZIP file deleted, extra WordPress files deleted.</small></div>';</script>";
283+
ob_end_flush(); ob_flush(); flush(); ob_start();
284+
}
285+
}
286+
287+
}
288+
ob_end_flush();ob_flush(); flush();
289+
echo "<script>
290+
document.querySelector('h1').innerHTML += `
291+
<div style=\"font-size: 0;margin: 2rem; padding:1rem;\">
292+
<small style=\"font-size: 1rem;\">
293+
<a href=\"?r=" . time() . "\" style=\"background: #0060df;color: white;text-decoration: none; padding: 0.5rem 2rem;border: none;border-radius: 3px;box-shadow: 0 0 8px -3px #00000069;margin: 0 1rem;cursor: pointer;\">Upload another file</a>
294+
<a href=\"?delete=true\" style=\"background:#df0025;color: white;text-decoration: none; padding: 0.5rem 2rem;border: none;border-radius: 3px;box-shadow: 0 0 8px -3px #00000069;margin: 0 1rem;cursor: pointer;\">Self Destruct?</a>
295+
</small>
296+
</div>`;</script>";
297+
echo "all done ./";
219298
exit;
220299
}
221300
?>
@@ -276,6 +355,14 @@ function human_timing($time) {
276355
$s -= $m * 60;
277356
return ($h > 0 ? "$h:" : "") . sprintf('%02d', $m) . ':' . sprintf('%02d', $s);
278357
}
358+
function rmdir_recursive($dir) {
359+
$files = array_diff(scandir($dir), ['.', '..']);
360+
foreach ($files as $file) {
361+
$path = $dir . '/' . $file;
362+
is_dir($path) ? rmdir_recursive($path) : unlink($path);
363+
}
364+
return rmdir($dir);
365+
}
279366
/*##################################################
280367
Lead Developer: [amirhp-com](https://amirhp.com/)
281368
##################################################*/

0 commit comments

Comments
 (0)