getRelativePath from file name ?

Permalink
Please help, how to get file's relative path just from its name.

 
Mnkras replied on at Permalink Reply
Mnkras
such as....
can you give an example?
eOne replied on at Permalink Reply
I have string value of file's name "img.jpg".

I cannot use getRelativePath() as it reports it's not an object.

I would need something like getRelativePathByName("img.jpg") or,
a way to get FileID from file's name.

Then relativePath wouldn't be problem.
teknotica replied on at Permalink Reply
teknotica
Hi eOne,

I'm not sure if this is what you are looking for but normally what I do is something like this:

$f = Loader::helper('concrete/file');
$fvFilename = $f->getFileRelativePath($fvPrefix, $fvFilename);


As you can notice, you must have the file prefix value which can be found through a query or through the file object, supposing you got already the file ID.

Hope it helps!
eOne replied on at Permalink Reply
I don't have fileID.
If I did, filePath would be easy to get.

What is file prefix and how it can be used ?


Problems are simple as this:
We have file named "img.jpg" uploaded through file manager; all we know is it's name.

a) How to find relative path ( or any path ) just by using it's name?
b) get fileObject or fileID from file name?
elyon replied on at Permalink Reply
elyon
Would you mind if I asked how the file is being uploaded, and what the file is being used for?

If the file needs to be uploaded before it is used, you can integrate with the file manager, and it will tell you what you need to know after you upload it.

If the file has already been uploaded, you should probably integrate with the file manager as well so the user can choose the graphic they want, and again, it will give you all the details you need
eOne replied on at Permalink Reply
file (thumbnail) is being uploaded via filemanager. It has the same name as large image but prefix tn_
Plan is to use it in gallery module when I need custom thumbs and not generated one.

Anyways ... for me it is logical to look for images or files by Name, so I thought it had to be there already :)
teknotica replied on at Permalink Reply
teknotica
Hi!

I'm not sure why you are just having a "name" after uploading a file using the file manager.


Once you upload a file you can manage it using the file helper (as I said on my first message) and get all type of information.


I would recommend you to take a look at the "file" block of Concrete, specially the view and controller, and how you can manipulate all the file information.


Other solution, specially if you are doing all this in your own block, is to create your own function in your controller with a query that looks up for the file prefix based on file name (look at the 'FileVersions' table). Once you got your file prefix, you can get the relative path using the function I wrote in my last message (don't forget your validations in your function)


PS.- the prefix is the name of the folder where your file is (it's new in latest Concrete versions and I think was to tidy up the file folder)


Hope it helps!
eOne replied on at Permalink Reply
I've added this function into concrete/module/file.php

public function getRelativePathFromName($fName) {
 Loader::model('file_set');
 $db = Loader::db();
 $result = $db->GetRow("SELECT FileVersions.fID FROM Files LEFT JOIN FileVersions on Files.fID = FileVersions.fID and FileVersions.fvIsApproved = 1
      WHERE FileVersions.fvFilename = ?", $fName);
 $path = $this->getRelativePathFromID($result[0]);
      return $path;
}


It is simple to call it but somehow it is still "half solution" to me.
elyon replied on at Permalink Reply
elyon
Oh, I get it, so you are adding a thumbnail, so you have access to those details, but you want to get the details for it's companion?

A real problem with this is that you can upload an unlimited number of files with the same original name.

Here's a couple ideas:

1. If you're writing a custom block, you can let the user choose the thumbnail and the large image separately using the file manager integration.

2. I think the file manager API contains some way of setting the thumbnail for a file, so that if you did it right, when you grab a file you would have up to three thumbnail images available. It might be possible to hook into that when you make your upload so that you have only one file to load in the end.
eOne replied on at Permalink Reply
actually, I am customizing gallery module I've bought from C5.

I need to have custom made thumbs for these images and that is a problem.

I thought to put just images in gallery, and as I have their names, just fetch tn_* for each and that's it.
Didn't know that would turn into such fuss :)

From your ideas, I think the best and easiest option would be to select custom thumbnail for large image after upload but somehow I missed that option.
If you please can point me how to use it - I would be grateful :)
elyon replied on at Permalink Reply
elyon
Hey eOne,

I apologize that I was confused about what you were really trying to do.

I think the easiest way to solve this problem would be to use a custom template for the gallery block. I would work out fine depending upon what your alternate thumbnails look like.

If you are reusing a set of standard graphics or icons, you can set up your custom template to display the icons you need, and you can then map the images in the gallery to each one of your icons based upon their order.

If you need to use the original graphics, but you would like to make a change to them, you may be able to accomplish this in your template by putting additional graphics behind, around or on top of the original thumbnail.

Another approach would be to merge some of the code from the gallery block into a custom template for the Image block. This probably sounds a little weird, but hear me out.

The image block lets you select an image as well as an on-state image. That's two images. By creating a custom template, you can use the first image as the thumbnail graphic, then instead of displaying the second graphic as an on-state, you can open it in the lightbox as the large image. This might work really well, the only downside is that you may lose the next/previous functionality in the lightbox.

If creating a custom template like this really won't work, you may need to create your own block. Modify the gallery block so that it accepts two graphics as its parameters instead of only one.

Or if you really need them linked together, maybe there is a way (I haven't seen the Javascript) to have separate Image blocks with a custom template, as I mentioned before, but to design the template in a way that all of the images on the same page will link together as one gallery?

I know that it seems frustrating that there is no built in method to search by path, but I personally believe it is a good long-term design. If you go to the file manager you can replace any file with a new version ... the new version may well have an entirely different file name. There might also be an unlimited amount of files with the same name in the file manager. That's why referencing a file ID and receiving an object with the path, relative path, absolute path, thumbnail path, etc. is a lot more powerful for 99.8% of situations. That just leaves the rare situation where you might just get really technical about it.
eOne replied on at Permalink Reply
hi elyon :)
thank you for your solutions but I've found easiest to achieve in the way I wrote before.

I added new function to file.php module.
That way I can easy pass filename and get back relative path of that file.
Of course, I need to have in mind that file names have to be unique ( which is not problem for what I need )

as you can see herehttp://proba.podrug.com/index.php?cID=47... thumbnails have names as their images just prefixed with tn_ so when gallery block generates files from images, I get filename and append tn_prefix, run my function and got filename I need.

I had to rewrite file.php module and view.php in gallery block but it works now.

Thank you.