Can anyone help with this $_POST problem???

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

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Can anyone help with this $_POST problem???

Post by mmc01ms »

I've created a weblog which i want to upload a picture to as well as title and story. The problem is that the picutre wont upload it keeps saying that 'Couldn't copy the file'. which means it's not finding the picture. Any help please? Code for html and php are below.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Newsboard Entry</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<table width="634" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td width="634" height="455" valign="top"><form action="writetonewsboard.php" method="post" enctype="multipart/form-data">
        <p> </p>
        <table width="512" border="0" cellspacing="2" cellpadding="2">
          <!--DWLayoutTable-->
          <tr> 
            <td height="42" align="right" valign="top">Entry Title</td>
            <td width="398" valign="top"><input type="text" name="title" id="title" size="45"></td>
          </tr>
          <tr> 
            <td height="218" align="right" valign="top">Story</td>
            <td valign="top"><textarea name="story" id="story" cols="60" rows="12"></textarea></td>
          </tr>
          <tr> 
            <td height="42" align="right" valign="top">Image Upload </td>
            <td valign="top"><input type="file" name="file" id="file" size="100"></td>
          </tr>
          <tr> 
            <td width="100" height="23"> </td>
            <td valign="top"><input type="submit" name="Submit" value="Submit">
              <input name="Reset" type="reset" id="Reset" value="Reset"></td>
          </tr>
        </table>
        <p> </p>
      </form></td>
    </tr>
</table>
</body>
</html>

Code: Select all

<?php
	
	$img = $_POST['file'];
	$title = $_POST['title'];
	$story = $_POST['story'];
	
	
	
if($file_name !="")
	{
		@copy("$img","../images/$img_name")
		or die("Couldn't copy the file.");
	}
	else
	{
		die("No input file specfied");
	}
$filename="newsboard.html";
$oldfile=fopen($filename, "r");
$file_content=fread($oldfile, filesize($filename));
fclose($oldfile);

$date = (date("d-m-Y"));
//$time = (date("H:i:s"));
$f = fopen($filename, "w+");

