What is "->"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Imosa
Forum Newbie
Posts: 12
Joined: Tue May 04, 2010 11:56 am

What is "->"

Post 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.
Last edited by Imosa on Tue May 04, 2010 2:16 pm, edited 1 time in total.
rnoack
Forum Commoner
Posts: 34
Joined: Mon May 03, 2010 12:38 am

Re: What is "->"

Post by rnoack »

i do not see a -> in your code
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: What is "->"

Post 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(); 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What is "->"

Post 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.
Imosa
Forum Newbie
Posts: 12
Joined: Tue May 04, 2010 11:56 am

Re: What is "->"

Post 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".
Imosa
Forum Newbie
Posts: 12
Joined: Tue May 04, 2010 11:56 am

Re: What is "->"

Post 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?
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: What is "->"

Post 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."
Post Reply