Jump to content

Recommended Posts

i can upload an image in firefox and i just tested it in ie and for some reason i just get the error saying the file is too large. but i think that is the error that is displayed if nothing gets uploaded. i am unsure why it doesnt work in ie any ideas would be helpful:

 

<?php

$max_size=2*1024*1024;

// Check if a file has been uploaded
if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)
{
     // Make sure the file was sent without errors
     if($_FILES['uploaded_file']['error'] == 0)
    {
      $target_path = "images/";
      $target_path = $target_path . basename( $_FILES['uploaded_file']['name']);    
      
      if(!file_exists($target_path)){

   if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))
      {
         
         // --- IMPORTANT --- //
                        $image = $target_path;
                        $maxHeight = 100; $maxWidth = 90;
                        include 'thumbnail_function.php'; // Include the function file
                        thumbnail($image, $maxWidth, $maxHeight); // Call the function with a width of 90...
                        $maxWidth = 500; $maxHeight = 400; // Redefine the size.
                        include 'view_function.php';
                        viewed($image, $maxWidth, $maxHeight); // Call the function again with a width of 350...
                        // --- IMPORTANT --- //
         
          echo "The file ".  basename($_FILES['uploaded_file']['name']). " has been uploaded";
                            
         $dbLink = new mysqli('79.170.44.131', 'web131-gallery', 'ghazarian', 'web131-gallery');
            if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
                                 }          
       
             // Gather all required data
             $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
             $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
             $size = intval($_FILES['uploaded_file']['size']);
             $image_path = $dbLink->real_escape_string($target_path);
                $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']);
             $desc = $dbLink->real_escape_string($_POST['description']);
             $image_path = $dbLink->real_escape_string($target_path);
             $thumb_path = $dbLink->real_escape_string($tmb_name);
             $viewed_path = $dbLink->real_escape_string($viewed);
          
          //query to insert the data i had gathered into the database
   $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `description`, `thumbnail_path`, `viewed_path`)
             VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}', '{$viewed}')";
          
          //executes the query
          $dbLink->query($query);
      }
   }
     
   else
       {
           echo '<h1>Error</h1>';
           echo 'A file with the same name exists please change the file name and try again';
       }
}
  
  else
        {
            echo '<h1>Error</h1>';
             echo 'A file was not sent to the database';
          }
}

else
        {
            echo '<h1>Error</h1>';
             echo 'The file is too large. Please upload an image no larger than 2mb';
         }
      
// Echo a link back to the main page
echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
?> 

 

<form action="add_file.php" method="post" enctype="multipart/form-data">
		 <input type="hidden" name="MAX_FILE_SIZE" value="<?php $max_size; ?>"/>
         <div id="addfile">
         <input type="file" name="uploaded_file" /><br />
         </div>
         
         <div id="dropdown">
	 <select name="gallery_type" id="gallery_type">
        	<option>Please Select an Album
		<?php
			for($i = 1; $i < $num_results+1; $i++) {
			echo "<option value=\"" . $galleryTypes[$i]['gallery_type_id'] . "\">";
			echo $galleryTypes[$i]['gallery_type_desc'];
			echo "</option>\n";
			}
		?>
            </option>
	</select>
        </div>
         
	<div id="desc">
        <textarea name="description" cols="40" rows="5" onkeyup="textLimit(this,3000);"> </textarea>
        </div>
         
         <div id="upload">
           <input type="submit" value="Upload" />
         </div> 


</form>

You have lumped the tests for the mime type and the size together and output one generic error message when either test fails.

 

Separate the tests and give them their own error message so that you know which validation test is failing and display the actual value that failed as part of the error message.

 

You will probably find that different browsers send different mime types, depending on the type of the file. You probably have a progressive jpeg image, which will result in a pjpeg mime type.

 

You also need to check for upload errors first before you can attempt to access any of the uploaded file information. See this recent post for some code to get you started on error checking - http://www.phpfreaks.com/forums/index.php/topic,292945.msg1386624.html#msg1386624

thanks for that. i changed a bit of my code to incorporate the pjpeg file type. the only problem im having is seperating the if statement.

 

if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/pjpeg|image\/pjpg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)

 

thats what i have now. but i wouldnt know how to seperate that if statment, would i put in another if statement below it like this?

 

if(isset($_FILES['uploaded_file']) &&  $_FILES['uploaded_file']['size']<= $max_size)
{
if(preg_match("/image\/jpeg|image\/pjpeg|image\/pjpg|image\/jpg/i",$_FILES['uploaded_file']['type'])
	{

When validating user input, where several things could be wrong at one time, it is usually better to create an array to hold the errors and to use a series of separate tests (rather than nesting tests.) You can then just test if the error array is empty to not before processing the data from the form or displaying the errors -

 

<?php
// Application values used for validation
$types = array(); // array of permitted mime types
$types[] = 'image/gif';
$types[] = 'image/jpeg';
$types[] = 'image/pjpeg';
// define the maximum and minimum permitted file size
$max_size = 2000000;
$min_size = 0;
// general settings
$index_name = 'uploaded_file'; // the index name of $_FILES['xxxxxx'] (for a single file upload)


$errors = array(); // create an array to hold validation errors
if($_FILES[$index_name]['size'] > $max_size){
$errors[] = "The size of the uploaded file {$_FILES[$index_name]['size']} is greater than the maximum permitted size $max_size<br />";
}
if($_FILES[$index_name]['size'] < $min_size){
$errors[] = "The size of the uploaded file {$_FILES[$index_name]['size']} is less than the minimum permitted size $min_size<br />";
}			
if(!in_array($_FILES[$index_name]['type'], $types)){
$errors[] = "The mime type of the uploaded file {$_FILES[$index_name]['type']} is not one of the permitted types<br />";
}

// process the file if there were no validation errors 
if(empty($errors)){
// no errors, process the form data here...

} else {
// a validation error occurred -
echo "The following validation errors occurred -<br />";
foreach($errors as $error){
	echo $error;
}
}
?>

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.