如果JSON为空,则重定向到网页

时间:2016-03-02 21:38:00

标签: php json

如果我不清楚这一点,请原谅我,但这就是我想要完成的事情。我有两个php页面,每个页面在提交每个表单时生成一个单独的json文件。我想要做的是在填写second.php之前要求填写main.php。因此,如果用户在填写main.php之前转到second.php,它会将它们重定向到main.php。所以我需要second.php来"搜索"对于main.json文件的路径,如果为空,则重定向回main.php。以下是生成main.json的代码的一部分。 json文件保存在我的网站上。

    require_once $this->site_path . 'inc' . DIRECTORY_SEPARATOR . 'json_file.class.php';
    $jsonFile = new JsonFile();
    $json_result = $jsonFile->writeFile(
            array(
                'file_name'     => $this->site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $file_data['username'] . DIRECTORY_SEPARATOR . 'main.json',//$this->json_path . DIRECTORY_SEPARATOR . $file_data['username']. '_'. 'main.json',
                'data'          => $obj
            )
    );

    $result['json_file'] = $json_result;

    // Save the uploaded file
    if (!empty($file_data['profilebackground']['size'])) {
  $target = $this->site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $file_data['username']. DIRECTORY_SEPARATOR . $file_name;
        if (!move_uploaded_file($file_data['profilebackground']['tmp_name'], $target)) {
            $result['image_file'] = 'There was an error saving the home page image. Please check the image directory permissions and try again.';
            error_log('File ' . $file_data['profilebackground']['name'] . ' not saved: check the image directory permissions.');
        } else {
    include_once("php_img_lib.php");
    img_resize($target, $target, 1000, 1000, $ext);
            $result['image_file'] = 'The home page image was successfully uploaded.';
        }
    } else {
    //  $result['image_file'] = 'There was an error uploading the home page image. Please try again.';
    //  error_log('File ' . $file_data['profilebackground']['name'] . ' not uploaded.');
    }

    return $result;
}

public function getmainData($username) {
    $albums_json_file = $this->site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $username . DIRECTORY_SEPARATOR . 'main.json';//$this->json_path . DIRECTORY_SEPARATOR . $username . '_'. 'main.json';
    try {
        $handle = @fopen($albums_json_file, "rb");
        if (!empty($handle)) {
            $albums_json_file_contents = fread($handle, filesize($albums_json_file));
            fclose($handle);
        }

        if (!empty($albums_json_file_contents)) {
            $albums_json_file_contents = json_decode($albums_json_file_contents);
        } else {
            $albums_json_file_contents = array();
        }

        if (!empty($albums_json_file_contents->main[0])) {
            return $albums_json_file_contents->main[0];
        } else {
            return $albums_json_file_contents;
        }
    } catch (Exception $e) {
        error_log($e->getMessage());
        $albums_json_file_contents = array();
        return $albums_json_file_contents;
    }
}

1 个答案:

答案 0 :(得分:0)

感谢您的所有帮助。我想通了,这真的很容易。我打电话给创建main.json文件的php并检索数据。由于它是空的,它重定向很好。我不确定为什么它在“其他”部分工作而不是“如果”部分但是嘿。它有效。

OrdersController