Opening a text file in browser - error on server - php

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;">
Code
</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 Code on http://www.example.com/dir/page.html then page.html is dropped and code.txt is appended, leading to http://www.example.com/dir/code.txt. If the code.txt file is not uploaded to that location on your webserver, it will not be found.
Are you sure code.txt is 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:
The file is on the server, but not where you think it is. For example, many webhosts have a private root directory that is not accessible to the web, inside of which is a public_html/ or www/ 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.
Your website is not set to allow .txt files to be served up to browsers. Have you been able to upload and see any txt files, or is this the first file with this problem? What happens if, for the sake of trying it, you rename the file to code.html and 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.

To open a new window by clicking on the link, you will need to use window.open method of javascript.
<a href="javascript: void(0)"
onclick="window.open('popup.txt',
'windowname1',
'width=200, height=77');
return false;">Click here for txt</a>

Related

What are possible reasons that HTML download link is failing?

I created a page for users to download certain files. I used the HTML download link to do this as so:
<a download href="/home/Uploads/someDir/someFile.PNG" >View</a>
This worked fine when I was developing on my laptop with WAMP. When I uploaded the code to our Centos 7 Server it did not work. I changed the Uploads, someDir, and someFile.PNG permissions to 777. I also made sure that the file path was correct. But the when the download link is pressed the file icon at the bottom of the screen says "Failed no file".
Maybe I need to do more with the permissions. I know that the folder that the html file is in has restricted permissions, but I would prefer to keep them that way. I do not know how that could cause an issue.
I am looking for debugging tips/possible causes. At this point I am clueless where to look.
The issue with the download not working is simply due to the fact that the path to your file is wrong. My advice on your root directory which is in most cases is var/www/html, create a folder called say somefoldername and manually upload the file to that folder then change the download link to be
View.
"Failed no file" has nothing to do with permissions. The path to your download file has most likely changed when the files were uploaded to the server. You should manually check to see if the file at
/home/Uploads/someDir/someFile.PNG
actually exists. If not you can change the link accordingly or move the download file to the appropriate location.
Another good alternative to solve this error is to put a dot '.' at the beginning of the url:
<a download href="./someDir/someFile.PNG" >View</a>
This way you will not need to indicate the complete url.
And if in the future the url changes, you won't have to modify the url.
In my case it works perfect, I hope it helps you too.
Just check the directory in which you are trying to fetch the file. Your looking file may be not present in that directory. Otherwise, you are using the correct piece of code.
<a download href="~/home/Uploads/someDir/someFile.PNG" >View</a>

Redirect a user trying to download a .zip file to download.php?file=fileName in IIS

I need to prevent people from downloading .zip files in my server unless they are logged in. For this purpose and since I'm also using MediaWiki and I would like to have to modify the least this as I'm not familiar with it I was thinking about doing the following:
When a user wants to download a .zip file, it will be redirected by the server (with a web.config rule) to something like download.php?file=fileName and inside the PHP, I can do my programming to see if he's logged in and then use readFile() to give him the file.
However I'm not familiar with IIS (not much more with Apache either) and I'm totally clueless as how to write this rule. Could someone please help me out on this?
I'm also open to other suggestions. Putting the upload folder in a place not accessible to the public (but to the server) may do the trick but images are also uploaded then and then they wouldn't download. I could, again modify the behavior of the upload system myself but as it's done by MediaWiki I would prefer not to.
I have found this code (by using a .htaccess to web.config online translator) but it's not working. Maybe it's easier for you to just fix this code:
http://pastebin.com/waMJnFyK
The uploads are in subdirectories within /images like for example /images/a/ae/file.zip and I would like that when you try to open that you get redirected to a php file where as a GET input I have the file location.
Solution I took: http://pastebin.com/7skGT9uN
It redirects everything that ends in .zip within /images to download.php?fileName=whatever where the /images part is not passed.

This path for a picture doesn't work

I'm building graphs using pChart in PHP. The case is that I have my picture in the right folder, I know it because if I put that path in the browser I can see my picture, but in the template, it doesn't show up. Firebug shows "failed to load the given URI".
<img src="/home/user1/mysite/Admin/Template/mx_graphs/example13.png">
The picture has all the rw permissions given, and the folder.
You appear to be using a UNIX file path instead of a relative URI. Your web server is unlikely to be configured to have the DocumentRoot be / (and it would be very silly to do so).
Construct a URI relative either to the HTML document or to the DocumentRoot of your server.
The latter will probably be /Admin/Template/mx_graphs/example13.png
Remember the statement:
<img src="/home/user1/mysite/Admin/Template/mx_graphs/example13.png">
Will run on the client side, so if you run this on the very computer where the picture is stored, it should work fine, but if you try to open it from another computer it will not work.
Try to use relative path (http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/), and try to keep the website resources into the website root folder, so that even when you host the site the image be copied over and will still be accessible..

