-2

this is an edited version of my previous question. My problem is that my file upload code is not working. It won't upload files to the server but saves the file name in the database. I have spent a long time trying to solve this with no luck. I am learning coding. I think is that the problem is in the structure of my page. Here is the code for the whole page. Could someone please point to me where I went wrong?

Here is my code:

 <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>
Nargis
  • 25
  • 7
  • I just now added my code to the question. Thanks. – Nargis Aug 13 '15 at 16:25
  • ODelibalta, I just posted my whole page's code. Could you please take a look if time permits. I really have spent long time researching and trying to fix the problem with no luck. I would really appreciate any help or comments. Thank you. – Nargis Aug 13 '15 at 21:11
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 13 '15 at 21:21
  • Thanks. I will try to implement that. – Nargis Aug 13 '15 at 21:27

2 Answers2

0

Your form needs to have an attribute of enctype="multipart/form-data" in order to be able to upload any files.

Avalanche
  • 1,468
  • 1
  • 11
  • 18
  • Dear Avalanche, Thanks! I added this to the form. I added an echo to see if I would get any results. After I submit I am getting a message that file has been uploaded and it also shows file size. But when I checked my server/folder it is still empty. – Nargis Aug 13 '15 at 21:26
  • @Nargis You are using relative paths, make sure they are accessible. It is generally a better option to use absolute paths so you know where exactly you are placing your files. Also, check your permissions - you may not have enough to write files. You should usually see errors, make sure you have error reporting enabled. Ultimately, you can simply try to write the file in the current directory with a dummy name, just to make sure your script is working correctly and then continue with more complicated actions. Many things can go wrong, as you can see :) – Avalanche Aug 13 '15 at 21:38
  • Dear Avalanche, Thank you. I have the permission to write files set up. I will try change the path and write file in the directory as you suggested. – Nargis Aug 13 '15 at 21:43
  • Dear @Avalanche, You are a hero! I tried to use absolute path: /home/host/www/site/directory/ and it worked! Thank you so much! I really appreciate your help! – Nargis Aug 13 '15 at 22:12
0

@Avalanche helped me to fix my code. Here are the solutions: 1. adding enctype="multipart/form-data" to the form; and 2. using absolute path since it is an edit page. So instead of "application/abstracts/" use /home/hostname/public_html/website.com/directory/

Thank you again for your help @Avalanche!

Nargis
  • 25
  • 7