SEARCH
  Web Programming PHP Image Class: Function Upload

Image Class: Function Upload

nathanael PHP May 30, 2007

function upload($Request_Name, $Img_Dir){
$Image = $_FILES[$Request_Name]['name'];
$sub_Image = str_replace(" ", "_", $Image);

//Make sure we dont overright a file
$dir_arr = Array();
$handle = opendir("$Img_Dir");
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dir_arr[] = $file;
}
}
if(in_array("$sub_Image", $dir_arr)) {
$_SESSION['Message'] = "$sub_Image Already exists";
return FALSE;
}
//Save Full Size Image
$copy = copy ($_FILES[$Request_Name]['tmp_name'], "$Img_Dir".$sub_Image);
if(!$copy) {
$_SESSION['Message'] = "an image must be select";
return FALSE;
}
return $sub_Image;
}

This is the function we call to upload an image. We pass 2 variables to this. $Request_Name and $Img_Dir

$Request_Name, is the name of the input box that uploads the file (ie <input type="file" name="img" />).

$Img_Dir is simply the directory the file will be uploaded to. This direcotry will need a mode of 777. most ftp programs will let you change the mode of a directory, or you can use command line and do chmod 777 directory.

Now we have

$Image = $_FILES[$Request_Name]['name'];
$sub_Image = str_replace(" ", "_", $Image);

What this does is first sets $Image to the name of the uploaded file. Then $sub_Image is created which is the name of the upload file with spaces (if any) replaced with underscors (_).

Next we have

//Make sure we dont overright a file
$dir_arr = Array();
$handle = opendir("$Img_Dir");
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dir_arr[] = $file;
}
}
if(in_array("$sub_Image", $dir_arr)) {
$_SESSION['Message'] = "$sub_Image Already exists";
return FALSE;
}

What this does is creats an array of all the files in the directory we're uploading the image to. It then checks to see if the uploaded file shares the same name as any image in the directory. If there is an image with the same name, a session message is sent, the code quits, and returns to the page the function was called from. You dont have to use the session message, you could edit the code to return a different variable, I just find the session message the easiest way to do this. If you want to use the session message, there are 2 things you need to do. In the file that calls the function add

session_start();

to the top of the page (right after <?);

you will also need a way to display the message, which can be done by doing this:

<?
if(isset($_SESSION['Message'])){
echo $_SESSION['Message'];
unset($_SESSION['Messge];
}

the next part we have is

//Save Full Size Image
$copy = copy ($_FILES[$Request_Name]['tmp_name'], "$Img_Dir".$sub_Image);
if(!$copy) {
$_SESSION['Message'] = "an image must be select";
return FALSE;
}

This is the part that uploads the image. if the image cannot be copied, it means an image wasnt select, there for it kicks back with the session message again, and quites the code.

Finally we have

return $sub_Image;
}

This will return the name of the final image (with spaces replaced with underscores). the only reason I do this, is so I can pass it to the resisze funtion; which you'll see later.

Now calling the function is pretty easy. there are a few things that are very important though.

First, the form you set up to upload the image should be similar to this.

<form method="post" action="file php code is in" enctype="multipart/form-data">

The enctype="multipart/form-data" is very important. if you dont have this, the image wont upload.

If you use the session message; you need to have session_start(); at the beginning of the file.

Here is a sample file of the form and upload script

<?
session_start();
require_once 'func_images.php';

$obj_images = new images();
$GLOBALS['img_root'] = "image_dir";

switch($_REQUEST['case']){
default :
if(isset($_SESSION['Message'])){
echo $_SESSION[Message];
unset($_SESSION['Message']);
}
echo<<<EOF
<form method="post" action="$PHP_SELF" name="theForm" enctype="multipart/form-data">
<input type="file" name="img" />
<input type="hidden" name="case" value="submit" />
<input type="submit" />
</form>
EOF;
break;
case 'submit' :
$upload = $obj_images->upload("img", $GLOBALS['img_root']);
header("location: $PHP_SELF");
break;
}
?>

Next function

subscribe to newsletter