Jump to content

UPLOAD only certain file type


johnseito

Recommended Posts

Here is what I have.

 

<?php

 

if (isset($_POST['submit'])) {

 

$target = "pictures/";

 

$target = $target . basename( $_FILES['uploaded']['name']) ;

$ok=1;

 

 

$allowed_type = array('image/jpg','image/tif','image/gif','image/pjpeg','image/png','image/jpeg');

if(in_array($uploaded_type,$allowed_type)){

 

 

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

{

echo "<font color =red> The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded</font><br><br>";

}

else {

echo "<font color=red> Sorry, there was a problem uploading your file. Please try again!</font><br><br>";

}

 

}else

{

echo "<font color=red>Sorry, you may only upload JPEG files!</font><br><br>";

$ok=0;

}

 

}

?>

 

<form enctype="multipart/form-data" action="upload.php" method="POST">

Please choose a file: <input name="uploaded" type="file" /><br />

<input type="submit" name="submit" value="Upload" />

</form>

Link to comment
Share on other sites

try php_info();

 

see if you can find anything that prevents you from uploading larger files, also check it to make suer your editing the correct php.ini file (more often than not people have multiple copies of php.ini in the php bin/system32 folder/windows folder etc)

 

other than that im not sure but maybe your web server (ie apache) is causing the problem.

 

--- also, what operating system/php version/apache vers you running and what exactly happens when you try to upload files larger than 2mb?

Link to comment
Share on other sites

I changed all the php.ini files to this

 

file_uploads = on

upload_max_filesize = 100M

max_execution_time = 240

max_input_time = 240

memory_limit = -1

post_max_size = 100M

 

and it still doesn't work

 

what operating system/php version/apache vers you running and what exactly happens when you try to upload files larger than 2mb?

 

Operating system  ---- vista

php version ---------  PHP 5.2.3

apache version - -----http server 2.2.4

 

It just doesn't upload it, it pretty much gives me an error, because in my code if it doesn't do this

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

it will give me an error.  When it upload successfully I will not see the error and I can see the file uploaded in the directory.

Link to comment
Share on other sites

your form - post size should also be more.

to increase it use php.ini and set post_max_size.

 

upload_max_filesize = 10M ;

post_max_size = 20M ;

 

and remember to check your memory_limit notable point is :memory_limit should be larger than post_max_size

 

Regards

Link to comment
Share on other sites

try this - its untested, and incomplete, but it should get you on the right track:

 

<?php
//check that there were no errors, and that something was submitted

if(!empty($_FILES) && $_FILES['attachment']['error'] == 0) { //['attachment'] is the name you gave your file input box
  //no errors, and a file is present - assign and check the values:
  $file_name = $_FILES['attachment']['name'];
  $file_type = $_FILES['attachment']['type'];
  $file_size = $_FILES['attachment']['size'];
  $file_tmpname = $_FILES['attachment']['tmp_name'];

  $error = array();
  //set allowed file types and check against the uploaded file
  $allowed_types = array('image/jpeg','image/pjpg','image/png','image/gif');
  if(!in_array($allowed_types, $file_type)) { //not an allowed file type
    $error .= 'type';
  }
  //next set the max filesize and check it against your file. 1048576 bytes = 1 mb
  $max_size = 1048576;
  if($max_size < $file_size) { //if the file is larger then the max size allowed:
    $error .= 'size';
}
//and so on....
  
//File checking done, lets see if there were any errors:
   if(!empty($error)) { //there was an error
     //some code here explaining what the errors were and output to the user
  } else { //no errors
    //upload the file
  }
}
//fin
?>

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.