fwrite($f,"<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#6699cc">\r\n");
fwrite($f, "<td width="70%"><font color="#000000" size="2" face="arial,helvetica,sans-serif">$date</font></td>\r\n");
//fwrite($f "<td><font size="2" face="arial,helvetica,sans-serif">$time</font></td>\r\n");//this line of code doesn't work!!!
fwrite($f, "</tr>\r\n");
fwrite($f, "<table>\r\n");
fwrite($f, "<table width="100%" border="0" cellspacing="0" cellpadding="2">\r\n");
fwrite($f, "<tr>\r\n");

fwrite($f, "<td width="200" valign="top"><img src="$img_name"></td>\r\n");
fwrite($f, "<td valign="top"><font color="#333366" size="3" face="arial, helvetica, sans-serif"><strong>$title<br>\r\n");
fwrite($f, "</strong><font color="#000000" size="2">$story</font></font></td>\r\n");
fwrite($f, "</tr>\r\n");
fwrite($f, "</table><br>\r\n");

fwrite($f, "\r\n");
fwrite($f, "$file_content");
fclose($f);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Write To Newsboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<table width="634" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td width="634" height="455" valign="top"><p><font face="Arial, Helvetica, sans-serif">Submit 
        Successful</font></p>
      <p><font face="Arial, Helvetica, sans-serif">You Sent: <? echo "$img_name"; ?>, 
        a <? echo "$img_size";?> byte file of a mime type <? echo "$img_type"; ?> 
        <a href="newsboard.html">See Newsboard</a></font></p></td>
  </tr>
</table>
</body>
</html> 
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Does the script have permission to copy the file to the destination? That is to say, what are the file permissions on "../images/" set to?

One thing you could try would be to comment out your "or die("Couldn't copy the file.")" code and see what the PHP error is..
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Uploaded files are in $_FILES not $_POST and they are uploaded to a temporary file name (see move_uploaded_file)

See http://www.php.net/manual/en/features.file-upload.php for examples.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

thanks for the reply i've read up the manual and i do have a better understanding now. however i have re-written the script slightly but still get some errors which this newie can't understand. output i get is:

Code: Select all

Here is some more debugging info:Array ( &#1111;file] =&gt; Array ( &#1111;name] =&gt; menuheader.gif &#1111;type] =&gt; image/gif &#1111;tmp_name] =&gt; C:\WINDOWS\php2C.tmp &#1111;error] =&gt; 0 &#1111;size] =&gt; 507 ) ) Success Submit Successful

You Sent: , a byte file of a mime type See Newsboard
html form is:

Code: Select all

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Newsboard Entry&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;table width="634" border="0" cellpadding="0" cellspacing="0"&gt;
  &lt;!--DWLayoutTable--&gt;
  &lt;tr&gt; 
    &lt;td width="634" height="455" valign="top"&gt;&lt;form action="writetonewsboard.php" method="post" enctype="multipart/form-data"&gt;
        &lt;p&gt;&amp;nbsp;&lt;/p&gt;
        &lt;table width="512" border="0" cellspacing="2" cellpadding="2"&gt;
          &lt;!--DWLayoutTable--&gt;
          &lt;tr&gt; 
            &lt;td height="42" align="right" valign="top"&gt;Entry Title&lt;/td&gt;
            &lt;td width="398" valign="top"&gt;&lt;input type="text" name="title" id="title" size="45"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt; 
            &lt;td height="218" align="right" valign="top"&gt;Story&lt;/td&gt;
            &lt;td valign="top"&gt;&lt;textarea name="story" id="story" cols="60" rows="12"&gt;&lt;/textarea&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt; 
            &lt;td height="42" align="right" valign="top"&gt;Image Upload &lt;/td&gt;
            &lt;td valign="top"&gt;&lt;input name="img_file" type="file" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt; 
            &lt;td width="100" height="23"&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td valign="top"&gt;
				&lt;input type="submit" name="Submit" value="Submit"&gt;
              &lt;input name="Reset" type="reset" id="Reset" value="Reset"&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
        &lt;p&gt;&amp;nbsp;&lt;/p&gt;
      &lt;/form&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
php script is:

Code: Select all

<?php
	
	
	$uploaddir = '/images/';
	$uploadfile = ($_FILES['img_file']['name']);

		echo 'Here is some more debugging info:';
		print_r($_FILES);
	
if (move_uploaded_file($_FILES['img_file']['tmp_name'], '../images/'.$uploadfile));
	{
		echo ' Success';
		
	}

$filename="newsboard.html";
$oldfile=fopen($filename, "r");
$file_content=fread($oldfile, filesize($filename));
fclose($oldfile);

$date = (date("d-m-Y"));
//$time = (date("H:i:s"));
$f = fopen($filename, "w+");

fwrite($f,"<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#6699cc">\r\n");
	fwrite($f, "<td width="70%"><font color="#000000" size="2" face="arial,helvetica,sans-serif">$date</font></td>\r\n");
	//fwrite($f "<td><font size="2" face="arial,helvetica,sans-serif">$time</font></td>\r\n");//this line of code doesn't work!!!
	fwrite($f, "</tr>\r\n");
	fwrite($f, "<table>\r\n");
	fwrite($f, "<table width="100%" border="0" cellspacing="0" cellpadding="2">\r\n");
	fwrite($f, "<tr>\r\n");
	
	fwrite($f, "<td width="200" valign="top"><img src="$img_name"></td>\r\n");
	fwrite($f, "<td valign="top"><font color="#333366" size="3" face="arial, helvetica, sans-serif"><strong>$title<br>\r\n");
	fwrite($f, "</strong><font color="#000000" size="2">$story</font></font></td>\r\n");
	fwrite($f, "</tr>\r\n");
	fwrite($f, "</table><br>\r\n");
	
	fwrite($f, "\r\n");
	fwrite($f, "$file_content");
	fclose($f);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Write To Newsboard</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	</head>
	
	<body>
		
		<table width="634" border="0" cellpadding="0" cellspacing="0">
			<!--DWLayoutTable-->
			<tr> 
				<td width="634" height="455" valign="top"><p><font face="Arial, Helvetica, sans-serif">Submit 
						Successful</font></p>
					<p><font face="Arial, Helvetica, sans-serif">You Sent: <? echo "$uploadfile"; ?>, 
						a <? echo "$img_size";?> byte file of a mime type <? echo "$img_type"; ?> 
						<a href="newsboard.html">See Newsboard</a></font></p></td>
			</tr>
		</table>
	</body>
</html>?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You've almost got it ;)
Here's a slightly tidied up version of yours:

Code: Select all

<?php

$uploaddir = '../images/';
$uploadfile = $_FILES['img_file']['name'];
echo 'Here is some more debugging info:';
print_r($_FILES);

if(empty($_FILES))
{
  die('No file uploaded');
}

if (move_uploaded_file($_FILES['img_file']['tmp_name'], $uploaddir.$uploadfile))
{
  echo ' Success';
} else {
  die('Failed moving the uploaded file.');
}

$filename = 'newsboard.html';
$file_content = file_get_contents($filename);

$date = date('d-m-Y');
//$time = (date("H:i:s"));

$content = <<<EOD
<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#6699cc">
  <tr>
    <td width="70%">
      <font color="#000000" size="2" face="arial,helvetica,sans-serif">{$date}</font>
    </td>
  </tr>
<table>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td width="200" valign="top">
      <img src="$uploadfile">
    </td>
    <td valign="top">
      <font color="#333366" size="3" face="arial, helvetica, sans-serif">
        <strong>{$_POST['title']}<br></strong>
        <font color="#000000" size="2">{$_POST['story']}</font>
      </font>
    </td>
  </tr>
</table>
<br>
EOD;

$f = fopen($filename, 'w+');
fputs($f, $content.$file_content);
fclose($f);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Write To Newsboard</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<table width="634" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="634" height="455" valign="top"><p><font face="Arial, Helvetica, sans-serif">Submit
      Successful</font></p>
      <p><font face="Arial, Helvetica, sans-serif">You Sent: <? echo "$uploadfile"; ?>,
      a <? echo $_FILES['img_file']['size'];?> byte file of a mime type <? echo $_FILES['img_file']['type']; ?>
      <a href="newsboard.html">See Newsboard</a></font></p>
    </td>
  </tr>
</table>
</body>
</html>
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

thanks for that mate helps alot im learning from a php4 book so its a bit outdated now. I also believe i may have a problem with my setup cause i got an error when trying your code. The link it didn't like was:

Code: Select all

a <? echo $_FILES['img_file']['size'];?> byte file of a mime type <? echo $_FILES['img_file']['type']; ?>
it produced this error.

Code: Select all

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\apache group\apache2\htdocs\writetonewsboard.php on line 61
any ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What have you got on the line before that?
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

Code: Select all

<p><font face="Arial, Helvetica, sans-serif">You Sent: <? echo '$uploadfile'; ?>,
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

<p><font face="Arial, Helvetica, sans-serif">You Sent: <? echo "$uploadfile"; ?>,
a <? echo $_FILES['img_file']['size'];?> byte file of a mime type <? echo $_FILES['img_file']['type']; ?>
There's no sysntax error on those two lines (from my post above) though for 'correctness' they should really be:

Code: Select all

<p><font face="Arial, Helvetica, sans-serif">You Sent: <?php echo $uploadfile; ?>,
a <?php echo $_FILES['img_file']['size']; ?> byte file of a mime type <?php echo $_FILES['img_file']['type']; ?>
Use those lines exactly like that and see if the error 'goes away' ;o
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

no same problem the whole script is below not sure what else it could be? :

Code: Select all

<?php
	
	$uploaddir = '../images/';
	$uploadfile = $_FILES['img_file']['name'];
	echo 'Here is some more debugging info:';
	print_r($_FILES);if(empty($_FILES))
	{  
		die('No file uploaded');
	}
	if (move_uploaded_file($_FILES['img_file']['tmp_name'], $uploaddir.$uploadfile))
	{  echo ' Success';
	} else 
	{  
		die('Failed moving the uploaded file.');
	}
	
	$filename="newsboard.html";
	$file_content= file_get_contents($filename);
	
	$date = (date("d-m-Y"));
	//$time = (date("H:i:s"));
	$content = <<<EOD
		
		<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#6699cc">
		<td width="70%"><font color="#000000" size="2" face="arial,helvetica,sans-serif">{$date}</font></td>
	//fwrite($f "<td><font size="2" face="arial,helvetica,sans-serif">$time</font></td>\r\n");//this line of code doesn't work!!!
	</tr>
		<table>
		<table width="100%" border="0" cellspacing="0" cellpadding="2">
		<tr>
		
		<td width="200" valign="top"><img src="$uploadfile"></td>
		<td valign="top"><font color="#333366" size="3" face="arial, helvetica, sans-serif"><strong>{$_POST['title']}<br>
		</strong><font color="#000000" size="2">{$_POST['story']}</font></font></td>
		</tr>
		</table><br>
		EOD;
	$f = fopen($filename, 'w+');
	fputs($f, $content.$file_content);
	fclose($f);
		
		
		
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Write To Newsboard</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	</head>
	
	<body>
		
		<table width="634" border="0" cellpadding="0" cellspacing="0">
			<!--DWLayoutTable-->
			<tr> 
				<td width="634" height="455" valign="top"><p><font face="Arial, Helvetica, sans-serif">Submit 
						Successful</font></p>
					<p><font face="Arial, Helvetica, sans-serif">You Sent: <?php echo '$uploadfile'; ?>, 
						a <?php echo $_FILES['img_file']['size'];?> byte file of a mime type <?php echo $_FILES['img_file']['type']; ?> 
						<a href="newsboard.html">See Newsboard</a></font></p></td>
			</tr>
		</table>
	</body>
</html>
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

tidy the code up a little bit more and its now

Code: Select all

<?php
	
	$uploaddir = '../images/';
	$uploadfile = $_FILES['img_file']['name'];
	echo 'Here is some more debugging info:';
	print_r($_FILES);if(empty($_FILES))
	{  
		die('No file uploaded');
	}
	if (move_uploaded_file($_FILES['img_file']['tmp_name'], $uploaddir.$uploadfile))
	{  echo ' Success';
	} else 
	{  
		die('Failed moving the uploaded file.');
	}
	
	$filename="newsboard.html";
	$file_content= file_get_contents($filename);
	
	$date = (date("d-m-Y"));
	$time = (date("H:i:s"));
	$content = <<<EOD
		
		<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#6699cc">
		<td width="70%"><font color="#000000" size="2" face="arial,helvetica,sans-serif">{$date}</font></td>
	<td><font size="2" face="arial,helvetica,sans-serif">{$time}</font></td>
	</tr>
		<table>
		<table width="100%" border="0" cellspacing="0" cellpadding="2">
		<tr>
		
		<td width="200" valign="top"><img src="$uploadfile"></td>
		<td valign="top"><font color="#333366" size="3" face="arial, helvetica, sans-serif"><strong>{$_POST['title']}<br>
		</strong><font color="#000000" size="2">{$_POST['story']}</font></font></td>
		</tr>
		</table>
		<br>
		EOD;
	
	$f = fopen($filename, 'w+');
	fputs($f, $content.$file_content);
	fclose($f);
		
		
		
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Write To Newsboard</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	</head>
	
	<body>
		
		<table width="634" border="0" cellpadding="0" cellspacing="0">
			<!--DWLayoutTable-->
			<tr> 
				<td width="634" height="455" valign="top"><p><font face="Arial, Helvetica, sans-serif">Submit 
						Successful</font></p>
					<p><font face="Arial, Helvetica, sans-serif">You Sent: <?php echo '$uploadfile'; ?>, 
						a <?php echo $_FILES['img_file']['size'];?> byte file of a mime type <?php echo $_FILES['img_file']['type']; ?> 
						<a href="newsboard.html">See Newsboard</a></font></p></td>
			</tr>
		</table>
	</body>
</html>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ah, right.
In my post the EOD; line is not indented at all, it's right up against the left hand side .. for a reason ;)
That whole <<<EOD thing i've used is called heredoc and the closing EOD; must not be indented or have any spaces before it at all.
Post Reply