Jump to content

Simple function question


slpctrl

Recommended Posts

How would I turn my upload script into a function. I need to make it a function but I've tried a few things to no success. It seems to me that within the function I shouldn't declare $HTTP_POST_FILES['image']; or whatever, and instead should post this outside of the funciton but I donno. How would you make this into a function?

 

$dir = "/images";
$size = 20000;
if (!isset($HTTP_POST_FILES['image'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['image']['tmp_name'])) {
if ($HTTP_POST_FILES['image']['size']>$size) { echo "The file is too big<br>n"; exit; }
if (($HTTP_POST_FILES['image']['type']=="image/gif") || ($HTTP_POST_FILES['image']['type']=="image/pjpeg") || ($HTTP_POST_FILES['image']['type']=="image/jpeg") || ($HTTP_POST_FILES['image']['type']=="image/png")) {
if (file_exists($dir . $HTTP_POST_FILES['image']['name'])) { echo "The file already exists<br>n"; exit; }
$res = copy($HTTP_POST_FILES['image']['tmp_name'], $dir .
$HTTP_POST_FILES['image']['name']);
if (!$res) { echo "upload failed!<br>n"; exit; } else { echo "upload sucessful<br>n"; }
echo "File Name: ".$HTTP_POST_FILES['image']['name']."<br>n";
echo "File Size: ".$HTTP_POST_FILES['image']['size']." bytes<br>n";
echo "File Type: ".$HTTP_POST_FILES['image']['type']."<br>n";
} else { echo "Wrong file type<br>n"; exit; }
}
$my_file = $HTTP_POST_FILES['image']['name'];

 

Could I do:

 

Function uploadimg($HTTP_POST_FILES['image'])
{
$dir = "/images";
$size = 20000;
if (!isset($HTTP_POST_FILES['image'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['image']['tmp_name'])) {
if ($HTTP_POST_FILES['image']['size']>$size) { echo "The file is too big<br>n"; exit; }
if (($HTTP_POST_FILES['image']['type']=="image/gif") || ($HTTP_POST_FILES['image']['type']=="image/pjpeg") || ($HTTP_POST_FILES['image']['type']=="image/jpeg") || ($HTTP_POST_FILES['image']['type']=="image/png")) {
if (file_exists($dir . $HTTP_POST_FILES['image']['name'])) { echo "The file already exists<br>n"; exit; }
$res = copy($HTTP_POST_FILES['image']['tmp_name'], $dir .
$HTTP_POST_FILES['image']['name']);
if (!$res) { echo "upload failed!<br>n"; exit; } else { echo "upload sucessful<br>n"; }
echo "File Name: ".$HTTP_POST_FILES['image']['name']."<br>n";
echo "File Size: ".$HTTP_POST_FILES['image']['size']." bytes<br>n";
echo "File Type: ".$HTTP_POST_FILES['image']['type']."<br>n";
} else { echo "Wrong file type<br>n"; exit; }
}
$my_file = $HTTP_POST_FILES['image']['name'];
}

 

Or something of the sort?

Link to comment
https://forums.phpfreaks.com/topic/104278-simple-function-question/
Share on other sites

Here's my problem:

 

This works:

 

<?php

echo('Upload images:


<FORM ENCTYPE="multipart/form-data" ACTION="" METHOD="POST">

The file: <INPUT TYPE="file" NAME="image"><br>
<INPUT TYPE="submit" VALUE="Upload">

</FORM>');
Function uploadimg($image)
{
   $dir = "C:/wamp/www/images/";
   $size = 20000;
   $image = $_FILES['image']['name'];
   $imagetype = $_FILES['image']['type'];
   $imagesize = $_FILES['image']['size'];
   if (!isset($_FILES['image'])) exit;
   if (is_uploaded_file($_FILES['image']['tmp_name'])) {
   if ($imagesize>$size) { echo "The file is too big<br>n"; exit; }
   if (($imagetype=="image/gif") || ($imagetype=="image/pjpeg") || ($imagetype=="image/jpeg") || ($imagetype=="image/png")) {
   if (file_exists($dir . $image)) { echo "The file already exists<br>n"; exit; }
   $res = copy($_FILES['image']['tmp_name'], $dir .
   $_FILES['image']['name']);
   if (!$res) { echo "upload failed!<br>n"; exit; } else { echo "upload sucessful<br>"; }
   echo "File Name: ".$_FILES['image']['name']."<br>";
   echo "File Size: ".$_FILES['image']['size']." bytes<br>";
   echo "File Type: ".$_FILES['image']['type']."<br>";
}
else
{
   echo "Wrong file type<br>n"; exit;
}
}
   $my_file = $_FILES['image']['name'];
}
$image = $_FILES['image'];
if(isset($image))
uploadimg($image);
else
die();
?>

 

And yes, this works. But it seems to me that either I don't need to declare what $image is outside of the function, since it's already specified in the function. What I want to do is

 

if(isset($_FILES['image']))

uploadimg();

else

die();

 

Now I know that to do this, you need to assign the parameter a default value. Can this be achieved like:

 

Function uploadimg($image = $_FILES['image'])

 

???

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.