Jump to content

Recommended Posts

Every time i try and move code around i always end up getting the error check message somehow with the file type (the image). It will even upload correctly and still give me the invalid file type message. Tried so many methods and just keep getting frustrated. Any help would be appreciated.

 

<form action="<?php echo $_SERVER['php_SELF'];?>" method="post" enctype="multipart/form-data">
<table class="main" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><font color="#002157"><b>Upload Image:</b></font>&nbsp&nbsp</td>
<td><input type="file" size="40" name="file" id="file"></td>
</tr>

<tr>
<td align="right"><font color="#002157"><b>Image URL:</b></font>&nbsp&nbsp</td>
<td><input type="text" size="40" name="URL"></td>
</tr>

<tr>
<td>&nbsp</td>
<td><br><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>

<?php
list($width, $height) = getimagesize($_FILES['file']['tmp_name']);

if($_FILES["file"]["name"]!=""){

if ($_FILES["file"]["type"] != "image/gif"
|| $_FILES["file"]["type"] != "image/jpeg"
|| $_FILES["file"]["type"] != "image/pjpeg"
|| $_FILES["file"]["type"] != "image/png"
|| $_FILES["file"]["type"] != "image/x-png"){
	echo "Invalid file type.<br>";
}
    if ($_FILES["file"]["error"] > 0){
    	echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    if($width!=300 || $height!=200){
    	echo "Bad dimensions.<br>";
    }
    if($_FILES["file"]["size"] > 100000){
    echo "File is over the 100KB limit.<br>";
    }
    else{
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"]/1024) . "KB<br>";
    //QUERY insert into database
    }
    
}
else{
  	echo "";
  }
?>

 

EDITED BY thorpe: Added


tags to enable syntax highlighting.

Link to comment
https://forums.phpfreaks.com/topic/86860-solved-uploaded-image-still-says-invalid/
Share on other sites

Well according to your code, if file type does not equal all of the file types, it will display an error.  Try something like putting the allowed file types into an array (let's call it $allowed), and using

if(!in_array($_FILES['file']['type'], $allowed)){ 
    //error 
}

i changed some things around to what you both said... yet... what happens now is the invalid type does not show when a successful upload occurs (GOOD!) and each error message shows up respectively (GOOD!) except after a changed some things around to a better implementation (which shows below) what happens now is... when the correct image is supposed to be uploaded, no success message appears. i want to test each error message and if the image passes, move on to success code. if there is another way around it without putting in each check field, please let me know! if someone sees something totally wrong... PLEASE help!!

 

<?php

list($width, $height) = getimagesize($_FILES['file']['tmp_name']);

$allowed = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png");

 

if($_FILES["file"]["name"]!=""){

 

if (!in_array($_FILES["file"]["type"], $allowed)){

echo "Invalid file type.<br>";

}

    if ($_FILES["file"]["error"] > 0){

    echo "Error: " . $_FILES["file"]["error"] . "<br>";

    }

    if($width!=300 || $height!=200){

    echo "Bad dimensions.<br>";

    }

    if($_FILES["file"]["size"] > 100000){

    echo "File is over the 100KB limit.<br>";

    }

    if(in_array($_FILES["file"]["type"], $allowed) && $_FILES["file"]["error"] < 0 && $width=300 && $height=200 && $_FILES["file"]["size"] < 100000){

move_uploaded_file($_FILES["file"]["tmp_name"], "" . $_FILES["file"]["name"]);

        echo "Stored in: " . "" . $_FILES["file"]["name"];

    echo "<br>Upload: " . $_FILES["file"]["name"] . "<br>";

    echo "Type: " . $_FILES["file"]["type"] . "<br>";

    echo "Size: " . ($_FILES["file"]["size"]/1024) . "KB<br>";

    //QUERY insert into database

    }

   

}

else{

  echo "";

  }

?>

 

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.