I'll be adding this to jQuery pretty quick, but put this here as I improve (add form fields, etc).
So when called by AJAX/POST, this will upload a .torrent from browse, and automatically parse & after parsing passes, inserts the data to the database.
Same idea as torrents-upload.php but not at all. Similar in some ways in terms of the parsing routines. This is a very good PHP class, and should be used by my opinion.
Works really well for me, and has worked on every torrent file I've thrown at it so far.
Testers
Send a file POST request to /ajax/ajax_ajax_torrent_upload.php
[color=rgb(226, 80, 65)]<form id="form-torupload"[/color] will call jQuery and make the request. Otherwise, will post through form.
[color=rgb(226, 80, 65)]<div id="preview-torrent"></div>[/color] will return the message response (data : success, else .. ) via FadeIn(), FadeOut() using set intervals.
[color=rgb(226, 80, 65)]<div id="err"></div>[/color] will return the POST response (data : success, else .. ) via FadeIn(), FadeOut() using set intervals.
Instructions
Create as /ajax/ajax_torrent_upload.php
(jQuery) add to header.php
Upload parse.class.php to /ajax/
Now in full working state, I will begin to push to jQuery and rework code to PDO.
** Note: Due to lack of <form> fully designed yet, things like category and language are at a default value of 1
In action on my end
[ATTACH type="full" alt="Tooltip.png"]5516[/ATTACH]
Green button triggers hidden file input that brings up the browse, and upon "onchange" the torrent is uploaded and torrent appears in same moment on homepage no refresh. Lots of things you can do with it. This is using Tooltipsy plugin.
So when called by AJAX/POST, this will upload a .torrent from browse, and automatically parse & after parsing passes, inserts the data to the database.
Same idea as torrents-upload.php but not at all. Similar in some ways in terms of the parsing routines. This is a very good PHP class, and should be used by my opinion.
Works really well for me, and has worked on every torrent file I've thrown at it so far.
Testers
Send a file POST request to /ajax/ajax_ajax_torrent_upload.php
<form id="form-torupload" action="/ajax/ajax_torrent_upload.php" method="post" enctype="multipart/form-data">
<input type="file" accept=".torrent" name="torrent" />
<input type="hidden" name="userid" value="<?php echo $CURUSER["id"]; ?>" required />
</form>
<!-- Status -->
<div id="err"></div>
<!-- Feedback -->
<div id="preview-torrent"></div>
[color=rgb(226, 80, 65)]<form id="form-torupload"[/color] will call jQuery and make the request. Otherwise, will post through form.
[color=rgb(226, 80, 65)]<div id="preview-torrent"></div>[/color] will return the message response (data : success, else .. ) via FadeIn(), FadeOut() using set intervals.
[color=rgb(226, 80, 65)]<div id="err"></div>[/color] will return the POST response (data : success, else .. ) via FadeIn(), FadeOut() using set intervals.
Instructions
Create as /ajax/ajax_torrent_upload.php
<?php
/*
AJAX Torrent Uploading - Using Parsing class - Torrent()->object()
Replaces old torrents-upload.php and related classes: BDecode(), BEncode()
Triggered by AJAX call. Accepts .torrent, parses data and builds array, inserts to DB.
https://www.phpclasses.org/package/4896-PHP-Create-and-parse-Torrent-files.html
*/
error_reporting(E_ALL);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_FILES['torrent'])) {
$uploadedFile = $_FILES['torrent']['tmp_name'];
$dirPath = "../uploads/";
$ext = pathinfo($_FILES['torrent']['name'], PATHINFO_EXTENSION);
move_uploaded_file($uploadedFile, '../uploads/' . $_FILES["torrent"]['name']);
$dbHost = 'localhost';
$dbUsername = 'username';
$dbPassword = 'password';
$dbName = 'database';
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$userid = isset($_POST['userid']) ? intval($_POST['userid']) : 0;
$name = $_FILES["torrent"]['name'];
require('../backend/functions.php');
require('parse.class.php');
$torrent = new Torrent("../uploads/$name");
$torr_hash = $torrent->hash_info();
$torr_announce = $torrent->announce('http://tracker2.itzmx.com:6961/announce'); // Alternative tracker
$torr_size = $torrent->size();
$torr_saveas = $torrent->name();
$torr_private = $torrent->is_private() ? 'yes' : 'no';
// Prep for form fields - input type = text, name = "torrentname"
if (!empty($_POST['torrentname'])) {
$torr_name = $_POST['torrentname'];
} else {
$torr_name = str_replace('.torrent', '', $torrent->name());
}
$error = '';
// Check upload
if (!is_array($_FILES['torrent'])) {
$error = 'The file contains no array';
} elseif (empty($_FILES['torrent'])) {
$error = 'You did not select a torrent file';
}
// Proceed if no error
if ($error === '') {
$insert = $db->query("INSERT INTO torrents (filename, owner, name, descr, image1, image2, category, added, info_hash, size, numfiles, save_as, announce, external, nfo, torrentlang, anon, last_action)
VALUES ('" . $torr_saveas . "', " . sqlesc($userid) . ", '" . $torr_name . "', 'Torrent Description.', ''image1', 'image2', 1, '" . get_date_time() . "', '" . $torr_hash . "', '" . $torr_size . "', 3, '" . $torr_name . "', '" . $torr_announce . "', '" . $torr_private . "', 'no', 1, 'no', '" . get_date_time() . "')");
$edit_id = mysqli_insert_id($db);
mysqli_close($db);
header("Location: " . $site_config["SITEURL"] . "/torrents-edit.php?id=" . $edit_id);
die;
} else {
die($error);
}
} else {
echo 'Invalid request.';
}
?>
(jQuery) add to header.php
$(document).ready(function (e) {
$("#form-torupload").on('change', (function (e) {
e.preventDefault();
$.ajax({
url: "/ajax/ajax_torrent_upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
beforeSend: function () {
$("#preview-torrent").html("Uploading torrent...").delay(4000).fadeOut().delay(4000).fadeIn();
$("#preview-torrent").fadeIn();
$("#err").fadeOut();
},
success: function (data) {
if (data.redirect == true) {
// Redirect to torrent editor
window.location.href = '/torrents-edit.php?id=<?= mysqli_insert_id(); ?>';
}
if (data == 'invalid') {
// Invalid format
$("#err").html("Invalid torrent!").fadeIn();
} else {
$("#preview-torrent").fadeOut();
$("#updated-torrent").html("Torrent has been uploaded. Refresh to see all changes.").delay(1000).fadeIn().delay(2000).fadeOut();
}
},
error: function (e) {
$("#err").html(e).fadeIn();
}
});
}));
});
Upload parse.class.php to /ajax/
Now in full working state, I will begin to push to jQuery and rework code to PDO.
** Note: Due to lack of <form> fully designed yet, things like category and language are at a default value of 1
In action on my end
[ATTACH type="full" alt="Tooltip.png"]5516[/ATTACH]
Green button triggers hidden file input that brings up the browse, and upon "onchange" the torrent is uploaded and torrent appears in same moment on homepage no refresh. Lots of things you can do with it. This is using Tooltipsy plugin.