如何在两个文件中使用更改变量

时间:2015-01-13 12:28:38

标签: php global-variables

所以我有一个像$mapname = date('YmdHis');这样的变量,这个变量每秒都会改变..我想在文件中使用它但问题是日期每秒都会改变,而且两个不同的文件不执行它们的脚本同时..

因此,当我提交表单时,我会收到两个文件夹,例如:20150113132538和20150113132547.脚本会检查是否存在基于$ mapname的文件夹。但是因为一个脚本比另一个脚本慢(取决于你上传文件的上传速度),它会创建一个新文件夹,因为该文件夹不存在。

任何人都有解决方案吗?


以下是两个脚本中的代码,要求date.php基本上将此$mapname = date('YmdHis');放在文件中

此脚本调用此脚本:

<script type="text/javascript">
            function SaveIt() {
        event.preventDefault();
        var myDrawing = document.getElementById("simple_sketch");
        var drawingString = myDrawing.toDataURL("image/png");
        var postData = "canvasData="+drawingString;
        var ajax = new XMLHttpRequest();
        ajax.open("POST",'saveimage.php',true);
        ajax.setRequestHeader('Content-Type', 'canvas/upload');
        ajax.onreadystatechange=function()
            {
                if (ajax.readyState == 4)
                {alert("image saved");}
            }
            ajax.send(postData);
    }
    </script>

此代码在此处打开saveimage.php

<?php
$upload_dir = "uploads/";

require_once "date.php";

if (!file_exists($upload_dir.$mapname)) {
    mkdir("uploads/".$mapname, 0777, true);
    }

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
    $rawImage=$GLOBALS['HTTP_RAW_POST_DATA'];
    $removeHeaders=substr($rawImage, strpos($rawImage, ",")+1);
    $decode=base64_decode($removeHeaders);
    $fopen = fopen( 'uploads/'.$mapname.'/test.jpg', 'wb' );
    fwrite( $fopen, $decode);
    fclose( $fopen );
}
?>

^这是上传我的(草图)画布的脚本。

require_once "date.php";

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    $path = "" . $upload_dir . "/" . $mapname . "/"; // Upload directory

    // Loop $_FILES to execute all files
    foreach ($_FILES['files']['name'] as $f => $name) {
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }

        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            elseif(!$_POST["fancier"]){
                $message[] = "Please fill in your name.";
                continue;
            }           
            else{ // No error found! Move uploaded files
                if (!file_exists($path)) {
                    mkdir($path, 0777, true);
                }
                if (file_exists($path.$name)) {
                    $path_parts = pathinfo($name);
                    $name = $path_parts['filename'] . "_" . date('ynjGis') . "." . $path_parts['extension'];
                }
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) {
                    $count++; // Number of successfully uploaded files
                }
            }
        }
    }
}

^这会上传多张图片。

图像大小比画布大,因此画布脚本在1/2秒内加载,而上传大约需要9/10秒。因此根据当前文件创建两个不同的文件夹日期。

1 个答案:

答案 0 :(得分:1)

在您在浏览器中访问的脚本中设置$GLOBALS['mapname'],并在其他脚本中使用它,我假设您在第一个脚本中使用requireinclude

变量将在整个请求中保留其内容。您甚至可以使用常量来防止覆盖它:

if(!defined('MAPNAME')) {
    define('MAPNAME', date('YmdHis'));
}