Forum Moderators: coopster
So anyway the problem. I'm trying to rewrite a bunch of upload scripts for a CMS, but I don't want to have to past the same script in 5 different locations to do file type checking. There is an upload area for files, one for images, one for video media etc. etc. And I'm working on consolodating the upload script and the file type checking is something that I think I can make one function to handle all the scripts.
SO the problem is passing several variables (and they may not be variables, I'm new to PHP) to a function, one is the temporary file from the upload code block, I can't seam to get it to pass to the function so it can be checked, the other is the white list array. The code I'm currently working with did work when it was in the same page but as soon as I put it in a function in an included page, the code stopped working. This is our current code:
file: Upload.php
if(ISSET($_POST['uploaded_image']) && $_POST['uploaded_image'] ==1) {
//Set server path for upload folder, coneects to stored path in database
$SITE_GALLERY_UPLOAD_URL = SITE_CONFIG("SITE_GALLERY_UPLOAD_URL");
$fileCheck = $_FILES['upload_image']['name'];
//Create white list or allowed file types array
$whitelist = array('jpg','gif','png','jpeg');
checkExtention();
if ($worked) {
$filename = $_FILES['upload_image']['tmp_name'];
//Get file size
$size=$_FILES['upload_image']['size'];
//create time stamp
$timestamp = mktime();
//Create new file name by adding time stamp
$name=$timestamp . $_FILES['upload_image']['name'];
//Add folder path to store resized image
$target_path = $SITE_GALLERY_UPLOAD_URL . "small";
// Define image size
$w="100";
$h="100";
// Set resolution
$q="80";
//File name for Fopen in the function list
$path="small";
uploadPic($filename,$size,$name,$w,$h,$q,$target_path);
//this chmode changes the permissions of the file in the /upload_image/small/ folder only
chmod("$target_path/$name", 0666);
// medium image
$target_path = $SITE_GALLERY_UPLOAD_URL . "medium";
$w="250";
$h="250";
$q="80";
$path="medium";
uploadPic($filename,$size,$name,$w,$h,$q,$target_path);
//this chmode changes the permissions of the file in the /upload_image/medium/ folder only
chmod("$target_path/$name", 0666);
// large image
$target_path = $SITE_GALLERY_UPLOAD_URL . "large";
$w="500";
$h="500";
$q="80";
$path="large";
uploadPic($filename,$size,$name,$w,$h,$q,$target_path);
//this chmode changes the permissions of the file in the /upload_image/large/ folder only
chmod("$target_path/$name", 0666);
//Set full file name for moving the original file
$target_path = $SITE_GALLERY_UPLOAD_URL . $_FILES['upload_image']['name'];
//move temporary flie to the upload folder and rename
if(move_uploaded_file($_FILES['upload_image']['tmp_name'], $target_path)) {
//this chmode changes the permissions of the file in the /upload_image/ folder only
chmod("$target_path", 0666);
//set $upload to true for SQL upload
$uploaded = 1;
} else{
//if there was an error, set upload to false
$uploaded = 0;
}
} else {
$error[] = "Wrong file type";
}
$image_name = $name;
//if the upload was successful, write file names and paths to the database
if($uploaded ==1) {
$query = "INSERT INTO images (img_link, img_date) VALUES ('{$name}','{$timestamp}')";
mysql_query($query) or die(mysql_error());
header("Location: main.php?page=manage_images&message=Success");
die();
} else {
// if upload was unsuccessful return to manage images with an error message
header("Location: main.php?page=manage_images&message=Error");
die();
}
}
File: includes.php
function checkExtention() {
global $fileCheck, $whitelist;
$ext = explode('.',$fileCheck);
//Create if statement to make sure $ext array is not empty
if (isset($ext)) {
//grab the extension name by finding the letters after the last period
$result = count($ext)-1;
//change all letters to lower case
$new_ext = strtolower($ext[$result]);
//check extension against whitelist, if a match is found move on
if(!in_array($new_ext, $whitelist, true)) {
$new_ext = $worked;
} else {
header("Location: main.php?page=manage_images&message=Errorwrongextention");
die();
}
} else {
header("Location: main.php?page=manage_images&message=Errornofile");
die();
}
}
The error message I keep getting is this:
warning: in_array() [function.in-array]: Wrong datatype for second argument in serverpath/include.php
So what I have read is this is a needle/ haystack issue, but nether the file name or the white list are getting to the function and I'm not sure how to correct this. I'm planing on replacing our current upload code but if I can't get this to work then my plan for consolidating code wont work.
Help is much appreciated.
the error seems to be coming from here
if(!in_array($new_ext, $whitelist, true)) {
so it doesn't think that $whitelist is an array
I then look at where $whitelist is declared, outside of this function
the global is attributed to it it inside the function, that looks like the confusion
you have used global to define a new var inside the function, you aren't working on that function
if you remove $whitelist from this line
global $fileCheck, $whitelist;
then you may have access to the other var, though if you don't then you could use global on that var outside the function, which would put $whitelist in scope, or you could pass it to the function
trying to define an new verable inside the function was the problem but I was not sure how else to go about it at the time.
Thanks!