Jump to content

Obvious noob question - passing variable along an include


Ansel_Tk1

Recommended Posts

Hi - I'm trying to pass a field value along to another page via an include. Could someone help me with the correct syntax?

 

Here is what I've got, where f is the variable name:

 

<?php include 'includes/download.php?f=<?php echo $row_birthdayfile['birthday_pdf']; ?>; ?>

 

Thanks for any help.

Dan

 

 

 

Link to comment
Share on other sites

doing an include is just like running that php script in the place of the include line. so, download.php will have access to all the variables already declared in the script that is including it

 

what exactly are you trying to accomplish?

Link to comment
Share on other sites

Hi Aaron - thank you

 

I have a download script that uses URL varible ?f= to receive the file name.

 

I'm trying to pass that variable from my script, making f=$row_birthdayfile['birthday_pdf'] so that the script download.php will run and send the file to the browser.

 

does that make sense?

 

thanks

 

Link to comment
Share on other sites

By the way, If i think your trying to accomplish; creating a file, and then download it via the browser without saving it, print the source and do this...

 

<?php
ob_start();
session_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/msword"); //applications for pdf = application/pdf etc.
header("Content-Disposition: attachment; filename=\"report_".time().".doc\";" );
header("Content-Transfer-Encoding: binary");
print "%%PDF"; //example, this must be the FULL source of the "document"
ob_end_flush();
?>

 

however if it is saved, then just do this...

<?php
ob_start();
session_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/msword"); //applications for pdf = application/pdf etc.
header("Content-Disposition: attachment; filename=\"PATH/TO/FILENAME.EXT\";" );
header("Content-Transfer-Encoding: binary");
ob_end_flush();
?>

Link to comment
Share on other sites

in that case you will want to do one of two things.

 

1) wrap the code in download.php with a function with one argument. then, at the top of the calling script, do the include of download.php and then in your script you can call that function. if you are going to be sending the file though, the download function can be the ONLY thing sending output to the screen.

 

2) another option is to make download.php accessible as it's own script. this would mean moving it out of includes. then, you can point the persons browser to that script with f as a GET variable. MAKE SURE YOU ADD SECURITY HERE as you don't want someone to be able to download any file. But then, you can have links on the pages point to the script:

<a href="download.php?f=<?php echo $row_birthdayfile['birthday_pdf']; ?>; ?>">Download File</a>

or you can send the browser there with a header() call in PHP:

<?php
  //some other code here
  header('Location: download.php?f='.$row_birthdayfile['birthday_pdf']);
  exit;
?>

Link to comment
Share on other sites

Thank you all for your help. I feel that I am close to joy here, but need a little more help regarding syntax for creating a function.

 

The script below works, although because I have a problem with the header info conflicting, the download is not initiated - the contents of the file are displayed on the browser page instead in gobledygook. Here is what I've got. I feel like I need to wrap the download section in a function, which I tried but had no luck. Thank you all again for your help.

 

<?php virtual('/Connections/myconnect.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

$colname_birthdayfile = "1";

if (isset($_GET['Province'])) {

  $colname_birthdayfile = $_GET['Province'];

}

$colname2_birthdayfile = "1";

if (isset($_GET['City'])) {

  $colname2_birthdayfile = $_GET['City'];

}

mysql_select_db($database_sportballdb, $sportballdb);

$query_birthdayfile = sprintf("SELECT * FROM sportball_website_birthdayinfo WHERE birthday_province = %s AND birthday_city = %s", GetSQLValueString($colname_birthdayfile, "text"),GetSQLValueString($colname2_birthdayfile, "text"));

$birthdayfile = mysql_query($query_birthdayfile, $sportballdb) or die(mysql_error());

$row_birthdayfile = mysql_fetch_assoc($birthdayfile);

$totalRows_birthdayfile = mysql_num_rows($birthdayfile);

$filename = $row_birthdayfile['birthday_pdf'];

// File Download Script

 

$download_path = $_SERVER['DOCUMENT_ROOT'] . "/birthdayinfopdf";

 

// Make sure we can't download files above the current directory location.

if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");

$file = str_replace("..", "", $filename);

 

// Make sure we can't download .ht control files.

if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");

 

// Combine the download path and the filename to create the full path to the file.

$file = "$download_path/$file";

 

// Test to ensure that the file exists.

if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");

 

// Extract the type of file which will be sent to the browser as a header

$type = filetype($file);

 

// Get a date and timestamp

$today = date("F j, Y, g:i a");

$time = time();

 

// Send file headers

header("Content-type: $type");

header("Content-Disposition: attachment;filename=$filename");

header('Pragma: no-cache');

header('Expires: 0');

 

// Send the file contents.

readfile($file);

?>

<?php

mysql_free_result($birthdayfile);

?>

 

Of course, when the page is run and the variable passed I get:

 

Warning: Cannot modify header information - headers already sent by (output started at /xxxx/birthday-info-go.php:5) in /xxxx/birthday-info-go.php on line 87

 

Warning: Cannot modify header information - headers already sent by (output started at /xxxx/birthday-info-go.php:5) in /xxxx/birthday-info-go.php on line 88

 

Warning: Cannot modify header information - headers already sent by (output started at /xxxx/birthday-info-go.php:5) in /xxxx/birthday-info-go.php on line 89

 

Warning: Cannot modify header information - headers already sent by (output started at /xxxx/birthday-info-go.php:5)in /xxxx/birthday-info-go.php on line 90

 

Link to comment
Share on other sites

filetype doesn't return the content-type you're looking for I believe.

"Returns the type of the file. Possible values are fifo, char, dir, block, link, file, socket and unknown. "

 

 

You could try using mime_content_type or finfo_file

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.