Jump to content

[SOLVED] simple but odd problem w/ file uploading


evanct

Recommended Posts

So I have this form:

 

<form action='post.php' method='POST' enctype='multipart/form-data'>
//other input fields...//
<input type='file' name='imgupload' value='Browse' />
<input type='submit' name='newpost' value='Post' />

 

the file upload is optional. so i have this part at the top of the processing script post.php:

if (isset($_FILES['imgupload'])) {

	//some file processing//

}

 

regardless of whether i leave the upload field blank or not, that block of code runs. that's it. it's bracketed correctly, and I don't see anything in the rest of the code that would interfere - and it's not a complicated script. no relevant Notices, either. any idea what might cause this? if it matters here's the whole code:

 

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
require_once('connect.php');

if (isset($_POST['newpost'])) {
$img=NULL;
if (isset($_FILES['imgupload'])) {
	$pathinfo=pathinfo($_FILES['imgupload']['name']);
	$newname=rand().uniqid();
	$img=$newname.'.'.$pathinfo['extension'];
	move_uploaded_file($_FILES['imgupload']['tmp_name'],'img/'.$img);
}
$text=addslashes($_POST['posttext']);
$parent=$_POST['parent'];
$ip=$_SERVER['REMOTE_ADDR'];
if ($_POST['author']=='trip') {
	$result=mysql_query("
						SELECT 
							name 
						FROM 
							ip 
						WHERE 
							ip_add='$ip'");
	if (mysql_num_rows($result)==0) {
		$author=mt_rand();
		mysql_query("INSERT INTO 
						ip(ip_add,name) 
					VALUES
						('$ip','$author')");
	} else {
		$row=mysql_fetch_array($result);
		$author=$row[0];
	}
} else {
	$author='Anonymous';
}
$sql="INSERT INTO posts(post_text,file,votes,parent,auth_ip,auth_name) VALUES('$text','$img',1,'$parent','$ip','$author')";
mysql_query($sql) or die('Could not add post: '.mysql_error());
if ($parent != 0) {
	$sql="UPDATE 
		posts 
	SET 
		children=children+1 
	WHERE 
		post_id='$parent'";
	mysql_query($sql);
} 
if (mysql_error()) {
	echo 'error: '.mysql_error();
} else {
	echo 'successfully posted.';
}
header('refresh:2;url=index.php');

}

?>

 

Link to comment
Share on other sites

It would set that field as NULL because regardless if something is 'technically' in there it will treat it as if it has been submitted with something in it.

 

Try to check to see if it is empty rather than the variable being set.

 

if (!empty($_FILES['imgupload'])) {

	//some file processing//

}

Link to comment
Share on other sites

$_FILES['imgupload'] will be set (if uploads are enabled, your form has a correct enctype, and there is a type="file" field), even if a file is not selected to be uploaded. You would need to test the ['error'] element for a value of 4 to determine if a file was not selected in the form - http://us3.php.net/manual/en/features.file-upload.errors.php

 

You can use the following code to see exactly what is in the $_FILES array for testing -

<?php
echo "<pre>";
echo "FILES:";
print_r($_FILES);
echo "</pre>";
?>

Link to comment
Share on other sites

$_FILES['imgupload'] will be set (if uploads are enabled, your form has a correct enctype, and there is a type="file" field), even if a file is not selected to be uploaded. You would need to test the ['error'] element for a value of 4 to determine if a file was not selected in the form - http://us3.php.net/manual/en/features.file-upload.errors.php

 

thanks, i figured it was something like that

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.