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
mmc01ms
Forum Commoner
Posts: 97 Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK
Post
by mmc01ms » Tue Dec 21, 2004 5:43 pm
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>&nbsp;</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">&nbsp;</td>
<td valign="top"><input type="submit" name="Submit" value="Submit">
<input name="Reset" type="reset" id="Reset" value="Reset"></td>
</tr>
</table>
<p>&nbsp;</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>
?>
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Dec 21, 2004 5:54 pm
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..
mmc01ms
Forum Commoner
Posts: 97 Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK
Post
by mmc01ms » Wed Dec 22, 2004 4:35 am
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 ( їfile] => Array ( їname] => menuheader.gif їtype] => image/gif їtmp_name] => C:\WINDOWS\php2C.tmp їerror] => 0 їsize] => 507 ) ) Success Submit Successful
You Sent: , a byte file of a mime type See Newsboard
html form is:
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>&nbsp;</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 name="img_file" type="file" /></td>
</tr>
<tr>
<td width="100" height="23">&nbsp;</td>
<td valign="top">
<input type="submit" name="Submit" value="Submit">
<input name="Reset" type="reset" id="Reset" value="Reset"></td>
</tr>
</table>
<p>&nbsp;</p>
</form></td>
</tr>
</table>
</body>
</html>
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>?>
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Dec 22, 2004 5:10 am
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 » Wed Dec 22, 2004 6:41 am
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?
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Dec 22, 2004 6:46 am
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 » Wed Dec 22, 2004 12:36 pm
Code: Select all
<p><font face="Arial, Helvetica, sans-serif">You Sent: <? echo '$uploadfile'; ?>,
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Dec 22, 2004 12:43 pm
<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 » Wed Dec 22, 2004 3:51 pm
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 » Wed Dec 22, 2004 3:55 pm
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>
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Dec 23, 2004 4:41 am
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.