Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

mysql - PHP form not uploading file

I have a PHP form that is inserting information into a database. It all works except for the file upload. The filename needs to get placed inside the "image" column in the database table and the file needs to be put in the directory as well. Funny thing is that this uploader was working yesterday. :-S

Please can someone just review my PHP code and see if i am missing something?? Thanks so much!!

FORM CODE:

<form name="upload_announcement" method="post" action="PostAnnouncement.php">
Title: (limit 35 characters)<br />
<input type="text" name="title" maxlength="32" style="width:200px;" /><br /><br />
Message: (limit 500 html characters)<br />
<textarea name="message" cols="60" rows="20"></textarea><br /><br />
Upload Image: (Specs: .jpg format, 255px X 255px, and less than 500kb in size.)<br />
<input type="file" name="image" id="image" style="color:#fff;" />
<br /><br />
Start Date:<br />
<input name="dateStart" type="text" id="dateStartImg" />
<br /><br />
End Date:<br />
<input name="dateEnd" type="text" id="dateEndImg" />
<br /><br />
<input type="hidden" name="customerId" value="<?php echo $_COOKIE['customerId']; ?>" />
<input type="submit" name="upload" value="Upload Announcement" />
</form>`

SUBMIT CODE:<br />
`include('ConfigRead.php');

$customerId = $_COOKIE['customerId'];
$select = mysql_query('select filingName from user where customerId = '.$customerId.' limit 1') or die('Error: ' . mysql_error());
$selectRow = mysql_fetch_array( $select );
$filingName = $selectRow['filingName'];
$imageFileName = $_FILES['image']['name'];

if((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/pjpeg"))
&& ($_FILES["image"]["size"] < 500000))
{
  if($_FILES["image"]["error"] > 0){
    header("location:Announcements.php?file=error");
  }else{
    move_uploaded_file($_FILES["image"]["tmp_name"],
    "../Admin/CustomerFiles/Announcements/" . $filingName . "/" . $imageFileName);
  }
}else{
  header("location:Announcements.php?file=error");
}

$sql="INSERT INTO announcements (customerId, filingName, title, message, image, dateStart, dateEnd) VALUES ('$_POST[customerId]','$filingName','$_POST[title]','$_POST[message]','$imageFileName','$_POST[dateStart]','$_POST[dateEnd]')";

if (!mysql_query($sql,$connRead))
  {
  die('Error: ' . mysql_error());
  }

include('CloseConnRead.php');

header("location:ManageAnnouncements.php?add=success");`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add the multipart/form-data enctype to your form when uploading files

<form name="upload_announcement" method="post" enctype="multipart/form-data" action="PostAnnouncement.php">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...