This topic is locked

If file exists ?

2/19/2007 2:55:51 AM
PHPRunner General questions
A
asimha author

Could someone kindly assist here?

  • I am displaying a list of records in which there is a file name and a link to that file.
  • The path to the file location is hardcoded hence when the record is displayed the link is active.
    I would like to display the link only when the file exists.
    Any help much apprecaited!

    Thank you

    Andre

Alexey admin 2/19/2007

Andre,
use Custom View type for your field.

Here is the sample code for Custom expression box:

if(file_exists("files/".$value))

{

$value="<a href=\"files/".$value."\">".$value."</a>";

}

else

$value="";

A
asimha author 2/19/2007

Andre,

use Custom View type for your field.

Here is the sample code for Custom expression box:


Thank you very much Alexey, however as usual I get lost with the more complex expressions.
What is wrong in the following expression?
[codebox]if(file_exists("/docs/" . $data["docs"]))

{

$value= "<a href=\"/docs/" . $data["docs"] . "\" target=\"_blank\" onclick=\"java script:ShowDoc('" . $data["doc_name"] . "',this.href,'1'); return false\">DOC</a>";

}

else

$value="";

[/codebox]

J
Jane 2/19/2007

Andre,
$data variable is the global array and you need to define it:

global $data;

if(file_exists("/docs/" . $data["docs"]))

{

$value= "<a href=\"/docs/" . $data["docs"] . "\" target=_blank onclick=\"java script:ShowDoc('" . $data["doc_name"] . "',this.href,'1'); return false;\">DOC</a>";

}

else

$value="";

A
asimha author 2/19/2007

Andre,

$data variable is the global array and you need to define it:


Thanks very much Jane, but this still does not work and generates an error in include/commonfunctions.php (syntax?)

Sergey Kornilov admin 2/19/2007

Andre,
what exactly is the error?

A
asimha author 2/19/2007

Andre,

what exactly is the error?


Sergey the error is
Parse error: syntax error, unexpected T_STRING in /home/www/3584ef1d9a4e06b4f5916fec98d25abc/web/media/include/commonfunctions.php on line 343
and the resulting code in commonfunctions.php is
[codebox]function CustomExpression($data,$field,$table="")

{

global $strTableName;

if(!$table)

$table=$strTableName;

$value=$data[$field];

if($table=="user_helper" && $field=="doc")

{

global $data;

if(file_exists("/docs/" . $data["doc"]))

{

$value= "<a href=\"/docs/" . $data["doc"] . "\" target="_blank\" onclick=\"java script:OpenDoc('" . $data["doc_name"] . "',this.href,'1'); return false;\">DOC</a>";

}

else

$value="";
}

return $value;

}
[/codebox]

Sergey Kornilov admin 2/19/2007

Andre,
what is the line 343 in your include/commonfunctions.php file?

A
asimha author 2/20/2007

Andre,

what is the line 343 in your include/commonfunctions.php file?


It is the last curly bracket of the snippet above Sergey. This is probably some very silly syntax error but I just can't find out where ....

Alexey admin 2/20/2007

Andre,
try to remove target="_blank\" clause from your code.

A
asimha author 2/20/2007

Andre,

try to remove target="_blank\" clause from your code.


Now the code works with no error but the link to the document is not active although the file exists ...

Alexey admin 2/20/2007

Andre,
the solution we provided does work.
Inspect your code, check field and directory names etc , find and fix the error.

A
asimha author 2/20/2007

Andre,

the solution we provided does work.
Inspect your code, check field and directory names etc , find and fix the error.


Alexey, I have found the problem ... and solved the problem!
The function file_exists will return True if the directory or the file exists, that is why all my links are active.
I replaced file_exists with is_file and my problems are now solved!
Thanks for all your help and sorry for the hassle - this was tough one for me!