Jump to content

upload txt file to root folder and access it from xxx.com/ex.txt


Kemal
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
 Share

×
×
  • Create New...