Jump to content

[SOLVED] Image filter


emediastudios

Recommended Posts

I have this code.

The image size filter works but not the file extension filter.

Would like a check to see if file exsists too.

Thanks for any help

 

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr(strrchr($file_name,"uploadFile".$x),1));
if(in_array($extension, $valid))
	return TRUE;
else
	return FALSE;
}


//filter by size,
function valid_size()
{
if($_FILES['uploadFile'. $x]['name']['size'] > 1048576)
	return FALSE; //Over one mega
else
	return TRUE;
}

Link to comment
Share on other sites

thanks, your a wizard, but how would include that code in mine, code below

 

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);


// Connects to your Database 
mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 





$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);

}



//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr(strrchr($file_name,"uploadFile".$x),1));
if(in_array($extension, $valid))
	return TRUE;
else
	return FALSE;
}


//filter by size,
function valid_size()
{
if($_FILES['uploadFile'. $x]['name']['size'] > 1048576)
	return FALSE; //Over one mega
else
	return TRUE;
}


// check if successfully copied

if($copy){
print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
//Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;

} else{
echo "$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br />
<br />
Please go back and try agian";
}
// end of loop
?>



Link to comment
Share on other sites

i added the code, it still uploads the files, Does it check in my ../images folder?

 

Change from:

	$files = glob("*.$extension");

 

To:

	$files = glob("../images/*.$extension");

 

Basically, make the ../images/ work based on where the script is running.

Link to comment
Share on other sites

Another reason I use GetImageSize and not file_exists is because file_exists only checks on your server, but GetImageSize works for both absolute and relative urls.

 

And also, she wasn't checking to see if the file itself existed (first script), but rather if the filename existed (second script)

Link to comment
Share on other sites

I added this to my code

//filter file exists 
function existingfile($filename){
	files = glob("../images/*.$extension"); /////////////// line 46
if(in_array($filename, $files))
	return TRUE;
else
	return FALSE;
}

and i get this error

 

 

Parse error: syntax error, unexpected '=' in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\add_test.php on line 46

 

Link to comment
Share on other sites

Another reason I use GetImageSize and not file_exists is because file_exists only checks on your server, but GetImageSize works for both absolute and relative urls.

 

And also, she wasn't checking to see if the file itself existed (first script), but rather if the filename existed (second script)

 

Ahhh normally I dont like to argue on PHPFreaks since I don't think forums are a good place for arguements, but I'm in the right mood so here it comes:

 

I have this code.

The image size filter works but not the file extension filter.

Would like a check to see if file exsists too.

Thanks for any help

 

There is the thread starter asking how to see if a file exists....

 

 

 

That glob example will simply build an array of filenames in a folder, yes?  That means that if a file exists in the folder then the file will be added to the array.  If you didn't get what I just said, read it until it hits you.  With file_exists you can have the same problem, but due to the extra processing time involved with the glob thing, you're more likely to come across the problem of the file being created inbewteen checking if it doesn't exists and creating it.

 

I've never used ImageGetSize, but I would assume that unless you plan on using the extra data it provides, there is no advantage from using it over file_exists.

 

Another reason I use GetImageSize and not file_exists is because file_exists only checks on your server, but GetImageSize works for both absolute and relative urls.

 

I would assume you mean absolute and relative filepaths, not URLs since there is no reason for the thread creator to be entering web addresses in his/her script.  file_exists('../lol.txt') and file_exists('/some/folder/lol.txt') are both valid.  A relative and absolute path ;p.

 

 

Bleh well I'm done now.

Link to comment
Share on other sites

I added the code and made the changes, thanks everyone, i dont get errors now but the image still uploads and overwrites the old one.

 

Here is my full code

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);


// Connects to your Database 
mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 





$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);

}
//filter file exists 
function existingfile($filename){
	$files = glob("../images/*.".$extension); /////////////// line 46
if(in_array($filename, $files))
	return TRUE;
else
	return FALSE;
}



//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr(strrchr($file_name,"uploadFile".$x),1));
if(in_array($extension, $valid))
	return TRUE;
else
	return FALSE;
}


//filter by size,
function valid_size()
{
if($_FILES['uploadFile'. $x]['name']['size'] > 1048576)
	return FALSE; //Over one mega
else
	return TRUE;
}


// check if successfully copied

if($copy){
print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
//Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;

} else{
echo "$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br />
<br />
Please go back and try agian";
}
// end of loop
?>



Link to comment
Share on other sites

....  You never check if the file exists... You have the functions in there, but you never use them...

 

for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);

}

 

That never checks if the file exists....

 

Something like the following should work:

 

for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
if(file_exists($path . $file_name) || !valid_ext($file_name)) {
    echo "The file {$file_name} already exists.";
}
else {
    $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);
}

}

 

Also, copy is only set to hold the value of the last move, so if the last one moves correctly and the first 8 don't then the if($copy) idea is defeated entirely.

Link to comment
Share on other sites

I added your code

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);


// Connects to your Database 
mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 





for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
if(file_exists($path . $file_name) || !valid_ext($file_name)) {
     echo "The file {$file_name} already exists.";
}
else {
     $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);
}

}




// check if successfully copied

if($copy){
print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
//Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;

} else{
echo "$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br />
<br />
Please go back and try agian";
}
// end of loop
?>

 

But now it doesnt post the files or record and i get this error

 

The File(s) could not be uploaded!

The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!

 

Please go back and try agian

Link to comment
Share on other sites

I have gone all day on this one and it is driving me crazy>..Please someone.

 

My Code

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);


// Connects to your Database 
mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 





$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);

}




//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr(strrchr($file_name,"uploadFile".$x),1));
if(in_array($extension, $valid))
	return TRUE;
else
	return FALSE;
}


//filter by size,
function valid_size()
{
if($_FILES['uploadFile'. $x]['name']['size'] > 1048576)
	return FALSE; //Over one mega
else
	return TRUE;
}


// check if successfully copied

if($copy){
print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
//Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$ID','$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;

} else{
echo "$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br />
<br />
Please go back and try agian";
}
// end of loop
?>



 

From all the responses proir to this, im sure there is an answer in there somewhere, i'm a begginer and still learning.

Could some one paste the nessessary code in mine and ill see if it works.

 

Thanks so much

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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