I keep getting a T String parse error. I am not sure what I'm missing? Any help would be appreciated!

<?php
$filename = "http://www.empowergroupusa.com/cambiolabs/extension/catalog/files/inspection_886.pdf";

if (file_exists($filename)) {
    echo "<a\href="http://www.empowergroupusa.com/cambiolabs/extension/catalog/files/inspection_886.pdf </a>";
} else {
    echo "The file $filename does not exist";
}
?>

Dani AI

Generated

The original parse error came from unmatched quotes in the anchor echo and a misuse of URLs with filesystem checks. correctly pointed out the quoting problem, and was right that PHP's file-check functions expect a server filesystem path, not an HTTP URL. 's streaming idea (read the file and send appropriate headers) is the right direction — just be careful building the local path (the extra space in the posted $_SERVER['DOCUMENT_ROOT'] concat will break it).

Keep output and existence checks separate, build paths safely, and make filenames dynamic via an ID-to-filename map (never trust raw user input). Example pattern (illustrative — adapt paths and whitelist to your app):

$url = '/cambiolabs/extension/catalog/files/inspection_' . $id . '.pdf';
$fs = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . $url;

if (is_file($fs) && is_readable($fs)) {
    echo '<a href="/download.php?id=' . (int)$id . '" target="_blank">Download PDF</a>';
} else {
    echo 'File not found.';
}

To hide the real URL and stream securely from download.php, keep a server-side whitelist or ID map, use realpath to prevent directory traversal, validate the ID, and stream with a safe routine (fopen + fpassthru or readfile). Make the Content-Disposition either inline to try opening in-browser or attachment to force download, and include Content-Length when possible.

Quick troubleshooting checklist:

  • Fix the quoting in your echo (use single quotes outside or concatenate).
  • Use filesystem paths with file_exists/is_file, not HTTP URLs.
  • Remove stray spaces when joining $_SERVER['DOCUMENT_ROOT'] and paths.
  • If checking a remote URL, use get_headers() or curl (allow_url_fopen must be enabled for file functions to accept HTTP).
  • Sanitize any dynamic part (use intval or a whitelist) to avoid arbitrary file access.

Recommended Answers

All 8 Replies

try:

<?php
$filename = "";

if (file_exists($filename)) {
echo '<a href="">PDF</a>';
} else {
echo "The file $filename does not exist";
}
?>

try:

<?php
$filename = "";

if (file_exists($filename)) {
echo '<a href="">PDF</a>';
} else {
echo "The file $filename does not exist";
}
?>

Thanks! I got the error to go away, but the link shows up on the webpage and it is not working. How do I get it to work without exposing the url? Also, I would like to make the part after inspection variable or dynamic so it will change based upon which page is loaded? Thanks so much!

I am pretty sure the path for file_exists needs to be relative to the script it's called from. If the script is being run from the root you want:

if(file_exists('/cambiolabs/extension/catalog/files/inspection_886.pdf')) {
//do something
}

Also, I would like to make the part after inspection variable or dynamic so it will change based upon which page is loaded?

I have no idea what you are talking about. You need to elaborate!

I am pretty sure the path for file_exists needs to be relative to the script it's called from. If the script is being run from the root you want:

if(file_exists('/cambiolabs/extension/catalog/files/inspection_886.pdf')) {
//do something
}

Would fopen() work to open the pdf file in a new window?

try:

<?php
$filename = "";
if (file_exists($filename)){
	header('Content-Type: application/pdf');
	header('Content-Disposition: attachment; filename="Inspection.pdf"');
	readfile($filename);
	exit;
}
else
{
	echo "The file $filename does not exist";
}
?>

as long as i can see, the error is in this line of code: echo "<a\href=" </a>";

do something like this:
echo '<a href="">PDF file</a>'; or
echo "<a href=\"">PDF file</a>";

On my previous post change:

$filename = "";

to:

$filename = $_SERVER['DOCUMENT_ROOT'] . ' /cambiolabs/extension/catalog/files/inspection_886.pdf';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.