Server doesn't show file replacement until I open it in my file explorer

So I am running xampp and wordpress. I have an image uploaded to the uploads directory named "avatar.jpeg". It displays just fine. But when I delete it and upload a new image file named "avatar.jpeg", the server doesn't reflect the change and just shows the old file. But when I open the file in Eclipse or my explorer, then the server starts showing the change and displays the new image.
This is probably something basic which I never learned about. I tried chmod to set the file permissions on the new file but that didn't help.
The file will be cached in your browser. To force a reload, append an arbitrary variable to your file, e.g. <img src="yourfile.jpg?1234567" />
I also guess it's a caching issue like freddy K. does.
I would suggest to configure ETags on your Server.
Simply appending something to the URL may only help you when the appended string is changing on every update of the requested file(using the current timestamp or similar all the time will force the client to download the file on every request and slow down the page).
http://httpd.apache.org/docs/2.0/mod/core.html#fileetag

Storing and reading images above public_html

I am trying to secure my PHP Image upload script and the last hurdle I have to jump is making it so that users cannot directly excecute the images, but the server can still serve them in web pages. I tried changing ownership and permissions of the folders to no avail, so I am trying to store the images above public_html and display them in pages that are stored in public_html.
My File Structure:
- userimages
image.jpg
image2.jpg
- public_html
filetoserveimage.html
I tried linking to an image in the userimages folder like this:
<img src="../userimages/image.jpg">
But it does not work. Is there something I am missing here? If you have any better suggestions please let me know. I am trying to keep public users from executing potentially dangerous files they may have uploaded. Just as an extra security measure. Thanks!
You want something that's basically impossible.
The way a browser loads a page (in a very basic sense) is this:
Step 1: Download the page.
Step 2: Parse the page.
Step 3: Download anything referenced in the content of the page (images, stylesheets, javascripts, etc)
Each "Download" event is atomic.
It seems like you want to only serve images to people who have just downloaded a page that references those images.
As PHP Jedi illustrated, you can pass the files through PHP. You could expand on his code, and check the HTTP_REFERER on the request to ensure that people aren't grabbing "just" the image.
Now, serving every image through a PHP passthru script is not efficient, but it could work.
The most common reason people want to do this is to avoid "hotlinking" -- when people create image tags on other sites that reference the image on your server. When they do that, you expend resources handling requests that get presented on someone else's page.
If that's what you're really trying to avoid, you can use mod_rewrite to check the referer.
A decent-looking discussion of hotlinking/anti-hotlinking can be found here
Use an image relay script!
To serve a imagefile that is outside the public_html folder you would have to do it by a php script. E.g make a image-relay.php that reads the image that is outside the public html...
<?php
header('Content-Type: image/jpeg');
$_file = 'myimage.jpg'; // or $_GET['img']
echo file_get_contents('/myimages/'.$_file);
?>
Now, $_file could be a $_GET parameter, but its absolutley important to validate the input parameter...
now you can make an <img src="image-relay.php?img=flower.jpg"> to access a flower.jpg image that is located in /myimage/flower.jpg ...
Well, a web browser will only be able to access files and folders inside public_html.
If the public_html directory is the root of the server for your users, Apache cannot serve anything that is not inside/below that dorectory.
If you want a file to be served by Apache directly, you'll have to put it in/below public_html.
I think your misunderstanding is in the fact that if you include an image in an <img> tag, your browser will send the exact same request to the webserver to fetch it, that will be sent to the webserver if you try to open the src url of the image in your browser directly.
Therefore, either both things work, or neither.
There are hacks around, involving a (php or other) script to make sure that an IP that has requested the image has also requested the html page within the last few seconds (which will not work if the user is behind a proxy that rotates outgoing IPs) or by checking the referer (which does not work with HTTPs and also not if the user has referer disabled).
If you want to make sure that only some users can see the image (both via <img> tag and directly), you can put the image outside public_html and have a (php or other) script that verifies the user's credentials before serving the image.
If you are using Apache or lighttpd you can use the X-Sendfile header to send files that are not in the web root(provided you haven't changed the configuration of mod_xsendfile).
To learn more about X-sendfile see this site.
This solution is giving you the best possible performance as PHP doesn't send the file but the server does and therefore PHP can be exited while the files are being served.
Hope that helps.

Categories