Page 1 of 1

Simple file upload problem

Posted: Sat Apr 03, 2010 3:28 pm
by rockinruler1
Hi. I am 15 years old and trying to make a php upload page for my brothers assigment but my code doesnt work. the _FILE[file] variable itself doesn't get set. Please tell me where the problem is

[text]<HTML>
<HEAD>
<script type="text">
</script></HEAD>
<BODY style="text-align:center">
<SCRIPT LANGUAGE="javascript">

function setOptions(chosen) {

var val = chosen.options[chosen.selectedIndex].value;

if (val== "Select1.1") {
document.FORM1.SELECt3.style.display='none';
document.FORM1.SELECt2.style.display='none';
document.FORM1.SELECt4.style.display='none';
}
if (val== "Syllabus") {
document.FORM1.SELECt3.style.display='none';
document.FORM1.SELECt4.style.display='inline';
}
if (val== "Computers"||val=="EXTC"||val=="Electronics" ||val=="IT" ||val=="Biomedical"||val=="Production" ||val=="Chemical") {
document.FORM1.SELECt3.style.display='none';
document.FORM1.SELECt2.style.display='inline';
}
if (val== "Fees") {
document.FORM1.SELECt2.style.display='none';
document.FORM1.SELECt3.style.display='inline';
}
if (val== "SEM1"||val== "SEM2"||val== "SEM3"||val== "SEM4") {
document.FORM1.sub.style.display='inline';
document.FORM1.Ufile.style.display='inline';
}
if (val== "Select2.1"||val=="Select1.1"||val=="Select3.1") {
document.FORM1.sub.style.display='none';
document.FORM1.Ufile.style.display='none';
}
if (val== "Select4.1"){
document.FORM1.SELECt2.style.display='none';
}
}
</SCRIPT>
<form method="post" action="upload.php" enctype="multipart/form-data">

<br><br><select NAME="SELECT1" onchange="setOptions(this);" >
&nbsp<Option value="Select1.1" selected="selected">Select any one option
&nbsp<Option value="Syllabus">Syllabus
&nbsp<Option value="Fees">Fees
&nbsp<Option value="UP">University Papers
&nbsp<Option value="SGM">Sports Game Shedules
&nbsp<Option value="ETT">Exam Time Tables
&nbsp<Option value="Disclosures">Disclosures
</SELECT>

<br><br><select NAME="SELECT4" id="SELECt4" style="display:none" onchange="setOptions(this);">
&nbsp<Option value="Select4.1">Select any one option
&nbsp<Option value="Computers">Computers
&nbsp<Option value="EXTC">EXTC
&nbsp<Option value="Electronics">Electronics
&nbsp<Option value="IT">IT
&nbsp<Option value="Biomedical">Biomedical
&nbsp<Option value="Production">Production
&nbsp<Option value="Chemical">Chemical
</SELECT>



<br><br><select NAME="SELECT2" id="SELECt2" style="display:none" onchange="setOptions(this);">
&nbsp<Option value="Select2.1">Select any one option
&nbsp<Option value="SEM1">SEM1
&nbsp<Option value="SEM2">SEM2
&nbsp<Option value="SEM3">SEM3
&nbsp<Option value="SEM4">SEM4
</SELECT>


<br><br><select NAME="SELECT3" id="SELECt3" style="display:none" onchange="setOptions(this);">
&nbsp<Option value="Select3.1">Select any one option
&nbsp<Option value="SEM1">SEM1
&nbsp<Option value="SEM2">SEM2
&nbsp<Option value="SEM3">SEM3
&nbsp<Option value="SEM4">SEM4
</SELECT>


<br><br> <Input type="file" name ="file" value="Browse" id="Ufile" style="display:none">



<br><br><Input type="submit" name="submit" value="Submit" id="sub" style="display:none">

</FORM>
</BODY>
</HTML>[/text]

Code: Select all

