Asked  8 Months ago    Answers:  5   Viewed   28 times

If it's Path_To_DocumentRoot/a/b/c.php,should always be /a/b.

I use this:

dirname($_SERVER["PHP_SELF"])

But it won't work when it's included by another file in a different directory.

EDIT

I need a relative path to document root .It's used in web application.

I find there is another question with the same problem,but no accepted answer yet.

PHP - Convert File system path to URL

 Answers

36

Do you have access to $_SERVER['SCRIPT_NAME']? If you do, doing:

dirname($_SERVER['SCRIPT_NAME']);

Should work. Otherwise do this:

In PHP < 5.3:

substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']));

Or PHP >= 5.3:

substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT']));

You might need to realpath() and str_replace() all to / to make it fully portable, like this:

substr(str_replace('\', '/', realpath(dirname(__FILE__))), strlen(str_replace('\', '/', realpath($_SERVER['DOCUMENT_ROOT']))));
Wednesday, March 31, 2021
 
SilverHorn
answered 8 Months ago
16

Try to use RecursiveIteratorIterator in combination with RecursiveDirectoryIterator

$path = realpath('/path/you/want/to/search/in');

$objects = new RecursiveIteratorIterator(
               new RecursiveDirectoryIterator($path), 
               RecursiveIteratorIterator::SELF_FIRST);

foreach($objects as $name => $object){
    if($object->getFilename() === 'work.txt') {
        echo $object->getPathname();
    }
}

Additional reading:

  • http://www.phpro.org/tutorials/Introduction-to-SPL.html
Wednesday, March 31, 2021
 
drowneath
answered 8 Months ago
13

This is going to get the first 10 pages of data. I limited it to 10 to prevent it from eating my quota. If you remove that it will continue to chug away getting all the rows and eating up your quota.

NOTE: This is only going to work if you have a single report. If you have more then one report then you are going to have to do some fancy stuff to apply the correct pageToken to each report and remove the reports which are complete. At this time there is no report Id or way of matching up what you sent to the report that is returned other than its place in the reports array.

$service = new Google_Service_AnalyticsReporting($client); 

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2016-01-01");
$dateRange->setEndDate("2017-06-30");

// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");

//Create the Dimensions object.
$date = new Google_Service_AnalyticsReporting_Dimension();
$date->setName("ga:date");
$pagePath = new Google_Service_AnalyticsReporting_Dimension();
$pagePath->setName("ga:pagePath");

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("81692014");
$request->setPageSize("10000");  
$request->setDateRanges($dateRange);
$request->setDimensions(array($date,$pagePath));
$request->setMetrics(array($sessions));
$request->setMetrics(array($sessions));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$data =  $service->reports->batchGet( $body );


// Remove count if you really want everything.
$cnt = 0; 
while ($data->reports[0]->nextPageToken > 0 && $cnt < 10) {
    // There are more rows for this report.
    $body->reportRequests[0]->setPageToken($data->reports[0]->nextPageToken);
    $data =  $service->reports->batchGet( $body );
    $cnt++;
    }

I put a tutorial up on it Google Analytics V4 pagination and the full code i used can be found on GitHub note this code was ripped from my sample project.

Wednesday, March 31, 2021
 
Tak
answered 8 Months ago
Tak
42

Assuming that you use the DATETIME-format as datatype in your database this should help you:

$start =  date("h:i:s", strtotime($this->input->post('start')));
$end =  date("h:i:s", strtotime($this->input->post('end')));
...
$sql = "SELECT materialID FROM schedule WHERE date_reserve = ? AND TIME(start_time) >= ? AND TIME(end_time) <= ?";

Edit: So if you use TIME-format you only have to change the format of the values you are inserting:

$start =  date("h:i:s", strtotime($this->input->post('start')));
$end =  date("h:i:s", strtotime($this->input->post('end')));
Saturday, May 29, 2021
 
Hilmi
answered 5 Months ago
36

When a webpage is requested from a server. The server looks at the path (i.e. example.com/this/is/a/path) to figure out which file to serve and from where.

As I understand it, what you want to do is have your index.php handle all the requests. The way you would do this to use URL Rewriting.

Assuming you are using an apache web server, you can use something called *mod_rewrite* to do this. See more on mod_rewrite here.

For the specific rules to use, you probably want to use something like the code in V_K's answer.

Saturday, May 29, 2021
 
linjuming
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :