Page 1 of 1

What is "->"

Posted: Tue May 04, 2010 12:12 pm
by Imosa
I'm trying to make a website where people can download a folder of pictures off of my server.
I went online and found this tutorial on how to zip up files and then force download them. I was excited with how well this fit my problem and typed up this php document:

Code: Select all

<html>
<body>
<?php

function getfiles() {
	$dir = "uploads/";
    $zipname = "download.zip";
	$pictures;
    
	//compiles a list of all the files in the uploads foulder
	if(is_dir($dir)){
		if($dh = opendir($dir)) {
	    	$i = 0;
	    	while(($file = readdir($dh)) !== false) {
	        	$pictures[$i] = $file;
	            $i++;
	        }
	        closedir($dir);
	    }
	}
	
    zipFilesAndDownload($pictures,$zipname,$dir);
}

//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
      //create the object
      $zip = new ZipArchive();
      //create the file and throw the error if unsuccessful
      $res = $zip->open($archive_file_name, ZIPARCHIVE::CREATE);
      if ($res===FALSE) {
        exit("cannot open" . $archive_file_name . "\n");
      }

      //add each files of $file_name array to archive
      foreach($file_names as $files)
      {
        $zip->addFile($file_path.$files,$files);
      }
      $zip->close(); 

  //then send the headers to foce download the zip file
  header("Content-type: application/zip");
  header("Content-Disposition: attachment; filename=$archive_file_name");
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile("$archive_file_name");
  exit;
}

getfiles();

?>
</body>
</html> 
Upon running this code, the browser just prints out everything after the first "->" as if it's just text.

What exactly does "->" do and why does it seem to close my php tag?
Googleing doesn't return much information. I could only find that it calls functions or parameters of an object which if fine but doesn't really help me.

Re: What is "->"

Posted: Tue May 04, 2010 12:23 pm
by rnoack
i do not see a -> in your code

Re: What is "->"

Posted: Tue May 04, 2010 12:54 pm
by flying_circus
What happens when you change

Code: Select all

//create the object
  $zip = new ZipArchive();
  //create the file and throw the error if unsuccessful
  $res = $zip.open($archive_file_name, ZIPARCHIVE::CREATE);
  if ($res===FALSE) {
    exit("cannot open" . $archive_file_name . "\n");
  }

  //add each files of $file_name array to archive
  foreach($file_names as $files)
  {
    $zip.addFile($file_path.$files,$files);
  }
  $zip.close(); 
to:

Code: Select all

//create the object
  $zip = new ZipArchive();
  //create the file and throw the error if unsuccessful
  $res = $zip->open($archive_file_name, ZIPARCHIVE::CREATE);
  if ($res===FALSE) {
    exit("cannot open" . $archive_file_name . "\n");
  }

  //add each files of $file_name array to archive
  foreach($file_names as $files)
  {
    $zip->addFile($file_path.$files,$files);
  }
  $zip->close(); 

Re: What is "->"

Posted: Tue May 04, 2010 2:01 pm
by requinix
Assuming you have $zip->open and $zip->addFile and the first thing you see on your page is "open" then PHP hasn't been properly installed. Go through the instructions again and make sure you did everything.

Re: What is "->"

Posted: Tue May 04, 2010 2:23 pm
by Imosa
Yes, I'm sorry. I was talking about it with a friend and he just wanted to try replacing the '->' with a '.'. What flying_circus posted was right and I edited my original post with the correct code.
tasairis wrote:Assuming you have $zip->open and $zip->addFile and the first thing you see on your page is "open" then PHP hasn't been properly installed. Go through the instructions again and make sure you did everything.
I was afraid of something like that. Is there any way to test if php is working correctly? I've already written other things and those all work fine, they just don't contain additional '>'s.

Also, can someone tell me exactly what -> does?

P.S. What is one of these ('>') called, anyway? Can't just be a "greater-then-symbol".

Re: What is "->"

Posted: Tue May 04, 2010 4:02 pm
by Imosa
Ok, I figured out what the problem was. I was running the website through the file instead of over the internet. I still don't get why php is like that. Why can't it just be run like regular html can?

Re: What is "->"

Posted: Tue May 04, 2010 8:43 pm
by minorDemocritus
Because HTML isn't "run". HTML is markup... it's just data to display, and information on how to display it.

PHP is a programming language... it is run by the webserver. Your browser knows how to display HTML, it doesn't know how to run PHP. PHP is run on the webserver (it's 'server-side'), and then the webserver takes the HTML that PHP outputs, and sends it to the browser.

The '->' symbol is the way to call a function or access a variable inside an object. Check out the PHP docs for Classes and Objects. It's tricky at first, but you'll get it. :mrgreen:

One more hint... the header() function calls aren't going to work. Read this http://php.net/manual/en/function.header.php. Notice the part that says "Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP."