Re: [NEWBIE] Trying to create a function from an existing script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dave wrote:
PHP List,

The Situation:
I'm trying to make a page where users can upload an image. I've located on the internet a script that is simple enough for me to use, and I can build upon it to customize for my needs.
The script as it is on the web site I got it from is a stand alone .php file in the same directory as the form that calls it. In order to keep my site organized and consistent with what I've constructed so far, I'd like to make the code into a function, included in my directory with other image manipulation functions.


The Problem:
I'm not able to create the right syntax to pass the $HTTP_POST_FILES variable to the function. So the function gets called, but then it stops very early because what I end up giving it is essentially an empty array, so it thinks there's no file to handle.


What I've Tried So Far:
I've tried putting $HTTP_POST_FILES directly into the function parameters, and I've tried renaming $HTTP_POST_FILES as a different array before passing it to the function. But I'm essentially shooting in the dark, trying random variants of syntax.


   The Question:
   How do I pass the $HTTP_POST_FILES to the function?

   Any help would be much appreciated.

For Reference:
What follows is the form as it appears in the HTML, and then after that is the function as it appears in a separate .php file:


<form enctype="multipart/form-data" action="(EmptyReference!)" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
<input name="upfile" type="file">
<input name="store_dir" type="hidden" value="images">
<input type="submit" name="Upload" value="submit">
<?php
if(isset( $Upload ))
{
upload_img();
}
?>
</form>


----------

function upload_img()

define your function as upload_img($name, $store_dir)

then later on use $HTTP_POST_FILES[$name], your function will be more universal.

{
global $HTTP_POST_FILES;
//  Check to see if a file has been included when the form was submitted
if( strlen($HTTP_POST_FILES['upfile']['name']) < 1 )
{
  echo("Error! No files to upload... Exiting");
  exit();
}

// Check to see if the directory specified in the store_dir
// input field is a valid directory, if it isn't clear up
// the temporary files from the server.
if( !is_dir($store_dir) )
{
  echo("Specified directory is not valid... Exiting");
  unlink($HTTP_POST_FILES['upfile']['tmp_name']);
  exit();
}

// Copy the temporary file to the specified directory and
// give it the original name it had on the source machine.
if( copy($HTTP_POST_FILES['upfile']['tmp_name'],$store_dir.$HTTP_POST_FILES['upfile']['name'])

Use move_uploaded_file($HTTP_POST_FILES[$name]['tmp_name'], $store_dir . basename($HTTP_POST_FILES[$name]['name']);


move_uploaded_file - so you can be sure it's really an uploaded file
basename - 'name' is provided by the user and it can contain anything, for example ../index.php. If $store_dir is 'upload/', then you would overwrite your index.php file


)
{
echo("Uploaded ".$HTTP_POST_FILES['upfile']['name']." successfully.");
}
// If the copy function fails output a message.
else
{
echo("Upload of ".$HTTP_POST_FILES['upfile']['name']." to ".$store_dir." failed!!!!<BR>");
}


// Finally tidy up behind yourself and delete the
// temporary file from the server.

unlink($HTTP_POST_FILES['upfile']['tmp_name']);

temporary files are cleaned up automaticly. And if move_uploaded_file is used it no longer exists so it will trigger a warning


}


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux