1 year ago

#255417

test-img

Antoine

move_uploaded_file() file not found with rutorrent php script

For practical reasons, I created a form to send torrent files without having to go through ruTorrent (not accessible from the internet while my form is). I want to use ruTorrent's addtorrent.php script to launch the torrents in rTorrent. But I can't do it.

I don't understand why. I do a var_dump() of $_REQUEST and $_FILES on addtorrent.php with a die; right after to stop the script.

Here is the result when I use my script to call addtorrent.php

var_dump($_REQUEST)

array(1) { ["submit"]=> string(0) "" }

var_dump($_FILES)

array(1) { ["torrent_file"]=> array(5) { ["name"]=> array(1) { [0]=> string(86) "myfile.torrent" } ["type"]=> array(1) { [0]=> string(24) "application/x-bittorrent" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpZv2i4o" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(203516) } } }

and the result when I call addtorrent.php directly.

var_dump($_REQUEST)

array(1) { ["submit"]=> string(0) "" }

var_dump($_FILES)

array(1) { ["torrent_file"]=> array(5) { ["name"]=> array(1) { [0]=> string(86) "myfile.torrent" } ["type"]=> array(1) { [0]=> string(24) "application/x-bittorrent" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpviKVkV" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(203516) } } }

The data sent are the same. Except that when I use my script, the result is File not found.

My script (I know that some pieces of my script are not used. They are there for later, when I manage to upload my files) :

<?php
if(isset($_POST['submit'])){
    $currentDirectory = getcwd();
    //$whereUpload = $_POST['repertoire'];
    $errors = [];

    if (!empty($whereUpload)){
        $uploadDirectory = "/uploads/"/*. $whereUpload ."/"*/;
    } else {
        $uploadDirectory = "/uploads/";
    }

    $fileExtensionsAllowed = array('torrent');

    // Velidate if files exist
    if (!empty(array_filter($_FILES['torrent_file']['name']))) {

        // Loop through file items
        foreach($_FILES['torrent_file']['name'] as $id=>$val){

            $fileName = $_FILES['torrent_file']['name'][$id];
            $fileError = $_FILES['torrent_file']['error'][$id];
            $fileSize = $_FILES['torrent_file']['size'][$id];
            $fileTmpName  = $_FILES['torrent_file']['tmp_name'][$id];
            $fileType = $_FILES['torrent_file']['type'][$id];
            $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
            $uploadPath = $currentDirectory . $uploadDirectory . basename($fileName);
            $isDir = $currentDirectory . $uploadDirectory;

            if (!is_dir($isDir)) {
                mkdir($isDir, 0777, true);
            }

            // Check file extension
            if (!in_array($fileExtension,$fileExtensionsAllowed)) {
                $errors[] = "Extension non autorisée " . $fileName;
                $fileError = $fileError + 1;
            }

            // Check file true mime type
            $fi = new finfo(FILEINFO_MIME_TYPE);
            $mime = $fi->file($fileTmpName);

            if ($mime !== 'application/x-bittorrent'){
                $errors[] = "Type MIME incorrect " . $fileName;
                $fileError = $fileError + 1;
            }

            // Check file size (bit)
            if ($fileSize > 4000000) {
                $errors[] = "Fichier trop lourd (4 MO) " . $fileName;
                $fileError = $fileError + 1;
            }

            // Check if file exist
            if (file_exists($uploadPath)) {
                $errors[] = "Le fichier existe déjà " . $fileName;
                $fileError = $fileError + 1;
            }

            // If no error, upload
            if ($fileError < 1 ) {
                
                    require_once("/var/www/rutorrent/php/addtorrent.php");
            }
        }

        // Display all errors
        if (!empty($errors)) {
            if (sizeof($errors) == 1){
                echo "Erreur trouvé <br>";
            } else {
                echo "Erreurs trouvées <br>";
            }
            foreach ($errors as $error) {
                echo  $error . "<br>";
            }
        }
    }
}

addtorrent.php script

<?php

require_once( 'Snoopy.class.inc');
require_once( 'rtorrent.php' );
set_time_limit(0);

if(isset($_REQUEST['result']))
{
    if(isset($_REQUEST['json']))    
        cachedEcho( '{ "result" : "'.$_REQUEST['result'][0].'" }',"application/json");
    else
    {
        $js = '';
        foreach( $_REQUEST['result'] as $ndx=>$result )
            $js.= ('noty("'.(isset($_REQUEST['name'][$ndx]) ? addslashes(rawurldecode(htmlspecialchars($_REQUEST['name'][$ndx]))).' - ' : '').
                '"+theUILang.addTorrent'.$_REQUEST['result'][$ndx].
                ',"'.($_REQUEST['result'][$ndx]=='Success' ? 'success' : 'error').'");');
        cachedEcho($js,"text/html");
    }
}
else
{
    $uploaded_files = array();
    $label = null;
    if(isset($_REQUEST['label']))   
        $label = trim($_REQUEST['label']);
    $dir_edit = null;
    if(isset($_REQUEST['dir_edit']))
    {
        $dir_edit = trim($_REQUEST['dir_edit']);
        if((strlen($dir_edit)>0) && !rTorrentSettings::get()->correctDirectory($dir_edit))
            $uploaded_files = array( array( 'status' => "FailedDirectory" ) );
    }
    if(empty($uploaded_files))
    {
        if(isset($_FILES['torrent_file']))
        {
            if( is_array($_FILES['torrent_file']['name']) )
            {
                for ($i = 0; $i<count($_FILES['torrent_file']['name']); ++$i) 
                {
                                $files[] = array
                                (
                                    'name' => $_FILES['torrent_file']['name'][$i],
                                    'tmp_name' => $_FILES['torrent_file']['tmp_name'][$i],
                                );
                        }
            }
            else
                $files[] = $_FILES['torrent_file'];
            foreach( $files as $file )
            {
                $ufile = $file['name'];
                if(pathinfo($ufile,PATHINFO_EXTENSION)!="torrent")
                    $ufile.=".torrent";
                $ufile = getUniqueUploadedFilename($ufile);
                $ok = move_uploaded_file($file['tmp_name'],$ufile);
                $uploaded_files[] = array( 'name'=>$file['name'], 'file'=>$ufile, 'status'=>($ok ? "Success" : "Failed") );
            }
        }
        else
        {
            if(isset($_REQUEST['url']))
            {
                $url = trim($_REQUEST['url']);
                $uploaded_url = array( 'name'=>$url, 'status'=>"Failed" );
                if(strpos($url,"magnet:")===0)
                {
                    $uploaded_url['status'] = (rTorrent::sendMagnet($url,
                        !isset($_REQUEST['torrents_start_stopped']),
                        !isset($_REQUEST['not_add_path']),
                        $dir_edit,$label) ? "Success" : "Failed" );
                }
                else
                {
                    $cli = new Snoopy();
                    if(@$cli->fetchComplex($url) && $cli->status>=200 && $cli->status<300)
                    {
                            $name = $cli->get_filename();
                            if($name===false)
                            $name = md5($url).".torrent";
                        $name = getUniqueUploadedFilename($name);
                        $f = @fopen($name,"w");
                        if($f!==false)
                        {
                            @fwrite($f,$cli->results,strlen($cli->results));
                            fclose($f);
                            $uploaded_url['file'] = $name;
                            $uploaded_url['status'] = "Success";
                        }
                    }
                    else
                        $uploaded_url['status'] = "FailedURL";
                }   
                $uploaded_files[] = $uploaded_url;
            }
        }
    }
    $location = "Location: //".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/addtorrent.php?";
    if(empty($uploaded_files))
        $uploaded_files = array( array( 'status' => "Failed" ) );
    foreach($uploaded_files as &$file)
    {
        if( ($file['status']=='Success') && isset($file['file']) )
        {
            $file['file'] = realpath($file['file']);
            @chmod($file['file'],$profileMask & 0666);
            $torrent = new Torrent($file['file']);
            if($torrent->errors())
            {
                @unlink($file['file']);
                $file['status'] = "FailedFile";
            }
            else
            {
                if(isset($_REQUEST['randomize_hash']))
                    $torrent->info['unique'] = uniqid("rutorrent-",true);
                if(rTorrent::sendTorrent($torrent,
                    !isset($_REQUEST['torrents_start_stopped']),
                    !isset($_REQUEST['not_add_path']),
                    $dir_edit,$label,$saveUploadedTorrents,isset($_REQUEST['fast_resume']))===false)
                {
                    @unlink($file['file']);
                    $file['status'] = "Failed";
                }
            }
        }
        $location.=('result[]='.$file['status'].'&');
        if( isset($file['name']) )
            $location.=('name[]='.rawurlencode($file['name']).'&');
    }
    header("HTTP/1.0 302 Moved Temporarily");
    if(isset($_REQUEST['json']))
        $location.='json=1';
    header($location);
}

Any ideas?

php

file-upload

move-uploaded-file

0 Answers

Your Answer

Accepted video resources