Jump to content

[SOLVED] Check if file exists so that I can link to a product file form a directory


Recommended Posts

Background:

What I am trying to do is to have a folder full of product datasheets so that the product page when loaded checks for the file in this directory and if it matches the product SKU then it displays a link to the pdf file. I would also do this for a second data file that is included with most products.

 

 

I am in product.tpl and found where I should put the code but not sure what to use. I have done this before in ASP and PHP but the template is in smarty and dont know where to start.

 

Can someone help please. TIA

Link to comment
Share on other sites

my logic would be:

 

pdfdir=/path/to/files

filename=(pull in product SKU from somewhere).pdf



if file_exists $pdfdir\$filename
 output <a href="$pdfdir\$filename">Product Details file</a>
else
 output "No product Details file at this time"
end if

 

how do I do this in a template file?

Link to comment
Share on other sites

Hi,

 

You have to assign the variable output to smarty

global $smarty;
$smarty->assign('output', $ouput);

 

and then in your template

{$output}

 

Thanks for the response elkastor... I thought no one was reading my post for a bit there :)

 

The output item was just used to show the general programming logic that I was aiming at. What I am looking at is being able to check if a file exists or not in a directory within the webspace. I believe I can only use "smarty" functions within this template so I am unsure how to do a file exists check.

 

I have a bit in the product page now that works out the product SKU and constructs a link to the pdf file but I do not have a pdf file for every product so therefore I need to check if the file exists first of all and then if it does show the link and if not display some other text.

Link to comment
Share on other sites

its product.tpl under the matrice theme folder.

 

What I tried was this:

 

{assign var='pdffile' value='/public_html/shop/img/pdf/HE-1000.pdf'}
 {if file_exists($pdffile)}{$pdffile}{else}0 - {$pdffile} {/if}

 

this does not work for me which I got from the smarty forum. What I have in place at present at the moment that almost works is this:

 

<ul>
<li><a href=/shop/img/pdf/{$product->reference|escape:'htmlall':'UTF-8'}.pdf>Product Data Sheet</a></li>
<li>Photo Optics Data Sheet</li>
</ul>

 

but this does not deal with the file being missing.

Link to comment
Share on other sites

I think you're doing the wrong way.

 

Try to override the __construct function from the Product class to add an attribute pdf which link to the pdf if it exists and is empty otherwise.

 

and then in the product.tpl you just have to test the pdf attribute

{if $product.pdf}
<a href="{$product.pdf}">Download</a>
{/if}

Link to comment
Share on other sites

new to prestashop and can believe that I am going about things the wrong way :) Where is the _construct function and what would I be looking to modify?

 

Is there no way to check the filesystem for a file being present? I have another filetype as well for different product info that I need to provide a potential link to as well.

Link to comment
Share on other sites

sorry not sure how this hangs together. I can do the code (if I was allowed :) ) for a file exists within the template and can see that I can use php code within the construct class but not sure how this will be pulled through to the template product file.

 

I am not a coder but know a couple of bits here and there but not this stuff. This is the only bit of code I need for getting my product catalog up and running. I would really appreciate it if you could help me with this last bit.

 

thanks for all yor help so far.

Link to comment
Share on other sites

In my own product.tpl, I use the following code and it works perfectly:

 

{assign var="myfile" value="$tpl_dir../../img/pdf/{$product->reference|lower}.pdf"}
{if file_exists($myfile)} <a href="$myfile">$myfile</a> {/if}

 

important: the filename must be lowercase.

  • Like 1
Link to comment
Share on other sites

thanks for the tpl stuff. My filename is constructed from the product sku so which is entered in upper case in the catalog and the filenames were in uppercase to match this. Is the |lower element you are using chanking the product ref to lower case to match?

 

will play with this and see if I can get it to work. It looks like I was not too far off :)

Link to comment
Share on other sites

Thanks for all the help. This is the code that I eventually used. Hopefully it can help someone else:

 


  {assign var="myfile" value="/home/sitename/public_html/shop/img/pdf/{$product->reference|lower}.pdf"}
  {assign var="filelink" value="/shop/img/pdf/{$product->reference|lower}.pdf"}
   {if file_exists($myfile)} <li><a href={$filelink}>{$product->reference} Product Data Sheet</a></li> {/if}

 

I needed to vars so that I could have one for the absolute file reference and one for the link.

Link to comment
Share on other sites

  • 4 weeks later...

Thanks for all the help. This is the code that I eventually used. Hopefully it can help someone else:

 


  {assign var="myfile" value="/home/sitename/public_html/shop/img/pdf/{$product->reference|lower}.pdf"}
  {assign var="filelink" value="/shop/img/pdf/{$product->reference|lower}.pdf"}
{if file_exists($myfile)} <li><a href={$filelink}>{$product->reference} Product Data Sheet</a></li> {/if}

 

I needed to vars so that I could have one for the absolute file reference and one for the link.

hi,desquinn,i am looking for the same way to display my datasheet files in product page. I have bit skill to use copied code. if you can spend few time to write me simple guide in little bit more details, that will be highly appreciated. thanks

Link to comment
Share on other sites

Where is it that you need help. In simple steps I went to the theme directory and found product.tpl and found the area where I wanted to insert my code so it would display where I wanted. I then inserted the code above which looks in a folder and picks up the images if they match the product sku.

 

hope this helps.

Link to comment
Share on other sites

  • 2 months later...

Really not sure what Im doing wrong.. Im trying add manufacturer's logo on product page if exist but the following code doesn't work for me. Is there anything what Im doing wrong? Your help is appreciated

 

 

{$file='../../img/m/'}

{$file=$file|cat:$product->id_manufacturer}

{$file=$file|cat:'.jpg'}

 

{if file_exists($file)}

exist

<img class="logo_pro" src="{$img_manu_dir}{$product->id_manufacturer|escape:'htmlall':'UTF-8'}.jpg" alt="" />

{else}doesn't exist

 

the variable $file giving me path ../../img/m/xx.jpg - this file exist but it giving me all time "doesnt exist"

 

Thanks!

Link to comment
Share on other sites

Really not sure what Im doing wrong.. Im trying add manufacturer's logo on product page if exist but the following code doesn't work for me. Is there anything what Im doing wrong? Your help is appreciated

 

 

{$file='../../img/m/'}

{$file=$file|cat:$product->id_manufacturer}

{$file=$file|cat:'.jpg'}

 

{if file_exists($file)}

exist

<img class="logo_pro" src="{$img_manu_dir}{$product->id_manufacturer|escape:'htmlall':'UTF-8'}.jpg" alt="" />

{else}doesn't exist

 

the variable $file giving me path ../../img/m/xx.jpg - this file exist but it giving me all time "doesnt exist"

 

Thanks!

 

oh got it! didn't see that I have to use $tpl_dir in path

Link to comment
Share on other sites

  • 11 months later...

just found out that you cannot use file_exists() on a URL, but you can use it on a local file.

So, instead of checking file URL, check its path.

This worked for me, when I wanted to check if manufacturer has image

{assign var="manpic" value="img/m/{$manufacturer->id}.jpg"}
{if file_exists($manpic)}
file exists, insert any code here
{/if}

 

And this didn't work:

 

{assign var="manpic" value="{$img_manu_dir}{$manufacturer->id}.jpg"}
{if file_exists($manpic)}
file exists, insert any code here
{/if}

  • Like 3
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
×
×
  • Create New...