Kemal

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

Recommended Posts

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? 

 

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By hellomoto
      I have web hosting with the following .htaccess en root, to point it to a subdirectory "audino.us", wherein I have PW installed:
      RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?something.com$ RewriteCond %{REQUEST_URI} !^/something.com/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /audino.us/$1 RewriteCond %{HTTP_HOST} ^(www.)?something.com$ RewriteRule ^(/)?$ something.com/index.php [L] RewriteCond %{HTTP_HOST} ^(www.)?something.com$ RewriteRule ^(/)?$ something.com [L] However when I go to audino.us/pw to access the admin, it becomes audino.us/audino.us/pw, and doesn't allow me to log in. How do I render the above to be recursive (?), i.e., to apply to all subordinate URLs? Is this to be done within the .htaccess in /audino.us, or in the root .htaccess above? Please help, I can't log in. Thanks much.
    • By Tony Carnell
      Hi all,
       
      I'm just looking for some advice really.
      We've got a current website in the root of our hosting account and have been developing a new Processwire powered one in a sub-directory.
      We're now getting close to setting the new site live and I'm wondering which is best; leave the new site in the sub-directory and modify the .htaccess file to point to it, or remove the existing site and move the new one across to the root.
      We're also looking to install Piwik in its own folder too, together with the Piwik Analytics module. Would this work if the site remains in the sub-directory, because Piwik would effectively be in a sub-directory off a sub-directory?
      Basically it comes down to which is the more satisfactory option, leave it where it is versus move it to the root.
      If it stays in the sub-directory, I'll want to change the .htaccess file to point at that location, but also hide the sub-directory so it looks as if it's running from the root, e.g. the site is running from www.example.com/cms/ but the visitor never sees the /cms/ in the urls.
      Thanks in advance for any advice.
      Tony.
    • By gesp
      Hi there,
      Is there anyway in processwire to output the URL of the site, instead of the root. Because the root returns this value: "/" and I need it to return the URL.
      Thanks in advance
    • By photoman355
      I have a slightly unusual setup where I'm using the first child of the root as my homepage like so:   Root       Home       Page 2       Page 3        etc   This is presenting me with a slight problem in that I need the Home url in the navigation tree to redirect to Root.  Here's my navigation setup:   <?php $root = $pages->get("/"); $children = $root->children(); foreach($children as $child) { echo "<li><a href='#{$child->name}'></a><a href='{$child->url}'><h1>{$child->title}</h1></a></li>"; } ?> I'm sure there's a nice easy way to do this in PW, can anyone help?
      For data setup I have Home rendered in the Root page.  Looking at SEO would simply changing the menu structure be enough to stop robots indexing the root/Home url so that Home is seen as Root?  
    • By onjegolders
      Title says it all really? Any idea why config->urls->root would display nothing? I'm guessing it may have to do with my config file but I've never seen this issue before...
      Thanks