Get Path of File in PHP without Uploading - php

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.

Related

Upload from local URL to server

Is it possible to send file from a local URL?
I need to upload files in php from your local url, eg I open the web page with:
www ... upload.php?url=c://...file.jpg,
from the url GET,I would get the file on the pc and would upload, without using html or anything else that I have to choose, just with the file url.
Important, it can only be this way, it will not be possible to choose the file during the upload, it will only be a POST or GET with the local url.
I researched and found nothing related, if anyone can help
I think it is impossible for server to get a client file by using the file path.
But maybe you can use JS and FileSystemobject to prepare the file, make it to streaming and post to the server.
And you must know FSO need a high security permission and may be disabled on users' browser.

How to get client side absolute path of selected file in php

I want to know whether there is anyway to get the client side absolute path of selected file!
$_FILES['xml_file']['tmp_name']; This also provides path of selected file but its server side path... Its result is something like this C:\xampp\tmp\php2679.tmp
I want C:\Users\Sami\Desktop\data\myfile.xml where my actual file is placed.
Because I want this actual path to use again for fetching and saving etc automatically (by code).
you may try echo realpath($_FILES['xml_file']['name']); on your php.
then the output will be:
C:\Users\Sami\Desktop\data\myfile.xml
When you click the browse button it chooses the file. When you click the submit button, the entire file is being sent from the browser to the web server, and PHP is indeed putting the entire file in the php servers temp directory. Then, your script gets called.
So when you try to get absolute path from $_FILES['xml_file']['name'] or $_FILES['xml_file']['tmp_name'] or realpath($_FILES['xml_file']['name']) you will get the path on server.
The solution to this problem would be to simply copy the file somewhere, and not ask for the file upload again. You'll need to be sure that where ever you copy it to on your web server, that the server has permissions to do so. Usually that involves giving write permissions for the user which the web server runs as (usually user 'apache' or 'nobody' for apache based servers) to a special directory you've created for the purpose.
Copy is available in PHP. http://us3.php.net/manual/en/function.copy.php
Your only trick will be what to name it so you can find it again for the second request, and deciding what directory to put it into.

How to provide a local path for downloading a file from a server?

I am trying to download an object/ file from AWS S3 to the local computer. I would like to provide the user with the opportunity to provide the local path. In HTML we have the
<form> and <input type="file">
elements to provide the user with the option to select a file from the file system for upload. How do we do the reverse? Any pointers would be greatly appreciated.
You can't.
Being able to do so would risk websites trying to put stuff in /etc/hosts, ~/.bash_profile, C:\Windows\System32, etc. You can set a (suggested) filename, but it's going to go to the browser's preferred Downloads folder.
It's a security issue to write anywhere you want to the filesystem. You can present it as a download (application/octet-stream / Content-Disposition) but the user's browser gets the right to choose in the end regardless.
You can force a file to download instead of display on a page from the server with a specific filename only, but it stops there. The browser has the choice of popping up a Save As dialog or saving it in the default Downloads folder.
Incidentally, when a user chooses to upload a file, you don't actually get the path either - you get a fake path and a user-defined real filename. On Windows Chrome, it sends something typically like c:\fakepath\ so it doesn't reveal overly-personal information in the path.

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>

How to let a user open a file stored on the server?

I have a WordPress site, in which type1 users can upload *.doc files and type2 users should be able to open them by clicking on a link which represents a file.
The file then should be opened in a new window of the browser.
I got the first part easily - there are many guides on uploading a file.
But I couldn't find one that goes through all the steps of getting and opening a file which resides on a server directory.
I assume clicking the link should cause an ajax request which will send the file key to the server.
The server can then get from the MySQL DB the file name and location.
But what do I do then?
How do I get the file from the server directory, send it back to the client and open it for reading in the browser?
Edit:
I want the files to be confidential and not let just anyone read them so giving the users the link for the file on the server isn't the right way.
I still think I need to send the file from the server directory to the client.
How can this be done?
You can add links to uploaded media in WordPress easily. Just remember to link to the actual file:
I want the files to be confidential and not let just anyone read them so giving the users the link for the file on the server isn't the right way.
Yes it is. You can't let people read the document without giving them the document.
If you want to keep it confidential, then require the users to agree not to share the link with anyone else. Or go further and add a layer of authentication + authorisation and also require that users not hand over their credentials.
Simply, redirect the browser to the location of the file. It's depend on user's browser settings that file opens on the browser directly, or user sees open/save dialog.
But, if you want to open the file inside the browser without letting user download it, you should redirect her/him to a custom page. e.g. a page that loads a doc editor.
There's no way to show a Microsoft Word file inside the browser unless they install proper software. e.g. an Applet or ActiveX or something that be able to open MSWord files.
Consider using WP Client File Share plugin.

Categories