Jump to content

[SOLVED] Image filter


emediastudios

Recommended Posts

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

 

I did this and had a play around and it works a little, Tells me if the file exists and i get an error i have echo'd, but if the file doesnt exist in continues on with readind th code and i get this error.

 

Fatal error: Call to undefined function valid_ext() in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\add_test.php on line 38

Link to comment
Share on other sites

change

if(file_exists($path . $file_name) || !valid_ext($file_name)) {

to

if(file_exists($path . $file_name) ) {

 

*note, i haven't read the whole thread

 

Awesome  ;D ;D ;)

 

Thats my check if file exists problem fixed.

 

Now file size and type.

 

I have this as a image size filter.

 

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

does not work, any ideas?

Thanks 4 ur help ;)

Link to comment
Share on other sites

try this sample script

 

<?php

//test
$Name = $_FILES['testfile']['name'];
$Size = $_FILES['testfile']['size'];

//Test Check
if(valid_ext($Name))
{
echo "valid ext";
}else{
echo "invalid";
}
echo "<br>";
if(valid_size($Size))
{
echo "valid size";
}else{
echo "invalid";
}

//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}

?>

Link to comment
Share on other sites

Thanks for that, i added your code but it echo,s file extension invalid when it is and echo,s valid size when it isnt ???

 

What would be the best way to implement your code in mine?

 

Here is mine

<?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()) ; 

// Uploads Images
$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);
if(file_exists($path . $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
?>


 

I changed your code to look like this, is this right?

 

<?php 
//test
$Name = $_FILES['uploadFile'. $x]['name'];
$Size = $_FILES['uploadFile'. $x]['size'];
?>

Link to comment
Share on other sites

try this

 

<?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()) ; 

// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop

for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
if(valid_ext($file_name))
{
echo "valid ext";
}else{
echo "invalid";
}
echo "<br>";
if(valid_size($Size))
{
echo "valid size";
}else{
echo "invalid";
}

// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
if(file_exists($path . $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


//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}
?>


 

Link to comment
Share on other sites

Your a champ super Guru.

Works!!! ;D

Ive been on this for weeks.

Only one thing, it still copies the file if it is of invalid ext but is of valid size.

It echo's correctly and reconizes the file if is valid or invalid in all categories, size, ext, and file exsists.

Can i get the code to stop processing the rest of the code when returns invalid?

Like ( if invalid ext or invalid size print url (error.php) and stop proceessing rest of code?)

Thanks for your help, i am stoked!! ;)

Link to comment
Share on other sites

try this (quick fix)

 

<?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()) ; 

// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop

for($x=0;$x<$uploadNeed;$x++)
{
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
$Valid = false;
if(valid_ext($file_name))
{
	echo "valid ext";
	$Valid = true;
}else{
	echo "invalid Type";
}
echo "<br>";
if(valid_size($Size))
{
	echo "valid size";
}else{
	$Valid = false;
	echo "invalid Size";
}
if($Valid)
{
	// strip file_name of slashes
	$file_name = stripslashes($file_name);
	$file_name = str_replace("'","",$file_name);
	if(file_exists($path . $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


//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}
?>

Link to comment
Share on other sites

You live up to your name "Super Guru"

Absolute Genuis.

You have solved my problem(s)

Just one last thing, dont mean to ask to much  ;) But on the bottom of my code where it says "Please go back and try agian"

How can i make that an active link to "property_add.php.

Thanks for everything, cant say how much you have helped.

Link to comment
Share on other sites

added an error reporting thing..

*untested

 

<?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()) ; 

// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop
$errors = array();
for($x=0;$x<$uploadNeed;$x++)
{
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
$Valid = false;
if(valid_ext($file_name))
{
	$Valid = true;
}else{
	$errors[] = "$file_name has an Invalid FileType";
}
echo "<br>";
if(!valid_size($Size))
{
	$Valid = false;
	$errors[] = "$file_name is too large";
}
if($Valid)
{
	// strip file_name of slashes
	$file_name = stripslashes($file_name);
	$file_name = str_replace("'","",$file_name);
	if(file_exists($path . $file_name) ) {
		$errors[] = "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')") ;
}
if(count($errors)>0)
{
echo "The following File(s) could not be uploaded,<BR>";
foreach($errors as $err)
{
	"$err <br>";
}
echo "The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br />
<br />
Please go <a href=\"property_add.php\">back</a> and try again";
}
// end of loop


//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","jpe","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}
?>

 

 

EDIT: OOOOPS forgot the s on errors

 

EDIT: also i have never heard of a jpe file!!

Link to comment
Share on other sites

With the last code you supplied me, as with the first, if i attempt to upload more than one file, say 4 images and i zip file or non image file, it uploads all the valid files and not the invalid files but still inserts the record into my database,

Is there a way that if one file is not of valid ext or valid size that it does not upload anything, and doesnt insert a record?

 

Thanks Super Guru ;D

 

Link to comment
Share on other sites

ahh very true (if the invalid file is AFTER a valid one)

OK simple fix..

 

 

Do one or both of the below (each will fix the proble)

 if($copy)

to

 if($copy && $Valid)

 

and/or

 

	if(file_exists($path . $file_name) ) {
		echo "The file {$file_name} already exists.";

to

		if(file_exists($path . $file_name) ) {
		echo "The file {$file_name} already exists.";
		$copy= false;

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.