We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to view content of files on server?

I am trying to create an user's items listing page (somethings similar to dropbox website but a lot more basic, of course).

In normal PHP (without any framework), I can do it simply by using something like <a href="absolute path of item on server">Item Name</a> It will use browser function to display content of the item (if .mp3 browser play music, if .jpg browser show the picture...etc)

But I'm using Phalcon and I have to do in another way This is what I'm trying to do

public function indexAction() {

//This action will list all items of an user
//The most important line is

{{ link_to("/items/show" ~ item.id, item.name) }}

//When user click the item in index page, it will forward to showAction with the corresponding $item_id

}

public function showAction() {

//Hard things for me is here. I don't know how this showAction can actually show the item's content

}

How can I view the content in Phalcon? At least like what I could do with normal PHP?

Thank you very much

When posting code, please use proper Markdown syntax for legibility: https://forum.phalcon.io/help/markdown.

To answer your question, I don't think this is a Phalcon issue, but rather an .htaccess issue. My projects have 2 .htaccess files. One at the root of the application that forwards all requests to public/(whatever was requested). The .htaccess file in the public directory then looks like this:

# This redirects any non-real-file (ie: images, css, js) to index.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

So, any requests for actual files are not forwarded to index.php. So, if you put your files inside your public/ directory, just a plain link to the filename should work.

Thank you! But if now I want to place files in different folder (for example /var/media/), what I have to do? Because the .htaccess way is work with public folder, right?

Probably the easiest way would be to have a controller & action set up to handle this type of URL:

/file/download/someRandomFilename.pdf

The download action would then send headers pretending to be a PDF, then readfile the /var/media/someRandomFilename.pdf. The term I use for this sort of thing is "file download proxy"