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');
?>
Related
I cannot believe this hasn't been asked before, but I cannot find the answer on stackoverflow, please redirect me if this is a duplicate and I'm too retarded to find it.
I am creating a script that will download a kmz file from my custom google map, then will unzip it, read the xml and create a web page that lists all my markers. But I don't have access to the servers to create a writable folder so where can I download the kmz file to then unzip it?
Tried the temp folder (/var/tmp) and it seems to download the file, but I cannot work on the file afterwards or extract it, probably because the file gets deleted right after I call fclose, after downloading, as it doesn't exist anymore if I scan the directory.
UPDATE:
To answer question, the file is immediately deleted because it is a shared server and there are tons of session files in that folder that are created by all the visitors of each websites
I am facing some typical problem from one of my project.
A section in my project need a file up loader. The requirement is when an user clicks on the browse button, it will not only opens up the local directory, the already uploaded file directory need to accessed as well.
In simple file uploader only the local directory can be accessed, I cannot able to fine any way out.
If any one can help me in this issue, it will be great for me.
Thanks in advance.
Is impossible to use to use the "file open dialog" / file uploader to browse files from server.
You can create a dialog inside your browser with (html,css,php).
Is difficult to give an example, because I do not know your requirements.
However you have to use two buttons, it will be easier:
upload from your computer
upload from the server
On click "upload from the server" , opens a window IN BROWSER (HTML+CSS) , that lists your folders and files on the server.
To implement this , read here:
http://php.net/manual/en/function.scandir.php
http://php.net/manual/en/function.readdir.php
I am trying make a php script to list the files of a folder above my web directory...I follow a small thing I found here which talked about
a symlink pointing to /var/uploads
a Apache Alias directive Alias /uploads /var/uploads
I did both of these.
$myDirectory = opendir("/var/stuff/stuff/");
That directory there links to like when I go in winscp and click that folder it directs me to it....and when I run my script to list all files inside /var/stuff/stuff/ it lists what is in /home/stuff/stuff.
The thing is when I click the links that it produces I get a not found on the server
The requested URL /stuff was not found on this server.
Would someone please be able to assist me with this?
Since the files are not under the root directory, you will not be able to download them directly, but What you can use for this issue is to create a downloader gateway.
I'll explain:
create a php script somewhere in your app, lets call it downloader.php
the downloader script would get a parameter, lets say: filename
now we could call the script like: http://YOUR-URL/downloder.php?filename=file-to-download
in your downloader.php file you can get the file name, read it from the file system, then force it to be downloaded by out puting its content and configure the correct headers
I'm not sure if you still need that but, if you need more assistance I can help you more with some code samples
ideally you can use .htaccess to hide your downloader gateway script
I want to download a torrent using php.
So the scenario is that the user uploads a torrent and the server downloads it.
Than the user can download the torrent using his browser.
Dont know if this is the right way for you but:
Install rtorrent (https://wiki.archlinux.org/index.php/RTorrent)
configure rtorrent to watch a direcory for new torrent-files (http://jkt.im/2011/09/28/automatic-torrent-management-with-rtorrent-and-some-helper-scripts/)
rtorrent will download the files and move it into a directory like "done"
php script checks the folder vor new content and convert / show the files online in webpage
All the functionality you need already exist with rTWi http://rtwi.jmk.hu/ and its also open source so all you need is little customization
I have read the following tutorial "Uploading Files To the Server Using PHP"
and have several questions related to the topics.
Q1> The tutorial mentions that
"Note that PHP must have write access
to $uploadDir or else the upload will
fail"
For me, I only allow the user to upload the file after the user has login to the website.
If we set that $uploadDir permission as 777, then everyone can have written permission to that folder. How to avoid this problems?
Also I am using WAMP as my testing bed, can I simulate the same case as a real web server?
Q2> In order to prevent Preventing direct access, the tutorial mentions:
"A better approach is to move the
upload directory away from your web
root. For example, the web root for
this site is:
/home/arman198/public_html/ to prevent
direct listing i can set the upload
directory to /home/arman198/upload/."
Now my problem is that how can I display the uploaded images on other website pages. Since, the upload is not accessible directly anymore? I need to display the uploaded image save personal headshot dynamically on other website page. Is it possible?
Thank you
It's a common problem.
All modern computers have a temporary files directory. On Linux/Unix it's /tmp, on Windows it's usually c:\temp. The OS install will have set permissions on that directory so that anyone can write files there but only privileged users can delete files that don't belong to them. This is where PHP will want to put an uploaded file; your application then has to move it elsewhere (this is the purpose of the move_uploaded_file() function). PHP under Windows may need upload_tmp_dir actually set in the php.ini file.
Once you have an uploaded file, you can shift it whereever you like, including to where the webserver can read it to serve it. The biggest problem with that it is awfully easy to put this directory inside your codebase. Don't do that. As soon as you do anything beyond editing the files inside the directory they are served from, it will be problematic. Trust me: I've dealt with a few times this in code I've inherited. It's easy to let your webserver load files from a location outside your codebase.
The other alternative is to produce a download script. That way the file need not be servable by the webserver at all. One disadvantage is that you don't get to leverage the web server's MIME translation, but then, that lets you control which types of image files are permitted.
For the second question, you can use a PHP script intead of direct access to the directory. Lets name it image.php. Lets assume that it can take a parameter id, like image.php?id=image_id. In that file you can get the id using superglobal array $_GET. Then you can search for images with that Id and just send it as response.
First one I'm not sure, but maybe play with .htaccess file.
And for the first question, try setting your permissions to 775. That should allow PHP to write the file to the directory without giving the general public write access.