Jump to content

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

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.