What are possible reasons that HTML download link is failing? - php

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>

Related

Get Path of File in PHP without Uploading

So essentially I want to insert the Path into my Database, as a string, I am not interested in uploading it - I just want local users to browse to the file then submit the form and the path to the file is added as a string. So when I echo this out into a url, people on the network can click the link to download it from the network location.
$location = fopen($_FILES["location"]["tmp_name"], 'r');
I've tried the above but I guess the file needs to be uploaded for it to get the path.
Any ideas?
Unfortunately PHP will not help you with this as it requires you to actually upload the file.
Likewise, as was mentioned in the comment you are not able to get the local file path either from the form. This is a browser standard and is done for security purposes. You can read more about it here: https://stackoverflow.com/a/4176605/8518859
The only solution in your case is to provide a text input and ask the user to paste the network path to the file. Perhaps if you provide them simple instructions on how to get the network path it will make their task easier.

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.

Opening a text file in browser - error on server

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>

PHP Download file from non-hosted directory

I've been able to upload a file through PHP to a non-hosted directory (directory is not on the website) with read/write permissions for the PHP file (www). How would I download the file from this directory? I've been able to list the contents of the directory, but clicking on the files (made the filenames links) does not work as the computer attempts to download the file from the path on the server. I'm new to PHP, so all help is appreciated. Thanks!
Edit: Before I get down votes for this being a broad question, I just want to know how to access the files in the non-hosted directory and pass them to the user. I already know how to download normal files hosted on the website. Thanks!
You can use a delegating PHP file for file access. I don't know anything about your structure, but if you can write it, you can (presumably) read it back with file_get_contents:
<?php
echo file_get_contents('/the/path/to/the/unhosted/directory/file.ext');
?>

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

Categories