Jump to content

File upload problems


tie372

Recommended Posts

Can someone help me upload multiple files?

I have tried using the array method (<input name=upload[] ...>), and I have tried giving the upload forms different names, but the second file always errors.

The script is so users can upload games that they made. The two files uploaded are the screenshot and the game itself (.exe or .swf)...

 

$name=$_POST[title];
$summary=$_POST[summary];
$genre=$_POST[genre];

$screenname=$_FILES['uploadfile']['name'][0];
$screensize=$_FILES['uploadfile']['size'][0];
$screentype=$_FILES['uploadfile']['type'][0];

$gamename=$_FILES['uploadfile']['name'][1];
$gamesize=$_FILES['uploadfile']['size'][1];
$gametype=$_FILES['uploadfile']['type'][1];

print_r($_FILES);
//screenshot

if(empty($screenname)){echo "no file was uploaded for the screenshot.  hit back and try again"; exit();}
if($screensize>3000000){echo "your file is too big.  hit back and try again"; exit();}

if($screentype == "image/gif" || $screentype =="image/jpg" || $screentype =="image/png" || $screentype =="image/jpeg" || $screentype =="image/bmp"){

$location="screens/" . $id;
$tmpname=$_FILES['uploadfile']['tmp_name'][0];
move_uploaded_file($tmpname, $location);

}

//screenshot
//game



if(empty($gamename)){echo "no file name was indicated.  hit back and try again"; exit();}
if($gamesize>26214400){echo "your file is too big.  hit back and try again"; exit();}

if($gametype== "application/octet-stream" || $gametype== "application/x-shockwave-flash")
{

$gamelocation="games/" . $id;
$tmpname=$_FILES['uploadfile']['tmp_name'][1];
move_uploaded_file($tmpname, $gamelocation);
}

//game

 

And here's the form part as well, just in case:

<form action=\"newgame.php\" enctype=\"multipart/form-data\" method=POST>
...
<tr><td>screenshot</td><td>
<input type=file name=\"uploadfile[]\"></input>
</td></tr>
<tr><td>file</td><td>
<input type=file name=\"uploadfile[]\"></input>
</td></tr>
...
</form>

 

The name, size, and type of the screenshot are recorded, but only the name of the gam file is recorded. Thank you.

Link to comment
https://forums.phpfreaks.com/topic/97838-file-upload-problems/
Share on other sites

I have posted this before its a multi image uploaded you can mod to  new doc types

<?php
#open sql connection if needed
ConnectSQL();
if(!empty($_POST)){
#prevent refresh reuploading
if($_POST['no_refresh'] != $_SESSION['no_refresh']){
$i = 1;
#loop through all files in array
while($i <6){
	#if the FILES array  has an error lets no ignore it
	if($_FILES['image_'.$i]['error'] == "0"){
		#keep file size proper
		if($_FILES['image_'.$i]['size'] < $_POST['MAX_FILE_SIZE']){
			#proper document type
			if(in_array($_FILES['image_'.$i]['type'],$image_types)){
				#upload location
				$rand_file= ROOT."uploads/";
				#tmp file name
				$file_loc = date("U")."_".rand(0,9999);
				#adding of extension
				switch($_FILES['image_'.$i]['type']){
					case "image/jpeg":
					case "image/pjpeg":
						$ext = ".jpg";
					break;
					break;
					case "image/gif":
					case "image/pgif":
						$ext = ".gif";
					break;
					break;
					case "image/png":
					case "image/ppng":
						$ext = ".png";
					break;
					break;
				}
				#move
				move_uploaded_file($_FILES['image_'.$i]['tmp_name'],$rand_file.$file_loc.$ext);
				#insert into myslql of relative data comment out if unnneede
				$q = "Insert into `".PICTURES_TABLE."`
					(Gallery_ID, Upload_IP, Upload_Name, Upload_Email, Upload_Date, Tmp_Name, O_Name)
					VALUES('".input_clean($_POST['pic_type'])."', '".input_clean($_POST['ip'])."', '".$_POST['name']."', '".$_POST['email']."', NOW(), '".$file_loc.$ext."', '".$_FILES['image_'.$i]['name']."')";
				$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
				$no_errors[] = "The image was uploaded!";
			}
			else{
				$errors[] = "The file type is invalid for the file uploaded.";
			}
		}
		else{
			$errors[] = "The file uploaded was too large.";
		}
	}
	else{
		if(!empty($_FILE['image_'.$i])){
			$errors[] = "There was an error uploaded the file.";
		}
	}
	$i++;
}
}
else{
	$errors[] = "Please do not refresh this page.";
}
}
echo "<h1>Photo Upload</h1>";
#Error reporting
if(!empty($errors)){
echo "<br /<br />";
echo "<h3>Please fix the following errors: </h3>";
echo "<ul class=\"error_ul\">";
	foreach($errors as $value){
		echo "<li>".$value."</li>";
	}
echo "</ul>";
echo "<br /><hr /><br />";
}
#no error reporting
if(!empty($no_errors)){
echo "<h3>Successs!</h3>";
echo "<ul class=\"noerror_ul\">";
	foreach($no_errors as $value){
		echo "<li>".$value."</li>";
	}
echo "</ul>";
echo "<br /><br />";
}
#the form action is this page
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" enctype=\"multipart/form-data\">\n";
echo "<input type=\"hidden\" value=\"".$_SERVER['REMOTE_ADDR']."\" />\n";
echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"5000000\">";
echo "<input type=\"hidden\" name=\"no_refresh\" value=\"".rand(0,9999)."\" />";
echo "Your Name (optional): <input type=\"text\" name=\"name\" value=\"\" /><br />\n";
echo "Your Email (optional): <input type=\"text\" name=\"email\" value=\"\" /><br />\n";
echo "Category/Event: \n";
echo "<select name=\"pic_type\">\n";
echo "<option value=\"\">None</option>\n";
foreach($gals as $key => $value){
echo "<option value=\"G_".$key."\">".$value['Name']."</option>\n";
}
foreach($events as $key =>$value){
echo "<option value=\"E_".$key."\">".$value['Name']."</option>\n";
}
#multi items note psedo array strucutre
echo "</select><br />\n";	
echo "Image 1: <input type=\"file\" name=\"image_1\" /><br />\n";
echo "Image 2: <input type=\"file\" name=\"image_2\" /><br />\n";
echo "Image 3: <input type=\"file\" name=\"image_3\" /><br />\n";
echo "Image 4: <input type=\"file\" name=\"image_4\" /><br />\n";
echo "Image 5: <input type=\"file\" name=\"image_5\" /><br />\n";
echo "<input type=\"submit\" value=\"Upload\" />";	
echo "</form>";
?>

Link to comment
https://forums.phpfreaks.com/topic/97838-file-upload-problems/#findComment-500574
Share on other sites

Thanks for the code, but I can't exactly modify it easily to mine.  Mine needs to be able to have different file extensions, so a while loop just complicates things...(I think).

 

EDIT: I also have different file size limits and file locations.

 

Here's the file array:

Array ( 
[pictureupload] => Array ( [name] => screenshotvirus.png [type] => image/png [tmp_name] => /tmp/blah [error] => 0 [size] => 53963 )
[gameupload] => Array ( [name] => sf.exe [type] => [tmp_name] => [error] => 1 [size] => 0 ) )

Link to comment
https://forums.phpfreaks.com/topic/97838-file-upload-problems/#findComment-500593
Share on other sites

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.