Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP在ApacheLocalServer中编写新文件_Javascript_Php_Jquery_Ajax_Html - Fatal编程技术网

Javascript PHP在ApacheLocalServer中编写新文件

Javascript PHP在ApacheLocalServer中编写新文件,javascript,php,jquery,ajax,html,Javascript,Php,Jquery,Ajax,Html,从已解析的前一个文件创建新文件时遇到问题。 我在javascript/jQuery中有这段代码(一段代码): 现在是PHP代码: <?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); funct

从已解析的前一个文件创建新文件时遇到问题。 我在
javascript/jQuery
中有这段代码(一段代码):

现在是
PHP
代码:

<?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://translate.google.es/?' . $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
$saveToFile = str_replace("$lang_from","$lang_to",$_file_srt); //substitution old language for the new one on the extension file

$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,$sub->text);
            fwrite($h,$space);
            fwrite($h,$space);
        }
        fclose($h);
    }
    exit('Data Saved.');
}
?>

您有权限写入该文件夹吗?不要忘记使用
可写
以确保您可以写入此文件。您是否收到“数据已保存”消息?尝试写入时是否出现任何错误?尝试退出(错误\u get\u last())@Legionar是的,当然,我正在用PHP代码将其他类型的文件保存在这些文件夹中(与发布的代码非常接近),一切都很好…完成了任何基本调试,如
var\u dump($save\u to\u file)
以查看生成的文件名/路径?你有很多假设,完全忽略了错误/无数据传入失败的可能性。你有权限写入该文件夹吗?别忘了使用
is_writeable
,以确保你可以写入该文件。你收到“数据已保存”消息了吗?尝试写入时是否出现任何错误?尝试退出(错误\u get\u last())@Legionar是的,当然,我正在用PHP代码将其他类型的文件保存在这些文件夹中(与发布的代码非常接近),一切都很好…完成了任何基本调试,如
var\u dump($save\u to\u file)
以查看生成的文件名/路径?这里有很多假设,完全忽略了错误/无数据传入失败的可能性。
<?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://translate.google.es/?' . $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
$saveToFile = str_replace("$lang_from","$lang_to",$_file_srt); //substitution old language for the new one on the extension file

$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,$sub->text);
            fwrite($h,$space);
            fwrite($h,$space);
        }
        fclose($h);
    }
    exit('Data Saved.');
}
?>