Jump to content

Recommended Posts

Posted

Hi,

Probably easy question but i want to learn all the ways to do it.

i uploaded ads.txt file to www.xxx.com/site/templates/ folder!

now i want to open it from www.xxx.com/ads.txt

how can i do it? 

 

 

Posted

Without touching rewrites or anything:

<?php namespace ProcessWire;

if ($input->url === '/ads.txt') {
    $file = $config->paths->templates . 'ads.txt';
    if (file_exists($file)) {
        readfile($file);
        exit;   
    }
}

Put this in site/init.php (create if it doesn't exists)

If you want to force download, use this one instead

if ($input->url === '/ads.txt') {
    $file = $config->paths->templates . 'ads.txt';
    if (file_exists($file)) $files->send($file);
}

 

  • Like 1
Posted

if you tell us why you did this we might suggest another solution ;)

for example you could create a page with a file field. then you would have an easy to use file uploader with all the other pw features like access control and so on and you could access the file via it's url ( /site/assets/files/1234/your-file.txt ).

that might or might not be a better solution depending on your usecase...

  • Like 2
Posted
49 minutes ago, bernhard said:

for example you could create a page with a file field. then you would have an easy to use file uploader with all the other pw features like access control and so on and you could access the file via it's url ( /site/assets/files/1234/your-file.txt ).

Good idea :)

Extending my answer above, you can create a page with a separate template and a file field, and set any role restrictions you want. Then with the help of some regex magic you can match any mysite.com/filename.ext url and serve files added to the page.

<?php namespace ProcessWire;

// match any url that ends with /name.ext
if (preg_match('#/([^\/]+\.[^\.\/]+)$#', $input->url, $matches)) {
    // Respect page access settings
    $filesPage = $pages->findOne('template=files');
  
    if ($filesPage->id) {
        // page's files folder under site/assets/files/<pageid>
        $pageFilesDir = $filesPage->files->url;
        $filePath = $pageFilesDir . $matches[1];
      
        if (file_exists($filePath)) {
            readfile($file);
            exit;
        }
    }
}

 

  • Like 1
Posted

One more option (and to state the obvious): you can simply upload ads.txt to the site root and then access it at www.xxx.com/ads.txt like you would expect.

  • Like 1
  • Haha 1
Posted
Just now, Robin S said:

One more option (and to state the obvious): you can simply upload ads.txt to the site root and then access it at www.xxx.com/ads.txt like you would expect.

That would be too easy :)

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...