Jump to content

n00b question... struggling with passing variables to a php page


ArielZusya

Recommended Posts

I'm very new to php (literally hours *GRIN*).  I've got a link on a page that I'd like to point to a php page and pass some variables to that page.  Here's what I'm trying to do.  I've got a list of files on my site that I'd like a user, when clicking on an associated link, to be forced to download the file rather than open it.  I'm trying to prevent streaming. Some of the files are midi, some are pdf, some are finale .mus files, and some are mp3s.  Right now I have the link on the list point to a php file unique to the spcific link (in other words if the link is for file1.mid then the link points to file1_mid.php).  I'd like to able to pass the filename and the mime-type to a more universal php file and have it work for all the files which could be downloaded (in other words, something like sendmethefile.php?myfn=file1.mid&mymt=audio/midi). This is, I'm certain, sounding more complex than it actually is (yep... I'm that new at this... can't even figure out how to describe it simply).  Here's the code I've got so far:

 

First, an example of the individual page referenced above (file1_mid.php):

<?php
header('Content-disposition: attachment; filename=file1.mid');
header('Content-type: audio/midi');
readfile('file1.mid');
?>

 

and this works just fine but I'd like it to be one size fits all... so something like (sendmethefile.php):

<?php
$myvar1 = $_GET['myfn'];
$myvar2 = $_GET['mymt'];
header('Content-disposition: attachment; filename=' && $myvar1);
header('Content-type: ' && $myvar2);
readfile($myvar1);
?>

 

but that doesn't work.  I get the following error:

Warning: readfile(file1.mid): failed to open stream: No such file or directory in /<actual directory info has been removed>/sendmethefile.php on line 6

 

It would be great if I could send the filename and mime-type so I can use the same sendmethefile.php for all the various files I would have links to on my site.  What do I need to do differently?  Thanks for your help!

Link to comment
Share on other sites

<?php
$myvar1 = $_GET['myfn'];
$myvar2 = $_GET['mymt'];
header('Content-disposition: attachment; filename=' && $myvar1);
header('Content-type: ' && $myvar2);
readfile($myvar1);
?>

 

I'm not sure why you are using the operator && (and) in the header function, you will probably have more luck with the code below

 

<?php
$myvar1 = $_GET['myfn'];
$myvar2 = $_GET['mymt'];
header('Content-disposition: attachment; filename='.$myvar1);
header('Content-type: '.$myvar2);
readfile($myvar1);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.