为什么文件上传不起作用?

时间:2015-08-13 16:02:28

标签: php

这是我之前提问的编辑版本。我的问题是我的文件上传代码不起作用。它不会将文件上传到服务器,但会将文件名保存在数据库中。我花了很长时间试图解决这个问题而没有运气。我正在学习编码。我认为问题出在我的页面结构中。这是整个页面的代码。有人可以指点我哪里出错了吗?

这是我的代码:

 <form name="form" method="post" onSubmit="return checkForm(this);"> 
 <?php

if (!isset($submit)) {

mysql_connect('localhost', 'user', 'password!') or die(mysql_error());
mysql_select_db("host_table") or die(mysql_error());

$id = $_GET[id];

$query2 = "SELECT * FROM table WHERE id='$id'";

$result2 = mysql_query ($query2) or die ('Could not run query: ' . mysql_error());

$info = mysql_fetch_array ($result2);

?> 
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
<tr> 
  <td width="16" height="38" bgcolor="#eaeaea"></td> 
  <td width="1280" valign="middle" bgcolor="#eaeaea"><span class="heading">Editing Application </span><br> </td> 
  <td width="16" bgcolor="#eaeaea"></td> 
</tr> 
<tr> 
  <td background="../img/box/left_line.jpg"></td> 
  <td> <table width="100%"  border="0.1" align="left" class="formtext"> 
      <tr> 
        <td><br> 
          Applicant Name:<span class="style4">*</span><br> 
          <input name="firstname" type="text" class="style5" id="firstname" value="<?php echo "$info[firstname]"; ?>" size="50" maxlength="100" /> 
          <br> 
          <span class="formnotes">(First name)</span> <br> 
          <br> 
          <input name="lastname" type="text" class="style5" id="lastname" value="<?php echo "$info[lastname]"; ?>" size="50" maxlength="100" /> 
          <br> 
          <span class="formnotes">(Last name)</span> <br> 
          <br> 
          Are you?<span class="style4">*</span> 
          <SELECT name="applicant_type" class="style5" id="applicant_type"> 
            <OPTION VALUE="<? echo "$info[applicant_type]"; ?>"><? echo "$info[applicant_type]"; ?></OPTION> 
            <OPTION value="Student">Student</OPTION> 
            <OPTION value="Professor">Professor</OPTION> 
            <OPTION value="Professional">Professional</OPTION> 
          </SELECT> 
          <br> 
          <br> 
          Applicant Email Address:<span class="style4">*</span><br> 
          <input name="email" type="text" class="style5" id="email" value="<?php echo "$info[email]"; ?>"  size="50" maxlength="100" /> 
          <br> 
          Applicant Mailing Address:<span class="style4">*</span><br> 
          <input name="address" type="text" class="style5" id="address" value="<?php echo "$info[address]"; ?>" size="50" maxlength="100" /> 
          <br> 
          <span class="formnotes">(Street or PO Box address) </span> <br> 
          <br> 
          If Non-US Enter Your Nation:<br> 
          <input name="nation" type="text" class="style5" id="nation" value="<?php echo "$info[nation]"; ?>" size="50" maxlength="100" /> 
          <br> 
          <br> 
          City:<span class="style4">*</span> 
          <input name="city" type="text" class="style5" id="city" value="<?php echo "$info[city]"; ?>" size="27" maxlength="80" /> 
&nbsp;State:<span class="style4">*</span> 
          <select name="state" id="state" class="style5"> 
            <option value="<?php echo "$info[state]"; ?>"><?php echo  "$info[state]"; ?></option> 
            <option value="Non-US">Non-US</option> 
            <option value="AK">AK</option> 
            <option value="AL">AL</option> 
            <option value="AR">AR</option> 
            <option value="AZ">AZ</option> 
            <option value="CA">CA</option> 
            <option value="CO">CO</option> 
            <option value="CT">CT</option> 
            <option value="DC">DC</option> 
            <option value="DE">DE</option> 
            <option value="FL">FL</option> 
            <option value="GA">GA</option> 
            <option value="HI">HI</option> 
            <option value="IA">IA</option> 
            <option value="ID">ID</option> 
            <option value="IL">IL</option> 
            <option value="IN">IN</option> 
            <option value="KS">KS</option> 
            <option value="KY">KY</option> 
            <option value="LA">LA</option> 
            <option value="MA">MA</option> 
            <option value="MD">MD</option> 
            <option value="ME">ME</option> 
            <option value="MI">MI</option> 
            <option value="MN">MN</option> 
            <option value="MO">MO</option> 
            <option value="MS">MS</option> 
            <option value="MT">MT</option> 
            <option value="NC">NC</option> 
            <option value="ND">ND</option> 
            <option value="NE">NE</option> 
            <option value="NH">NH</option> 
            <option value="NJ">NJ</option> 
            <option value="NM">NM</option> 
            <option value="NV">NV</option> 
            <option value="NY">NY</option> 
            <option value="OH">OH</option> 
            <option value="OK">OK</option> 
            <option value="OR">OR</option> 
            <option value="PA">PA</option> 
            <option value="RI">RI</option> 
            <option value="SC">SC</option> 
            <option value="SD">SD</option> 
            <option value="TN">TN</option> 
            <option value="TX">TX</option> 
            <option value="UT">UT</option> 
            <option value="VA">VA</option> 
            <option value="VT">VT</option> 
            <option value="WA">WA</option> 
            <option value="WI">WI</option> 
            <option value="WV">WV</option> 
            <option value="WY">WY</option> 
            <option value="AA">AA</option> 
            <option value="AE">AE</option> 
            <option value="AP">AP</option> 
            <option value="AS">AS</option> 
            <option value="PR">PR</option> 
            <option value="FM">FM</option> 
            <option value="GU">GU</option> 
            <option value="MH">MH</option> 
            <option value="MP">MP</option> 
            <option value="PW">PW</option> 
            <option value="VI">VI</option> 
            <? echo "$state" ?> 
          </select> 
  &nbsp;Zip Code:<span class="style4">*</span> 
          <input type="text" class="style5" name="zip" id="zip" value="<?php echo "$info[zip]"; ?>"size="10" maxlength="15" 

  onKeyPress="return checkIt(event)"> 
          <br> 
          <br> 
          Applicant Telephone:<br> 
          <input type="text" class="style5" name="tel" id="tel"value="<?php echo "$info[tel]"; ?>"size="40" maxlength="25"

  onKeyPress="return checkIt(event)"> 
          <br> 
          <span class="formnotes">(e.g.: 8638859044)</span> <br> 
          <br> 
          Social Media Page 1:<br> 
          <span class="formnotes">http://</span> 
          <input name="social1" type="text" class="style5" id="social1" value="<?php echo "$info[social1]"; ?>" size="60" maxlength="200" /> 
          <br> 
          <span class="formnotes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Facebook, LinkedIn, etc.) </span> <br> 
          <br> 
          Social Media Page 2:<br> 
          <span class="formnotes">http://</span> 
          <input name="social2" type="text" class="style5" id="social2" value="<?php echo "$info[social2]"; ?>" size="60" maxlength="200" / > 
          <br> 
          <span class="formnotes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Facebook, LinkedIn, etc.) </span><br> 
          <br> 
          Website:<br> 
          <span class="formnotes">http://</span> 
          <input name="web" type="text" class="style5" id="web" value="<?php echo "$info[web]"; ?>" size="60" maxlength="200" / > 
          <br> 
          <br> 
          University Name:<span class="style4">*</span><br> 
          <input name="college" type="text" class="style5" id="college" value="<?php echo "$info[college]"; ?>" size="70" maxlength="100" /> 
          <br> 
          <br> 
          Academic Department:<span class="style4">*</span><br> 
          <input name="department" type="text" class="style5" id="department" value="<?php echo "$info[department]"; ?>" size="70" maxlength="150" /> 
          <br> 
          <span class="formnotes">(History, Mathematics, Psychology, Chemical Engineering, etc.)</span> <br> 
          <br> 
          Degree Major:<span class="style4">*</span><br> 
          <input name="major" type="text" class="style5" id="major" value="<?php echo "$info[major]"; ?>" size="50" maxlength="150" /> 
          <br> 
          <span class="formnotes">(Anthropology, Biology, Chemistry and etc.) </span> <br> 
          <br> 
          Degree Pursued:<span class="style4">*</span><br> 
          <input name="degree" type="text" class="style5" id="degree3" value="<?php echo "$info[degree]"; ?>" size="50" maxlength="150" /> 
          <br> 
          <span class="formnotes">(Masters, PhD, etc.) </span> <br> 
          <br> 
          Faculty Mentor Name:<span class="style4">*</span><br> 
          <input name="mentor" type="text" class="style5" id="mentor" value="<?php echo "$info[mentor]"; ?>" size="60" maxlength="100" /> 
          <span class="formnotes"> <br> 
          (If you are a graduate student)</span> <br> 
          <br> 
          Faculty Mentor Email: <span class="style4">*</span><br> 
          <input name="mentor_email" type="text" class="style5" id="mentor_email" value="<?php echo "$info[mentor_email]"; ?>" size="40" / maxlength="80"> 
          <br> 
          <br> 
          Select Presentation Type:<span class="style4">*</span><br> 
          <SELECT name="session" class="style5" id="session"> 
            <OPTION VALUE="<?php echo "$info[session]"; ?>"><?php echo "$info[session]"; ?></OPTION> 
            <OPTION value="Poster Session Only">Poster Session Only</OPTION> 
            <OPTION value="Presentation Session Only">Presentation Session Only</OPTION> 
            <OPTION value="Poster or Presentation">Poster or Presentation</OPTION> 
          </SELECT> 
          <br> 
          <br> 
          Brief Presentation Description:<span class="style4">*</span><br> 
          <textarea name="presentation" cols="70" rows="15" class="style5" id="presentation" onKeyUp="limitText(this,2900);" onkeyup="valid(this)" onblur="valid(this)"> 
          <?php echo "$info[presentation]"; ?> 
          </textarea> 
          <br> 
          <span class="formnotes">(Please limit abstract to 400 words) </span> <br> 
          <br> 
          We are open to alternative proposal about the session and presentation, <br> 
          don't hesitate to offer suggestions:<br> 
          <textarea name="suggestions" cols="70" rows="3" class="style5" id="suggestions"><?php echo "$info[suggestions]"; ?></textarea> 
          <br> 
          <br> 
          How did you hear about the CCC and its symposium in Paris? <br> 
          <input type="checkbox" name="call_for_papers" value="<?php echo "$info[call_for_paper]"; ?>"> 
          Call for Papers <br> 
          <input type="checkbox" name="student_org" value="<?php echo "$info[student_org]"; ?>" id="student_org"> 
          Student Organization <br> 
          <input type="checkbox" name="other" value="<?php echo "$info[other]"; ?>" id="other"> 
          Other
          <input name="other" type="text" class="style5" id="other" value="<?php echo "$info[other]"; ?>" size="50" maxlength="100"> 
          <br> 
          <br> 
          Do you have any remarks or questions?<br> 
          <textarea name="questions" cols="70" rows="3" class="style5" id="questions"><?php echo "$info[questions]"; ?></textarea> 
          <br> 
          <br> 
          <br> 
          Password:<span class="style4">*</span> 
          <input type="password" class="style5" name="pwd" value="<?php echo "$info[pwd]"; ?>"> 
          <br> 
          <br> 
          Confirm Password:<span class="style4">*</span> 
          <input type="password" class="style5" name="pwd1" value="<?php echo "$info[pwd]"; ?>"> 
          <br> 
          <span class="formnotes">(For future use if you need to modify your application) </span><br> 
          <br> 
          <br> 
          Please attach your files here: </b> <br> 
          <table width="582"  border="0" cellpadding="0" cellspacing="0"> 
            <tr valign="bottom"> 
              <td height="37" colspan="3">Abstract: </td> 
            </tr> 
            <tr> 
              <td width="247" height="38"><input name="abstract" readonly type="text" size="30" class="style5" value="<? echo "$info[abstract]";?>"/  > 
                <br> 
                <span class="formnotes">(Current value)</span></td> 
              <td width="26" valign="top"><label> 
                <input type="checkbox" id="confirm" style="height:20px;width:20px"> 
                </label></td> 
              <td width="316" valign="middle"> <input name="abstract" type="file" id="abstract" size="30" class="style5" disabled / onchange="ValidateSingleInput(this);"> 
                <br> 
                <span class="formnotes">(Click the checkbox to upload a new PDF file</span></td> 
            </tr> 
            <tr valign="bottom"> 
              <td height="37" colspan="3">Previous Paper or Poster: </td> 
            </tr> 
            <tr> 
              <td height="37"><input name="poster" readonly type="text" size="30" class="style5" value="<? echo "$info[poster]";?>"/> 
                <br> 
                <span class="formnotes">(Current value)</span></td> 
              <td width="26" valign="top"> <label> 
                <input type="checkbox" id="confirmp" size="20" style="height:20px;width:20px"> 
                </label> </td> 
              <td><input name="poster" type="file" id="poster" size="30" class="style5" disabled / onchange="ValidateSingleInput(this);"> 
                <br> 
                <span class="formnotes">(Click the checkbox to upload a new PDF file)</span></td> 
            </tr> 
            <tr valign="bottom"> 
              <td height="37" colspan="3">Resume: </td> 
            </tr> 
            <tr> 
              <td height="37"><input name="resume" readonly type="text" size="30" class="style5" value="<? echo "$info[resume]";?>"/> 
                <br> 
                <span class="formnotes">(Current value)</span></td> 
              <td width="26" valign="top"> <label> 
                <input type="checkbox" id="confirmr" style="height:20px;width:20px"> 
                </label> </td> 
              <td><input name="resume" type="file" id="resume" size="30" class="style5" disabled / onchange="ValidateSingleInput(this);"> 
                <br> 
                <span class="formnotes">(Click the checkbox to upload a new PDF file)</span></td> 
            </tr> 
          </table> 
          <br> 
          <span class="style4">*</span> Required
          </p></td> 
      </tr> 
      <tr> 
        <td><p> <a href="javascript: history.go(-1)" class="formtext"> 
            <input name="goback" type="button" id="goback" value="Go back with no change" class="button"> 
            </a> 
            <input name="submit" type="submit" id="submit" value="Submit" class="button"> 
          </p></td> 
      </tr> 
    </table></td> 
  <td valign="baseline" background="../img/box/right_line.jpg"></td> 
</tr> 
<tr> 
  <td><img src="../img/box/left_corner.jpg" width="16" height="14"></td> 
  <td background="../img/box/bottom_line.jpg"></td> 
  <td><img src="../img/box/right_corner.jpg" width="16" height="14"></td> 
</tr> 

   

} else {
mysql_connect('localhost', 'user', 'password') or die(mysql_error());
mysql_select_db("host_table") or die(mysql_error());

include ('includes/sql.php');

$query1 = "UPDATE table SET firstname = '$firstname', lastname = '$lastname', applicant_type = '$applicant_type', email = '$email', address = '$address', nation = '$nation', city = '$city', state = '$state', zip = '$zip', tel = '$tel', social1 = '$social1', social2 = '$social2', web = '$web', college = '$college', department = '$department', major = '$major', degree = '$degree',  mentor = '$mentor', mentor_email = '$mentor_email', session = '$session', presentation = '$presentation', suggestions = '$suggestions', call_for_papers = '$call_for_papers', student_org = '$student_org', other = '$other', questions = '$questions', pwd = '$pwd', abstract = '$abstract', poster = '$poster', resume = '$resume' WHERE id='$id'";

    $result1 = mysql_query($query1) or die ('Could not run query: '.mysql_error());

    if ($result1) { echo "<br><br><center><span class='alertext'>Successfully modified!</span>"; 

?> 

$date=date("Y-m-d");
//Abstract File codes
$info = pathinfo($_FILES['abstract']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $lastname.-rand(10,5000)."_Abstract.".$ext;
$target = "application/abstracts/";  $target = $target .$newname;
$abstract=$newname;

//Poster File codes
$info = pathinfo($_FILES['poster']['name']);
$ext = $info['extension']; // get the extension of the file
$newname1 = $lastname.-rand(10,5000)."_Poster.".$ext;
$target1 = "application/posters/";  $target1 = $target1 .$newname1;
$poster=$newname1;

//Resume File codes
$info = pathinfo($_FILES['resume']['name']);
$ext = $info['extension']; // get the extension of the file
$newname2 = $lastname.-rand(10,5000)."_Resume.".$ext;
$target2 = "application/resumes/";  $target2 = $target2 .$newname2;
$resume=$newname2;

$error = false;

if(move_uploaded_file($_FILES['abstract']['tmp_name'], $target)) 

{   
//echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been    uploaded, and your information has been added to the directory<br>";  

} else $error = true;

if(move_uploaded_file($_FILES['poster']['tmp_name'], $target1))  
{   
//echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory <br>";  
 } else $error = true; 

if(move_uploaded_file($_FILES['resume']['tmp_name'], $target2))  
{   
//echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory<br>";  
} else $error = true;
?> 
<center> 
<br> 
<span class="formtext">:: <a href="javascript: history.go(-1)">Go back</a>  ::</span><br> 
<br> 
<span class="formtext">:: <a href='site'>Log out</a> ::</span>     <br> 
<?

}else { echo "<br><br><center><span class='alertext'>Error while modifying!    </span>"; }

}


// sending confirmation messages

        if ($email<>NULL) 
        {
            $message = "Dear $name, \n\n";
            $message .= "Your application has been successfully updated.  Below is a copy for your records.\n\n";

        }
        if ($sentmail) 
        {

            //Copy of the message sent to the CCC Team

            $message = "CCC Application Update message sent by $name on  ".date("Y-m-d")."\n\n";

}
?> 
</form>

2 个答案:

答案 0 :(得分:0)

您的表单需要具有enctype="multipart/form-data"属性才能上传任何文件。

答案 1 :(得分:0)

@Avalanche帮助我修复了我的代码。以下是解决方案: 1.添加enctype =&#34; multipart / form-data&#34;形式;和 2.使用绝对路径,因为它是一个编辑页面。因此,而不是&#34; application / abstracts /&#34;使用/home/hostname/public_html/website.com/directory /

再次感谢您的帮助@Avalanche!