Jump to content

Php Pointers


23.12.2012
 Share

Recommended Posts

I'm trying to create a photo uploading function for my script. Among other things, it is also supposed to modify three variables used outside the function ($message, $form, $menu). I tried using the global keyword, but it didn't work. I'm now using pointers and they don't seem to work either. Any helpful ideas? Thanks in advance!add_contacts.php

...$photo = photo_upload($photo_name, $photo_size, $photo_temp, $photo_type, $message, $form, $menu);...

photo_upload.php

// Define the functionfunction photo_upload($name, $size, $temp, $type, &$message, &$form, &$menu) {	// Check to see if the photo has been uploaded by checking the size. 	// If the size is bigger than 0kb, proceed with the script.	if($size > 0) {		// If the file is smaller than 1MB (1024kb), proceed with the script		if($size < 1024 * 1024) {			// If the file type is supported (JPG, JPEG, PNG, GIF), proceed with the script			if($type == 'image/jpg' || $type == 'image/jpeg' || $type == 'image/png' || $type == 'image/gif') {				// Rename the photo, adding the timestamp to its name				$photo = time() . $name;								// Move the file from its temporary location to the photos/ directory				move_uploaded_file($temp, 'photos/' . $photo);								// Return the photo name for later use in @link {add_contacts.php}				return $photo;			} // END of TYPE CHECK						// If the file type is not supported, display an error message, display the form and display the menu			else {				// Display an error message				$message = "Please upload a JPEG, PNG or GIF image";				// Display the form				$form = true;				// Display the menu				$menu = true;			} // END of TYPE CHECK		} // END of SIZE CHECK				// If the file is larger than 1MB (1024kb), display an error message, display the form and display the menu		else {			// Display an error message			$message = "Please upload a photo which is smaller than 1MB or 1024kb";			// Display the form			$form = true;			// Display the menu			$menu = true;		}	} // END of UPLOAD CHECK} // END of photo_upload() FUNCTION

Link to comment
Share on other sites

For what it is, the function looks fine. How it works in context might make a big difference. You may need to post more stuff.Have you considered returning multiple values using array syntax? Example 2 here shows a convenient method for that.Passing arguments by reference in order to modify values external to a function is a technique I remember with horror from days when I programmed in C and it was not possible to return anything but a single scalar value. It's not wrong, but it makes very ugly code. IMHO

Edited by Deirdre's Dad
Link to comment
Share on other sites

Note that PHP, as a very high-level language, doesn't technically use pointers - it has a symbol table instead, I believe.

Link to comment
Share on other sites

You've also got your function set to return a value in one place but not in other places. Your function will only return a value in one situation. If you're going to have it return a value then it's probably best to have it return a value regardless of what happened. You may not even need all of the variables passed by reference, I'm not sure why you need the menu and form variables, for example. It seems like you only set those variables to true if there was an error, so why not check for an error where this function was called and set those values outside of the function? If you return a blank filename if there was an error, then you can use the return value to determine if there was an error. You can also pass back an array like Dad mentioned.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...