<html>
<head>
</head>
<body>
	<?php

			
			    if ($_FILES['file']['name'] != '')
			    {
 			       echo "A file was uploaded.";
 				   } else {
 			       echo "No file was uploaded.";
				    }
			


			$extension = pathinfo($_FILES['file']['name']);
			$exten = $extension['extension'];


			if($exten=="pdf")
			{
				if(is_uploaded_file($_FILES['file']['tmp_name']))
				{
					move_uploaded_file($_FILES['file']['tmp_name'],"pqr/".$_FLIES['file']['tmp_name'].".".$exten);
					echo "Upload Sucessful<br />";
			    }
			    echo 'Failed';
			}
			else
			{
				echo '<li>File format not supported.\n'.$exten.'</li>';
			}

	?>
</body>
</html>

Re: Simple file upload problem

Posted: Sat Apr 03, 2010 5:35 pm
by josh
if there is an error you need to check the 'error' index of the $_FILE array

Re: Simple file upload problem

Posted: Sat Apr 03, 2010 9:02 pm
by Obadiah
if this page is a php page...
rockinruler1 wrote:Hi. I am 15 years old and trying to make a php upload page for my brothers assigment but my code doesnt work. the _FILE[file] variable itself doesn't get set. Please tell me where the problem is

Code: Select all

<html>
<head>
</head>
<body>
	<?php

			
			    if ($_FILES['file']['name'] != '')
			    {
 			       echo "A file was uploaded.";
 				   } else {
 			       echo "No file was uploaded.";
				    }
			


			$extension = pathinfo($_FILES['file']['name']);
			$exten = $extension['extension'];


			if($exten=="pdf")
			{
				if(is_uploaded_file($_FILES['file']['tmp_name']))
				{
					move_uploaded_file($_FILES['file']['tmp_name'],"pqr/".$_FLIES['file']['tmp_name'].".".$exten);
					echo "Upload Sucessful<br />";
			    }
			    echo 'Failed';
			}
			else
			{
				echo '<li>File format not supported.\n'.$exten.'</li>';
			}

	?>
</body>
</html>
try this instead

Code: Select all

<?php //<?php<--always a good thing to remember to keep this as the first line of code
			    if ($_FILES['file']['name'] != '')
			    {
 			       echo "A file was uploaded.";
 				   } else {
 			       echo "No file was uploaded.";
				    }
			


			$extension = pathinfo($_FILES['file']['name']);
			$exten = $extension['extension'];


			if($exten=="pdf")
			{
				if(is_uploaded_file($_FILES['file']['tmp_name']))
				{
					move_uploaded_file($_FILES['file']['tmp_name'],"pqr/".$_FLIES['file']['tmp_name'].".".$exten);
					echo "Upload Sucessful<br />";
			    }
			    echo 'Failed';
			}
			else
			{
				echo '<li>File format not supported.\n'.$exten.'</li>';
			}

	$display_block .="  //or echo"";
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>";


Re: Simple file upload problem

Posted: Sun Apr 04, 2010 8:27 pm
by lunarnet76
hi,

you should start by using the official PHP manual : http://www.php.net/manual/en/features.f ... method.php,
but your code as 2 main problems :
The first one is that you should use

Code: Select all

if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '')
because the variable $_FILES is populated only when something has been uploaded, so the first time the form is showed the $_FILES array is empty

Second the problem is the same, most of your code is executed all the time instead of only when the file has been uploaded!!!
You should show all PHP errors (with error_reporting(E_ALL) for example)! and do a

Code: Select all

echo '<pre>';print_r($_FILES);echo '</pre>';
just after your <?php tag

Good luck anyway^^

Re: Simple file upload problem

Posted: Sun Apr 04, 2010 8:53 pm
by josh
lunarnet76 wrote: is executed all the time instead of only when the file has been uploaded!!!
Actually the problem is that the user did upload the file, but he did not check the error code. Most likely they did upload the file, but one of the 8 error conditions is occurring (file too large, etc)