Jump to content

File Upload not working.


TimUSA

Recommended Posts

Hello all, I have a file upload script that redisplays the image after submit. Every thing seems to work fine for me, but several users have complained that it is not uploading the omage and therefor it does not redisplay it either. I am a bit stumped here!

 

<?php
/*
*AYC SCORING AND RANKING PROGRAM
*CREATED BY: Tim Willford
*EMAIL: [email protected]
*VERSION: 2.0
*/


/////CONFIGURATION/////
global $scripturl;
$date = date("Y/m/d");
echo '
<style type="text/css">
#leftcontent
{
float: left; 
margin-left: 10;
padding-right: 5;
}
#rightcontent
{
float: right;
margin-left: 10;
padding-right: 5;
}
</style>';

//FORM 1: ENTER RACE INFORMATION

//submit screenshot
if(!isset($_POST['upload'] ))
{
echo'
<table class = "bordercolor" cellspacing="1" cellpadding="1" width="100%" border="0">
	<td class ="catbg">SUBMIT SCREENSHOT:</td>
</table>
<br>';
echo'
<form name="form1" method="post" action="'.$scripturl.'?page=181'.$get['page'].'" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="upload" value="Upload">
</form>';
}
else if(isset($_POST['upload'] ))
{
if ($_FILES['imagefile']['type'] == "image/png")
{
	copy ($_FILES['imagefile']['tmp_name'], "screenshots/".$_FILES['imagefile']['name'])
	or die ("Could not copy");
}
}

//enter race information
if((isset($_POST['upload'] )) && (!isset($_POST['form1'] )))
{
echo'
<table class = "bordercolor" cellspacing="1" cellpadding="1" width="100%" border="0">
	<tr class ="catbg">
		<td>ENTER RACE INFORMATION:</td>
	</tr>
</table>
<br>';
echo'
<form action="'.$scripturl.'?page=182'.$get['page'].'" method="post">
<input name="form1" type="hidden" value="TRUE" />
';

//enter race host:
echo'
<div id="leftcontent">
Host<br>
<select id="host" name="host" style="width: 204px" value ="';
echo '" />';
	$query = 	"SELECT `memberName` 
				FROM `smf_members` 
				WHERE `ID_GROUP` IN (1, 9, 10, 11, 12, 13) 
				ORDER BY `memberName`;";
	$result = mysql_query($query);
		if(mysql_num_rows($result)) 
		{
			while($row = mysql_fetch_row($result)) 
			{
				print("<option value=\"$row[0]\">$row[0]</option>");
			}
		}
	mysql_data_seek($result, 0);
echo'
</select>
<br>';

//enter race date
echo'
Date:<br>
<input type="date type" name="date" id="date" style="width: 200px" value="' . $date . '"/>
<br>';

//enter number of boats
echo'
Number of Boats:<br>
<input type="text" name="boats" style="width: 200px" maxlength="20" size="20" />
<br>';

//enter race type
echo'
Race Type:<br>
<select id="factor" name="factor" style="width: 204px" value ="';
echo '" />
	<option value="1" selected>Fleet Race</option>
	<option value="2">Match Race</option>
        <option value="6">Club Race</option>';
        if ($context['allow_admin'])
        {
        echo'
        <option value="8">Championship</option>';
        }
        echo'
</select>
<br>';

//enter series name
echo'
Series Name:<br>
<select id="series" name="series" style="WIDTH: 204px" value ="';
echo '" />';
	$seriesquery = 	"SELECT `seriesName` 
					FROM `series_table`
					ORDER BY `seriesName` DESC
					LIMIT 0,15 ;";
	$seriesresult = mysql_query($seriesquery);
	while($row = mysql_fetch_row($seriesresult)) 
	{
		print("<option value=\"$row[0]\">$row[0]</option>");
	}
echo'
</select>
<br>
<br>';

//submit & reset
echo'
<input type="submit" value="Submit Race Information"> <input type="reset" value="Reset">
</div>';

//display screenshot
echo'
<div id="rightcontent">
<img width="500" height="" src="http://vsk-ayc.totalh.com/screenshots/' . $_FILES['imagefile']['name'] . '" alt="" />
<br>
<input type="text" READONLY name="image" style="WIDTH: 500px" value="http://vsk-ayc.totalh.com/screenshots/' . $_FILES['imagefile']['name'] . '" />
</div>
</form>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/98160-file-upload-not-working/
Share on other sites

You need to add error checking (test for errors), error reporting (display or log a meaningful message about what happened), and error recovery logic (what does the code do when an error is detect) to your program to get your code to tell you why it is not working. We cannot really tell anything without specific symptoms and can only provide a lot of guesses as to which of the several possible problems it could be.

 

You have some error checking/reporting now, such as or die ("Could not copy"); when the file cannot be copied, but you need to check everything possible.

 

The upload could be failing. Checking the ['error'] element of $_FILES['imagefile']['error'] would tell you if the upload worked (there is a section in the php manual that explains the possible values.) A zero value means the upload worked and your code should only access any of the upload file information if there was no upload error. You should display a meaningful error message for all the other possible upload error values and then prevent the remainder of the code from accessing any of the upload file information.

 

When you check for the type being "image/png" and it does not match, you don't do anything to notify that a problem occured, such as display the actual type information that was received so you can see what it actually is (different browsers provide different type information for the same file) or prevent the remainder of the code from being execuited. Almost every if() test of an external value requires an else statement to do something when the test failed because that indicates an unexpected value was received.

 

Also, global $scripturl; has no meaning unless this code is inside of a function. Do a "view source" in your browser and see what you are getting in the html where $scripturl should be.

 

Ok, I have worked on this a bit and am hoping for some further suggestions for error checking.

if(!isset($_POST['Upload'] ))
{
echo'
<table class = "bordercolor" cellspacing="1" cellpadding="1" width="100%" border="0">
	<td class ="catbg">SUBMIT SCREENSHOT:</td>
</table>
<br>';
upload();
}
else
{
if($_FILES['pix']['tmp_name'] == "none")
{
	echo'<p>File did not upload. File size must be less than 500K</p>';
	upload();
}
if(!ereg("image", $_FILES['pix']['type']))
{
	echo'<p>File is not a picture. Please try another file.</p>';
	upload();
}
else
{
	copy ($_FILES['pix']['tmp_name'], "screenshots/".$_FILES['pix']['name']) or die(''$_FILES['pix']['error']'');
	echo'Your file has uploaded correctly:<br>';
	echo $_FILES['pix']['name'];
	echo'<br>';
	echo $_FILES['pix']['size'];
}
}

 

 

I am concerned that one particular part may not be correct:

Will this work?

 or die(''$_FILES['pix']['error']'')

 

EDIT: I forgot the function code which displays the form :)

function upload(){
echo'
<form name="form1" method="post" action="'.$scripturl.'?page=181'.$get['page'].'" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pix">
<br>
<input type="submit" name="Upload" value="Upload Picture">
</form>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.