[SOLVED] Submitting a file to another page via a form...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

[SOLVED] Submitting a file to another page via a form...

Post by jonas »

Do you have to do anything special to submit a file to another page?

I have my form on one page, then it submits to another page which checks for errors and such and displays them. It also stores the file on the server if there are no errors.
Problem is, I submit it to the new page and it gives me an error.
My file uploading script was originally written as a self submitting form, but I didn't change anything except the fact it submits to a new page and not itself.

What should I do, here is my code:

Code: Select all

<?
include("dbconnection/connect_db.inc.php");
session_start();
if ($_SESSION['logged'] == 1){
$query = "SELECT * FROM members where username='".$_SESSION['user']."'";
$mysql_stuffg = mysql_query($query, $mysql_link);
while($roc = mysql_fetch_row($mysql_stuffg)){
	$memberID = $roc[0];
}
$query = "SELECT * FROM games where gameID='$gameID'";
$mysql_stuffg = mysql_query($query, $mysql_link);
while($roc = mysql_fetch_row($mysql_stuffg)){
	$gameID = $roc[0];
	$game_title = $roc[1];
}
$query = "SELECT * FROM misc";
$mysql_stuff = mysql_query($query, $mysql_link);
while($roc = mysql_fetch_row($mysql_stuff)){
	$faqsize = $roc[3];
}
print("<html>
<head>
<title>Bolt 3 - FAQs</title>
</head>
<link href="css.php" rel="stylesheet" type="text/css">
<body bgcolor="#DAD6B0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td valign="top">"); include('header.php'); print("</td>
	</tr>
	<tr>
	<td>
	<div align="center">
	<table width="778" class="contenttable" cellpadding="0" cellspacing="0">
	<tr>
	<td>
	<div align="center">
	<table width="100%" height="100%" class="contentinside">
	<tr>
	<td>
	<div align="center">
	<form action="submit-faqsfinal.php" enctype="multipart/form-data" name="submitcode" method="GET">
	<table width="768" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td>
		<div align="center">
		<table width="598" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td border="0"><img src="siteimages/gamefaqstop.jpg"></td>
	</tr>
	<tr>
		<td>
		<div align="center">
        	<table class="regpart1" cellpadding="0" cellspacing="0">
        	<tr>
        	<td>
		<div align="center">
        	<table width="596" border="1" cellpadding="0" cellspacing="0" bordercolor="#86846B" bgcolor="#9F9C7E">
		<tr> 
        		<td colspan="2"><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>Submit a FAQ</b></font></div></td>
		</tr>
		<tr> 
        		<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Game Name:</font></div></td>
			<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">$game_title<input type="hidden" name="gameID" value="$gameID"></font></div></td>
		</tr>
		<tr> 
        		<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">FAQ Title:</font></div></td>
			<td><div align="center"><input type="text" name="faq_title" class="form" size="40"></div></td>
		</tr>
		<tr>
			<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">What type of FAQ are you submitting:&nbsp;</font></div></td>
			<td><div align="center"><select name="faq_type" class="form">
			<option value="" selected>Choose a Type</option>");
			$query = "SELECT * FROM game_faq_types";
			$mysql_stuff = mysql_query($query, $mysql_link);
			while($roc = mysql_fetch_row($mysql_stuff)){
				$faq_type = $roc[1];
				print("<option value="$faq_type" class="dropdownc">$faq_type</option>");
			}
			print("</select></div></td>
		</tr>
		<tr> 
        		<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">FAQ Version:</font></div></td>
			<td><div align="center"><input type="text" name="faq_ver" class="form" size="5"></div></td>
		</tr>
		<tr> 
        		<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Select your FAQ:</font></div></td>
			<td><div align="center"><input type="file" name="file" class="form"></div></td>
		</tr>
		<tr> 
        		<td colspan="2"><div align="center"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">Only .TXT files are allowed for FAQs.<br>
			The maximum size your FAQ can be is <b>$faqsize</b> bytes.</font></div></td>
		</tr>
		<tr> 
        		<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Submitted By:</font></div></td>
			<td><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">$_SESSION[user]<input type="hidden" name="memberID" value="$memberID"></font></div></td>
		</tr>
		<tr> 
        		<td colspan="2"><div align="center"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">All faqs submitted must be approved before they will appear on the site.<br>
			Only submit your faq once.</font></div></td>
		</tr>
		<tr>
			<td colspan="2"><div align="center"><input type="submit" name="Submit" value="Submit FAQ" class="submitbutton"></div></td>
		</tr>
		</table>
		</div>
		</td>
		</tr>
		</table>
		</div>
		</td>
	</tr>
	<tr>
		<td>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td bgcolor="#6D6B57"><div align="left"><img src="siteimages/tf-left.gif"></div></td>
			<td bgcolor="#6D6B57"><div align="right"><img src="siteimages/tf-right.gif"></div></td>
		</tr>
		</table>
		</td>
	</tr>
	</table>
	</form>
	</div>
	</td>
	</tr>
	</table>
	</div>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	</table>
	</div>
	</td>
	</tr>
	<tr>
		<td>"); include('footer.php'); print("</td>
	</tr>
</table>
</body>
</html>");
} elseif (empty($gameID)){
print("<html>
<head>
<title>Bolt 3 - Error</title>
</head>
<body bgcolor="#DAD6B0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<link href="css.php" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">"); include('header.php'); print("</td>
</tr>
<tr>
<td valign="top">
<div align="center">
<table width="778" class="contenttable" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" height="100%" class="contentinside">
<tr>
<td valign="top">
<div align="center">
<br>
<table class="regpart1" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center">
<br>
You did not specify a game.<br>
<br>
</div>
</table>
</div>
<br>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>"); include('footer.php'); print("</td>
</tr>
</table>
</body>
</html>");
} else {
print("<html>
<head>
<title>Bolt 3 - Error</title>
</head>
<body bgcolor="#DAD6B0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<link href="css.php" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">"); include('header.php'); print("</td>
</tr>
<tr>
<td valign="top">
<div align="center">
<table width="778" class="contenttable" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" height="100%" class="contentinside">
<tr>
<td valign="top">
<div align="center">
<br>
<table class="regpart1" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center">
<br>
Sorry, only registered users of Bolt 3 can contribute.<br>
If you are registered, please login first.<br>
<br>
</div>
</table>
<br>
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>"); include('footer.php'); print("</td>
</tr>
</table>
</body>
</html>");
}
?>
This is for the form first page. I will post the second pages code in another post in a second.
Last edited by jonas on Sun Jul 11, 2004 9:07 pm, edited 2 times in total.
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Second pages code:

Code: Select all

<?
include("dbconnection/connect_db.inc.php");
session_start();
if ($Submit == "Submit FAQ"){
$query = "SELECT * FROM misc";
$mysql_stuff = mysql_query($query, $mysql_link);
while($roc = mysql_fetch_row($mysql_stuff)){
	$faqsize = $roc[3];
}
$last_updated = date('F d, Y');
$faq_url = "http://www.bolt3.com/faqs/$_FILES[file][tmp_name]";
$sql_username_result = mysql_query("SELECT * from game_faqs WHERE memberID='$memberID' AND faq_title='$faq_title' AND faq_url='$faq_url'");
if (empty($faq_title)){
	$error = 1;
	$nofaqtitle = "You did not fill out a FAQ title.";
}
if (empty($faq_type)){
	$error = 1;
	$nofaqtype = "You did not select out a FAQ type.";
}
if (empty($faq_ver)){
	$error = 1;
	$nofaqver = "You did not fill out a FAQ version.";
}
if ((mysql_num_rows($sql_username_result) == 0) && ($error != 1)) {
$fileext = end(explode('.', $_FILES['file']['name'])); 
if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
    $error = 1;	
    $nofaq = "You did not select your FAQ!"; 
    unlink($_FILES['file']['tmp_name']);  
  } else { 
    //a file was uploaded 
    $maxfilesize=$faqsize; 
    if ($fileext != 'txt') 
    { 
      $error = 1;
      $faqnotallowed = "This file type is not allowed."; 
      unlink($_FILES['file']['tmp_name']);  
    } elseif ($_FILES['file']['size'] > $maxfilesize) {
      $error = 1;
      $faqnotallowed = "This file is too large"; 
      unlink($_FILES['file']['tmp_name']); 
    } else { 
       //File has passed all validation, copy it to the final destination and remove the temporary file: 
       copy($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']); 
       unlink($_FILES['file']['tmp_name']); 
       $faquploaded = "File has been successfully uploaded!"; 
     } 
}
}
print("<html>
<head>
<title>Bolt 3 - FAQs</title>
</head>
<link href="css.php" rel="stylesheet" type="text/css">
<body bgcolor="#DAD6B0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td valign="top">"); include('header.php'); print("</td>
	</tr>
	<tr>
	<td>
	<div align="center">
	<table width="778" class="contenttable" cellpadding="0" cellspacing="0">
	<tr>
	<td>
	<div align="center">
	<table width="100%" height="100%" class="contentinside">
	<tr>
	<td>
	<div align="center">
	<table width="768" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td>
		<div align="center">
		<table width="598" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td border="0"><img src="siteimages/gamefaqstop.jpg"></td>
	</tr>
	<tr>
		<td>
		<div align="center">
        	<table class="regpart1" cellpadding="0" cellspacing="0">
        	<tr>
        	<td>
		<div align="center">
        	<table width="596" border="1" cellpadding="0" cellspacing="0" bordercolor="#86846B" bgcolor="#9F9C7E">");
		if (mysql_num_rows($sql_username_result) > 0){
			print("<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">You have already submitted this faq!</font></div></td>
		</tr>");	
		} elseif ((!empty($nofaqtitle)) || (!empty($nofaqtype)) || (!empty($nofaqver)) || (!empty($nofaq)) || (!empty($faqnotallowed))){
		print("<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">$nofaqtitle</font></div></td>
		</tr>
		<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">$nofaqtype</font></div></td>
		</tr>
		<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">$nofaqver</font></div></td>
		</tr>
		<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">$nofaq</font></div></td>
		</tr>
		<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">$faqnotallowed</font></div></td>
		</tr>");
		} else {
		$query = "INSERT INTO game_faqs SET gameID='$gameID', memberID='$memberID', faq_url='$faq_url', faq_type='$faq_type', last_updated='$last_updated', faq_ver='$faq_ver', faq_title='$faq_title', approved='0'";
		$mysql_stuff = mysql_query($query, $mysql_link);
		print("<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Your FAQ has been submitted. If approved, you will receive your rubix and the FAQ will be featured on Bolt 3.$faq_url</font></div></td>
		</tr>");
		}
		if ($error == 1){
			print("<tr>
		<td><div align="center"><font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">You must return to the FAQ contribution form and fill it out properly to submit a FAQ. <a href="submit-faqs.php?gameID=$gameID">Click here to return</a></font></div></td>
		</tr>");
		}
		print("</table>
		</div>
		</td>
		</tr>
		</table>
		</div>
		</td>
	</tr>
	<tr>
		<td>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td bgcolor="#6D6B57"><div align="left"><img src="siteimages/tf-left.gif"></div></td>
			<td bgcolor="#6D6B57"><div align="right"><img src="siteimages/tf-right.gif"></div></td>
		</tr>
		</table>
		</td>
	</tr>
	</table>
	</div>
	</td>
	</tr>
	</table>
	</div>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	</table>
	</div>
	</td>
	</tr>
	<tr>
		<td>"); include('footer.php'); print("</td>
	</tr>
</table>
</body>
</html>");
} else {
print("<html>
<head>
<title>Bolt 3 - Error</title>
</head>
<body bgcolor="#DAD6B0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<link href="css.php" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">"); include('header.php'); print("</td>
</tr>
<tr>
<td valign="top">
<div align="center">
<table width="778" class="contenttable" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" height="100%" class="contentinside">
<tr>
<td valign="top">
<div align="center">
<br>
<table class="regpart1" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center">
<br>
Your code was not entered because you did not fill out the code contribution form.<br>
<br>
</div>
</table>
<br>
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>"); include('footer.php'); print("</td>
</tr>
</table>
</body>
</html>");
}
?>
So I submit a file and I get this error:
Warning: unlink(): No such file or directory in /usr/home/virtual/bolt3.com/webroot/htdocs/submit-faqsfinal.php on line 30

I chmod'd my faqs folder to 777 also... :(

What should I do
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

PS: This script works fine as a self submitting form.
When I got it, it was in this form:

Code: Select all

<?php 
error_reporting(E_ALL); 
include("dbconnection/connect_db.inc.php");
$query = "SELECT * FROM misc";
$mysql_stuff = mysql_query($query, $mysql_link);
while($roc = mysql_fetch_row($mysql_stuff)){
	$faqsize = $roc[3];
} 
if (!empty($_POST['submit'])) { 
  $fileext = end(explode('.', $_FILES['file']['name'])); 
  print_r($HTTP_POST_FILES); 
  print_r($fileext); 
  if (!is_uploaded_file($_FILES['file']['tmp_name'])) { 
    $error = "You did not upload a file!"; 
    unlink($_FILES['file']['tmp_name']); 
    // assign error message, remove uploaded file, redisplay form. 
  } else { 
    //a file was uploaded 
    $maxfilesize=$faqsize; 

    if ($fileext != 'txt') 
    { 
      $error = "This file type is not allowed."; 
      unlink($HTTP_POST_FILES['file']['tmp_name']); 
      // assign error message, remove uploaded file, redisplay form. 
    } elseif ($_FILES['file']['size'] > $maxfilesize) { 
      $error = "This file is too large"; 
      unlink($_FILES['file']['tmp_name']); 
      // assign error message, remove uploaded file, redisplay form. 
    } else { 
       //File has passed all validation, copy it to the final destination and remove the temporary file: 
       copy($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']); 
       unlink($_FILES['file']['tmp_name']); 
       print "File has been successfully uploaded!"; 
       print $_FILES['file']['name']; 
       print '<br />Ext: '.$fileext; 
       exit; 
     } 
   } 
} 

?> 
<html> 
<head></head> 
<body> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data"> 
<?php if(!empty($error)) echo $error; ?> 
<br><br> 
Choose a file to upload:<br> 
<input type="file" name="file"><br> 
<input type="submit" name="submit" value="submit"> 
</form> 
</body> 
</html>
And it works like that, but not when I seperated it into 2 pages.
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

So basically, it works as a single page form submitting to itself but once I seperated it into 2 pages, it stopped working.

So does anyone know why, and what I should do to remedy this, thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tried switching out the copy..unlink bits for [php_man]move_uploaded_file[/php_man]?
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Maybe a method that doesn't require too much extra coding as I am a sloppy coder who doesn't know that much about PHP. ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that doesn't require much coding.. :P
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

I looked at the page, but how do I edited my file to use this new code?

I might make my uploader just one page since it worked that way... :S
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remove/comment out

Code: Select all

copy($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']); 
       unlink($_FILES['file']['tmp_name']); 

// replace with:

if(move_uploaded_file($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']) === false)
  die('error in moving file');
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Code: Select all

if ((mysql_num_rows($sql_username_result) == 0) && ($error != 1)) {
$fileext = end(explode('.', $_FILES['file']['name'])); 
if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
    $error = 1;	
    $nofaq = "You did not select your FAQ!"; 
    unlink($_FILES['file']['tmp_name']);  // [b]STOPS HERE ERROR HERE[/b]
  } else { 
    //a file was uploaded 
    $maxfilesize=$faqsize; 
    if ($fileext != 'txt') 
    { 
      $error = 1;
      $faqnotallowed = "This file type is not allowed."; 
      unlink($_FILES['file']['tmp_name']);  
    } elseif ($_FILES['file']['size'] > $maxfilesize) {
      $error = 1;
      $faqnotallowed = "This file is too large"; 
      unlink($_FILES['file']['tmp_name']); 
    } else { 
       //File has passed all validation, copy it to the final destination and remove the temporary file: 
       // copy($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']); 
       // unlink($_FILES['file']['tmp_name']);
	if(move_uploaded_file($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']) === false) 
  	die('error in moving file'); 

       $faquploaded = "File has been successfully uploaded!"; 
     } 
}
}
I did your suggestion but it isn't even getting there. It gets to line 6 and it says it cannot unlink the file (it says there is no file). But on my uploader I am selecting a txt file and submitting the form.

For some reason it's acting as if I have not submitted a form.
Dunno what to do.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hmm.. try changing the method to POST in the first page..
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Hey man, it works now! But the URL thing is messed up.
Since I don't store the FAQ in the database (that would be dumb) I put a copy of the URL where the FAQ is lcoated but it outputted this:
http://www.bolt3.com/faqs/Array[tmp_name]

What shoudl I do
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

$faq_url = "http://www.bolt3.com/faqs/{$_FILES['file']['tmp_name']}"; 

?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You'de probably want:

Code: Select all

$faq_url = "http://www.bolt3.com/faqs/{$_FILES['file']['name']}";
..as you've moved the file from it's tmp_name to it's name :o
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

One last question. For my backend I would like to have a way to delete the file affiliated with this submission without using an FTP. Is there a proper way to do this?

As I have it now, everything must be approved before it can be put on the site but if it isn't good enough or obscene then the admin can delete that submission from the DB (you know this already I think). I want to make it so if they delete the submission it also deletes the file that is on the server.


I'm assuming it's similar to unlink or that move_upload_file.
Post Reply