jeudi 5 novembre 2015

PHP write new file in apache localserver

Vote count: 0

I'm having problems to create a new file from a parsed previous one. I have this code in javascript/jQuery (a piece of it):

$('#button').live('click', function (e) {
        var file_name_srt_URL = 'folder_one/' + file_name_
        translatedLanguage = $('#input_text').val();
        var lang_from = shortLanguage(originalLanguage);//it's simple function, does something like: originalLanguage = english -> return 'en'
        var lang_to = shortLanguage(translatedLanguage);
        var jsonNameFile = JSON.stringify(file_name_srt_URL);
        var jsonLangFrom = JSON.stringify(lang_from);
        var jsonLangTo = JSON.stringify(lang_to);

        $.ajax({
            type: "POST",
            url: "traduction.php",
            data: {data: jsonNameFile, langFrom: jsonLangFrom, langTo: jsonLangTo}, 
            cache: false,

            success: function(){
                alert("Success!");
                hideMsgBox();
            }
        });
    });

And now, the PHP Code:

<?php
define('SRT_STATE_SUBNUMBER', 0);
define('SRT_STATE_TIME',      1);
define('SRT_STATE_TEXT',      2);
define('SRT_STATE_BLANK',     3);

libxml_use_internal_errors(true);
function translate($text, $from, $to){
    //fake user-agent
    ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
    ini_set('default_charset', 'utf-8');//set encoding in case it's other than utf-8 (you might need to re-set it afterwords)
    $get_string = 'hl=' . $from . '&tl=' . $to . '&q=' . urlencode($text);
    $data = file_get_contents('http://ift.tt/vdS1S2?' . $get_string);
    $DOM = new DOMDocument;
    $DOM->loadHTML($data);
    $items = $DOM->getElementById('result_box');
    return $items->nodeValue;
}
$file_name = json_decode($_POST['data']); //original filename route 
$lang_from = json_decode($_POST['langFrom']); //original language
$lang_to = json_decode($_POST['langTo']); //final language to translate

$url_to = 'new/'; //directory to save the new file
$_file_srt = str_replace("folder_1/", "$url_to", $file_name); //substitution Old Directory for the New one
$new_file_srt = str_replace("$lang_from","$lang_to",$_file_srt); //substitution old language for the new one on the extension file
$saveToFile = $url_to . $new_file_srt; //final name: URL + Filename

$subs    = array();
$state   = SRT_STATE_SUBNUMBER;
$subNum  = 0;
$subText = '';
$subTime = '';
$space = "\n";
$blank = " ";
$arrow = " --> ";

$lines   = file($file_name); //read file
foreach($lines as $line) {
    switch($state) {
        case SRT_STATE_SUBNUMBER:
            $subNum = trim($line);
            $state  = SRT_STATE_TIME;
            break;

        case SRT_STATE_TIME:
            $subTime = trim($line);
            $state   = SRT_STATE_TEXT;
            break;

        case SRT_STATE_TEXT:
            if (trim($line) == '') {
                $sub = new stdClass;
                $sub->number = $subNum;
                list($sub->start, $sub->end) = explode(' --> ', $subTime);
                $sub->text   = $subText;
                $subText     = '';
                $state       = SRT_STATE_SUBNUMBER;

                $subs[]      = $sub;
            } else {
                $subText .= $line;
            }
            break;
    }
}

if (isset($file_name))
{
    $h = fopen($saveToFile, 'w+');
    if ($h) {
        foreach ($subs as $sub) {
            fwrite($h,$sub->number);
            fwrite($h,$space);
            fwrite($h,$sub->start);
            fwrite($h,$arrow);
            fwrite($h,$sub->end);
            fwrite($h,$space);
            //fwrite($h,translate($sub->text,$lang_from,$lang_to));
            fwrite($h,$sub->text);
            fwrite($h,$space);
            //fwrite($h,$space);
        }
        fclose($h);
    }
    exit('Data Saved.');
}
?>

My problem is no file is created at any directory, I don't know why, because I did this things before with other files, but now I can't find the problem!

Any one can help me? Thank you!

asked 29 secs ago
Sergi
104

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



PHP write new file in apache localserver

Aucun commentaire:

Enregistrer un commentaire