accessing file of another drive - php

previously I could download wav file in php and IIS. But now file is not downloadable.I don't know what is going wrong . After installing and changeing php 5.3 to php 5.4 with php manager in IIS, the file is not able to downloaded. I have link to file to download it which looks like this:
Download
download.php scripts
<?php
$filename = $_GET['voice'];
$dir = 'd:/temp_file/voice/';
if(is_file($dir.$filename))
{
header('Content-type: audio/wav');
header('Content-Disposition: attachment; filename='".$filename.'"');
echo file_get_contents($dir.$filename);
}
?>
while I was trying to find the mistake why file is not downloadable, I remove if statement and run program, it does prompt window download file with audio player of empty wav file. So, I conclude that there is mistake in path which is not allowing to access file of another drive. I have php code in c:\inetpub\wwwroot\ but wav file to be download is in the d:\temp_file\voice path. What Should I have to do?

Maybe PHP open_basedir restriction.
Try to add d:/temp_file/voice/, to its content in php.ini.
Update
I never used IIS, but another thing that you have to check is if the IIS User have permission to read that directory. Try to add, just for test, Everyone with all permission to d:\temp_file\voice.

I don't know what is going wrong
Then enable error reporting, for what it looks like to me it's a syntax error at header('Content-Disposition: attachment; filename='".$filename.'"'), you're missing a quote (') after filename='". But since you say it has worked (and works without the if), I'd say that's a copy paste error.
Then do a var_dump($filename), which might contain quotes, since you put those around the URL in the link (<a href="download.php?voice='$filename'">). The file D:/temp_file/voice/'foo.wav' might not exist.

Related

Downloading an SQlite .db file

Hello currently I am working on a tool that converts an otherwise file-based stored text array(not really important though what it does). It works great and I am able to download and use it if I download the file off the FTP, but when I try to directly download it to the person using the .php file, it says Invalid file. My current code is:
header('Content-Type: application/octet-stream');
header("Content-Length: " . filesize($dbname));
header('Content-Disposition: attachment; filename="object database.db"');
readfile($dbname);
What is the issue? Is the content-type or something else wrong? Or should I be using readfile(I tried this?)
When I download with the above code, the name of the file seems to be correct or whatever but when I attempt to open it with the SQLite3 Browser(it works when I download directly off the FTP) the program displays "Invalid file format". When I use readfile it downloads as "download" with no file extension and when I attempt to open windows says Invalid file. Any help would be awesome.
There's no application/db content type. You should use application/octet-stream instead. Also filename in Content-Disposition is just filename you suggest client to save file as, but nothing more, so you yet needs to send the file content itself to the remote peer.
readfile(....);
die();
should do the work if your data is stored in the file already, otherwise you can just echo() it instead.

Opening downloaded zip file creates cpgz file?

If I make the url for a zip file the href of a link and click the link, my zip file gets downloaded and opening it gets the contents as I expect.
Here's that HTML:
download zip
The problem is I'd like the link to point to my application such that I could determine whether the user is authorized to access this zip file.
so I'd like my HTML to be this:
download zip
and my PHP for the /canDownload page:
//business logic to determine if user can download
if($yesCanDownload){
$archive='https://mysite.com/uploads/my-archive.zip';
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archive));
header("Content-Length: ".filesize($archive));
ob_clean();
flush();
echo readfile("$archive");
}
So, I think the problem has to do with the header() code but i've tried a bunch of things related to that based on various google and other SO suggestions and none work.
If you answer my question, it is likely you can answer this question too: Zipped file with PHP results in cpgz file after extraction
The answer in my case was that there was an empty line being output before readfile().
So i added:
ob_end_clean();
readfile($filename);
But you should probably search for the place where this line is being output in your code.
The PHP documentation for readfile says that it will output the contents of a file and return an int.
So your code, echo readfile("$archive");, will echo $archive (btw, the double quotes are meaningless here; you should remove them), and THEN output the int that is being returned. That is, your line should be: readfile($archive);
Also, you should be using a local path (not an http:// link) to the archive.
Altogether:
if($yesCanDownload){
$archive='/path/to/my-archive.zip';
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archive));
header("Content-Length: ".filesize($archive));
ob_clean();
flush();
readfile($archive);
}
Lastly, if that does not work, make sure filesize($archive) is returning the accurate length of the file.
Ok, I answered my own question.
The main problem, which I originally didn't make clear, was that the file was not located on my application server. It was in a Amazon AWS s3 bucket. That is why I had used a full url in my question, http://mysite... and not just a file path on the server. As it turns out fopen() can open urls (all s3 bucket "objects", a.k.a. files, have urls) so that is what I did.
Here's my final code:
$zip= "http://mysite.com/uploads/my-archive.zip"; // my Amazon AWS s3 url
header("Content-Type: archive/zip"); // works with "application/zip" too
header("Content-Disposition: attachment; filename='my-archive.zip"); // what you want to call the downloaded zip file, can be different from what is in the s3 bucket
$zip = fopen($zip,"r"); // open the zip file
echo fpassthru($zip); // deliver the zip file
exit(); //non-essential
Another possible answer, I found After much searching, I found that the two possible reasons for a *.zip "unzipping" to a *.zip.cpgz are:
the *.zip file is corrupted
the "unzip" tool being used can't
handle >2GB files
Being a Mac user, the second reason was the cause for my problem unzipping the file: the standard Mac OS tool is Archive Utility, and it apparently can't handle >2GB files. (The file in question for me was a zipped 4GB raspbian disk image.)
What I ended up doing was to use a Debian virtual machine, already existing in Virtual Box on my Mac. unzip 6.0 on Debian 8.2 had no problem unzipping the archive.
You're passing the URL to readfile() like:
$archive = 'https://mysite.com/uploads/my-archive.zip';
While you should pass the path on the server, for example:
$archive = '/uploads/my-archive.zip';
Assuming the file is located in the upload folder.
Additionally try the following headers:
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=file.zip");
In my case, I was trying to create the file in a directory above public_html and the rules of the hosting didn't allow it.

