I’m trying to open a text file in my browser, when doing it from my local machine it works perfectly but when I upload my files to my web server the link to the text file no longer works and I get a broken link error from the browser.
Here is my code for opening the txt file:
<div style="width: 100%; background-color: #CCC;text-align: center;">
<a href="code.txt" onclick="window.open(this.href,this.target,'height=300, width=500');return false;">Code</a>
</div>
A relative URL (neither a complete URL, i.e. one that starts with a protocol like
http://nor one that starts with a/indicating the root of the site) is relative to the last/in the URL. This means that if you have<a href="code.txt">Code</a>onhttp://www.example.com/dir/page.htmlthenpage.htmlis dropped andcode.txtis appended, leading tohttp://www.example.com/dir/code.txt. If thecode.txtfile is not uploaded to that location on your webserver, it will not be found.Are you sure
code.txtis live on the server? That is to say can you navigate manually to the text file on your server?Are you sure your PHP file is in the same directory as
code.txt?Are there any case (upper/lowercase) issues, like @BugFinder mentioned?
Does it work without the JavaScript popup? From your question it doesn’t sound like a JS issue, but seeing as @Sathish thought there was something to correct with your popup behavior (it looks fine to me) it’s worth removing that variable and seeing if a plain link works.
Are there any permission issues or restrictions on your server preventing the file from being displayed? For that matter, what is the exact error code and message you are receiving? 404? Something else?
I’m inclined to guess from what you’ve said so far that the file hasn’t actually been uploaded. Alternatively, it’s not actually in the same directory as your PHP file, despite what you’ve said. If it’s neither of these issues, please update your question with more details.
If the file exists according to FTP, there’s a couple of possibilities:
public_html/orwww/directory which is the actual root of your website. Have you been able to upload other files to your website in the past? It sounds like the PHP file is successfully available, so assuming the txt file’s in the same place, this isn’t the issue.code.htmland upload that to the same place?For the record, this is likely a far better question to bring up with your host (or just look through their docs / support) than with StackOverflow, it’s not a programming question but a hosting issue. Since we don’t know who your host is, nor the intricacies of every individual hosting provider, it’s hard to diagnose what’s wrong.