Jump to content

[SOLVED] File upload script glitch


dnoland

Recommended Posts

I have tried everything and still can not explain this... I just hope one of you can...

So i have someForm.html like this...

<form action = "basic.php" method = "post" enctype="multipart/form-data">
     File: <input type = "file" name = "file" id = "file" /><br />
     <input type="submit" name = "submit" id = "submit" value = "Submit" /><br />
</form>

And I have basic.php like this

<html>
  <body>
    <?php
       	function validateFile($upload)
	{
	else
		{
		if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000)
			{
			echo "Upload: " . $upload["name"] . "<br />";
			echo "Type: " . $upload["type"]	. "<br />";
			echo "Size: " . $upload["size"]	. "<br />";
			echo "Stored in: " . $upload["tmp_name"] . "<br />";
			return true;
			}
		else
			{
			echo "Invalid file<br />";
			return false;
			}
		}
	}
function saveFile($upload)
	{
	if (validateFile($uplaod))
		{
		// Do some stuff... that never seems to actually happen
		}
	else
		{
		echo "Can't save invalid file <br />";
		return false;
		}
	}
	validateFile($_FILES["file"]);
	saveFile($_FILES["file"]);
?>
</body>
</html>

And this is what I get back when I upload some gif (civilDisobidence.gif in this case)

<html>
  <body>
    Upload: civilDisobidience.gif<br />
    Type: image/gif<br />
    Size: 13139<br />
    Stored in: /tmp/phpIZBJ3R<br />
    Invalid file<br />
    Can't save invalid file <br />
  </body>
</html>

 

 

So what this comes down to is that the function validateFile is returning false in spite of the fact that it obviously is executing a part of the script that obviously ends in return true.  Further... it is running both the 'then' and 'else' parts of nested if else statement in the validateFile function.  I have never had any error like that before.

 

Any thoughts??

Thanks for any help on this one...

 

 

Link to comment
Share on other sites

if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000)

 

Well, I (me) woudl do the following just for debugging's sake:

 

if ($upload["size"] < 20000000) {
  if ($upload["type"] == "image/gif") {
    //call the function that does stuff if its valid
  } else if ($upload["type"] == "image/jpeg") {
    //call the function that does stuff if its valid
  } else if ($upload["type"] == "image/pjpeg") {
    //call the function that does stuff if its valid
  } else {
    return(false);
  }
}


Link to comment
Share on other sites

Well d22552000, I can do that... but I don't really understand why... the file output already told me that it is a gif and all of that...

Upload: civilDisobidience.gif<br />
    Type: image/gif<br />
    Size: 13139<br />
    Stored in: /tmp/phpIZBJ3R<br />
    Invalid file<br />
    Can't save invalid file <br />

So I can't really see how their could be an error in any of those conditions.  Besides, the whole condition

(($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000

should return true or false.  The fact that the server kicks back both the section about Type, Size, ect... and the words "invalid file" seem to indicate that it ran BOTH the 'then' and 'else' part of the code.  As far as I know, this should not happen in ANY event.

 

Thanks... I will try what you suggested and get back to you...

 

As for teng84... my mistake, I copied that section wrong... the script on the server says the following...

 

<html>
  <body>
    <?php
       	function validateFile($upload)
	{
	if ($upload["error"] > 0)
		{
		echo "Error: " . $upload["error"] . "<br />";
		return false;
		}
	else
		{
		if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000)
			{

			echo "Upload: " . $upload["name"] . "<br />";
			echo "Type: " . $upload["type"]	. "<br />";
			echo "Size: " . $upload["size"]	. "<br />";
			echo "Stored in: " . $upload["tmp_name"] . "<br />";
			return true;
			}
		else
			{
			echo "Invalid file<br />";
			return false;
			}
		}
	}
function saveFile($upload)
	{
	if (validateFile($uplaod))
		{
		if(file_exists("upload_" . $upload["name"]))
			{
			echo $upload["name"] . " already exists.<br />";
			return false;
			}
		else
			{
			move_uploaded_file($upload["tmp_name"], "upload_" . $upload["name"]);
			echo "Store sucess <br />";
			return true;
			}
		}
	else
		{
		echo "Can't save invalid file <br />";
		return false;
		}
	}
		validateFile($_FILES["file"]);
		saveFile($_FILES["file"]);
?>
</body>
</html>

 

 

Link to comment
Share on other sites

Ok...

print_r($_FILES);

Gives the following...

Array ( [file] => Array ( [name] => civilDisobidience.gif [type] => image/gif [tmp_name] => /tmp/phpAjHCsS [error] => 0 [size] => 13139 ) ) Upload: civilDisobidience.gif

Obviously the temp location is randomized... but the rest of that seems to be in order to me...

Link to comment
Share on other sites

this is thesame

       	function validateFile($upload){
	if ($upload["error"] > 0)
	{
		echo "Error: " . $upload["error"] . "<br />";
		$return= false;
	}
	elseif(file_exists("upload_" . $upload["name"])){
		echo $upload["name"] . " already exists.<br />";
		$return =false;
	}
	else
		{
		if ((($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000)
			{

			echo "Upload: " . $upload["name"] . "<br />";
			echo "Type: " . $upload["type"]	. "<br />";
			echo "Size: " . $upload["size"]	. "<br />";
			echo "Stored in: " . $upload["tmp_name"] . "<br />";
			move_uploaded_file($upload["tmp_name"], "upload_" . $upload["name"]);
			echo "Store sucess <br />";

			$return= true;
			}
		else
			{
			echo "Invalid file<br />";
			$return =false;
			}
		}
		return $return;
	}

sigle but the same with your two function

Link to comment
Share on other sites

try to

if (validateFile($uplaod)=== true)

 

that might be just the same but try it and i guess your function can only be one

Well... I did the === true change.  No effect... but at least now we are positive that the validateFile function is returning false... so the bug must live in that function...

 

As for that rewrite... if just gives me "invalid file" when I run that.  That means that the condition

(($upload["type"] == "image/gif") || ($upload["type"] == "image/jpeg") || ($upload["type"] == "image/pjpeg")) && $upload["size"] < 20000000

is false... no idea what that is about...

 

Now... here is something interesting (were back to the 2 function version here...)

if i

echo validateFile($_FILES["file"]) . "<br />";

it prints the normal output of validateFile as well as a 1.  I thought 1 was true??? validateFile must be returning false, otherwise I would not be getting "Can't save invalid file" from the saveFile function???

 

 

 

 

Link to comment
Share on other sites

Sorry... my bad... all of this trouble over some stupid typo...

 

function saveFile($upload)

{

if (validateFile($uplaod)===true) //Note to self... learn to spell upload... damn. 

 

I guess that is the trouble with loosely typed languages... or being unable to type... one of the two.

 

in your saveFile upload function:

if(file_exists("upload_" . $upload["name"]))

should be

if(!file_exists("upload_" . $upload["name"]))

 

In your current code, you are telling it if the file exists, upload it, or else return "Can't save invalid file"

I am going to have to disagree with you on that one...

sudo code for that save function

if (file already exists)

    { say so }

else

    { upload it }

 

All in all I have to say thanks to all of you... I am really impressed with this whole site, and very glad I joined this group.  Hopefully I will be able to give back (after I learn some more php).

 

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.