Jump to content

Image upload filter


emediastudios

Recommended Posts

I have this code i usethat works fine, except that any filr can be uploaded and any size.

How would i add some code to check that the file is a jpeg, gif or png and is under 1 meg?

 

Thanks in advance

 

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']; 
$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", "password") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 


//Writes the information to the database

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


$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);

}


// check if successfully copied

if($copy){
print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
} else{
echo "$file_name | could not be uploaded!<br>";
}
// end of loop
?>



Link to comment
https://forums.phpfreaks.com/topic/70867-image-upload-filter/
Share on other sites

Hello,

 

On page submit you can check for the file size, you will be getting the currently uploaded file's size in '$_FILES' variable and also from the $_FILES['img']['name'] parameters you can find the extension for the current file.

 

So in short, on page submit check the size and extension of the image and if it suits your requirements the allow upload else redirect on the same page with error message.

 

Regards,

Link to comment
https://forums.phpfreaks.com/topic/70867-image-upload-filter/#findComment-356277
Share on other sites

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.