Githlar Posted June 23, 2007 Share Posted June 23, 2007 You may ask "Why would you want to send a file with PHP instead of just linking to the file directly?" Simple answer: Download counter. I want to make a simple text file counter that will show me how many downloads my piece of software has received. What I want to do is to link to a PHP file that will add a hit to this simple text file counter and then route the browser to the file... but I have no idea how to do this. Is it possible to send a file as a browser would usually receive it instead of dumping it to the screen? And, while I'm at it, I DO want to change the MIME type of this file, so the browser recognizes it the way it should as I can't do this with my webspace provider. So, here's the rundown for the sake of simplicity: 1. User clicks the link, which leads to a PHP file. 2. The PHP file adds a count. 3. The PHP file sends the file with the application/x-xpinstall MIME type. Any way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/ Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 If u r using db than its simple create an if statement where u have to put the download link, after that put a query to insert record to db in that if statement. I think it work, about flatfiles i have no idea, Wait for someone who will try to help in flatfiles. Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280790 Share on other sites More sharing options...
Githlar Posted June 23, 2007 Author Share Posted June 23, 2007 Uh... what? I guess I forgot to mention that I'm a COMPLETE n00b. I haven't done this in so long that I've forgotten nearly everything I learned. Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280819 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 Look into the header() function, counters are fairly easy. Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280849 Share on other sites More sharing options...
Githlar Posted June 23, 2007 Author Share Posted June 23, 2007 I've heard of that, but I don't know enough about the internals of a browser to know what to send in order for the browser to think it's a file for download rather than display. Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280855 Share on other sites More sharing options...
Githlar Posted June 23, 2007 Author Share Posted June 23, 2007 So, does this mean I should make two calls to the header() function? One that says something along the lines of: header( "Location: http://mysite.com/myfile.xpi" ) and then header( "Content-Type: application/x-xpinstall" ) ? Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280859 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 You'd be better doing something like this: download.php <?php // Update your counter here, depending on your preferred method header("Content-type: application/x-xpinstall"); // Set the content type to the XPI install readfile("myfile.xpi"); // Read the XPI file to be installed ?> Untested, but give it a try Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280860 Share on other sites More sharing options...
Githlar Posted June 23, 2007 Author Share Posted June 23, 2007 OK, well I got the redirect to work using header, but I still can't get the MIME type set. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280861 Share on other sites More sharing options...
Githlar Posted June 23, 2007 Author Share Posted June 23, 2007 Haha! Thank you! I had never heard of readfile() before. I'll look that up as soon as I get done jumping for joy! I thank you very much for all your help. I was wondering if that Content-Type header would stay after a redirect... and I guess it didn't. But it's all fixed now thanks to you! Take THAT stupid web host. For the record I was NOT looking forward to reading the HTTP/1.1 standard, haha! But, if you have an idea what you're looking for, it's not so bad I guess. Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280862 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 So did it work? Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280866 Share on other sites More sharing options...
Githlar Posted June 23, 2007 Author Share Posted June 23, 2007 Yes, it worked and I can't thank you enough. Quote Link to comment https://forums.phpfreaks.com/topic/56827-send-file-with-php/#findComment-280887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.