php image jpg header problem

HI,
i have an image .jpg into my server. If I link direct to image i have this error:
Forbidden
You don't have permission to access http://...
I need to show with php. I have try
header('Content-Type: image/jpeg');
readfile('$file');
But nothing...
I have also try using server root...
any suggestions?
You are trying to load a file called $file. That's a weird name for a picture; I assume it's a PHP variable name where you store the picture file name. In such case:
header('Content-Type: image/jpeg');
readfile($file);
exit;
'$file'
in single quotes is looking for a file called $file
"$file"
in double quotes
or even without any quotes at all, is looking for a file called by the value stored in $file
You have to set the correct rights for the image. You can do this via ssh (console) or easier with your ftp program. If done so, you can access it directly
Probably the file is in a directory that the web server isn't set up to allow access to. PHP can access pretty much any directory on the web server, but apache/IIS/etc will restrict normal access by default to only directories specified in their configuration. If this is the problem then serverfault may have better expertise to help you get your server set up.

Content(php code) Download from apache directory list

I have to download .php file with php source code from a apache directory listing page. can anybody have such idea or experience????
You can either rename the file by choosing a different extension.
Or, you can remove the AddType attribute in IfModule related to your directory that defined .php extension.
You could use http://us3.php.net/highlight_file to set up a specific file that can print out the source for any file. If you look at the description section it also talks about how you can setup your server to automatically display the source if you have a file named .phps.
You should be able to figure out a solution that works for you with something like this.
If you absolutely need the file to download and not just display the source you could create a script that reads the file and presents it to the user as a plain text file download. That could be accomplished with a combination of http://php.net/manual/en/function.file-get-contents.php and http://us3.php.net/manual/en/function.header.php
You need to stream the file contents through another script:
download-file.php
<?php
$file = '/path/to/php-file.php';
header('Content-Type: application/x-httpd-php');
header(sprintf('Content-Disposition: attachment; filename=%s', basename($file)));
readfile($file);

Download PHP script instead of executing it

I have a downloads directory on a website where I store a bunch of different files for people to download (zip, exe, java, php, etc). The problem is that my website is written in PHP, so the web server, Apache, tries to execute the scripts instead of letting people download them. Without having access to Apache config (I'm on shared hosting), what is the easiest way to prevent Apache from executing scripts in a single directory?
I tried using mod_mime unsuccessfully. AddType doesn't work because (I'm guessing) a MIME type is already associated with PHP scripts. ForceType doesn't work because I store different types of files in the directory. Are there any other options?
If you have sufficient permissions for that, putting the following line in a .htaccess file in the directory in which you don't want PHP script to be executed might do the trick :
php_flag engine off
(Just tested on my webserver, and I got the source of a PHP script -- which had not been executed)
You could write a separate PHP script which sends the right Content-type header and then uses readfile() to pass through the contents of the PHP file without PHP actually executing them (and since Apache already passed off the request to PHP, it no longer cares). Just make sure you restrict it to only serving things out of that directory.
I think the common solution to this is to give files the extension phps.
I think I have the solution for you. Check this out:
http://www.boutell.com/newfaq/creating/forcedownload.html
Basically, it says that you should have the following code in your php page:
<?php
header('Content-disposition: attachment; filename=whatever.php');
header('Content-type: text/html');
readfile('whatever.php');
?>
I made a sample here:
http://sotkra.com/test.php
This forces the 'download' file prompt where the file download is the whatever.php
Cheers
You should have a download gateway script, such as download.php. It should take a query string argument which lists the file that needs downloaded.
That argument should be matched against a pre-existing ARRAY of accessible files (big security point there).
Then use:
<?php
$file = trim(isset($_GET['file']) ? $_GET['file'] : '');
$allow = array(
'foo.php' => 'text/plain',
'foo.jpg' => 'image/jpeg',
);
if(! isset($allow[$file]))
die('File not found.');
header('Content-Type: ' . $allow[$file]);
readfile($file);

Categories