PHP image upload and rename script not working

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
ReubenDog
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2011 4:02 pm

PHP image upload and rename script not working

Post by ReubenDog »

PHP image upload and rename script not working

Postby ReubenDog » November 6th, 2011, 6:08 pm
Hello, I'm fairly new to PHP and the web classes that I took in school touched very lightly on the subject, but nothing really in depth. I have a new client who's site is incorporating some really complex PHP scripting (well at least to me anyway). He requires a script that will take an uploaded image from a form, rename it then move the file with the new name to a corresponding folder. Below I have a script that seems to work on a Mac at my school with a Linux server, but doesn't seem to work on my Windows PC at the office with a XAMPP test server nor on my clients hosting server which is a Windows server (go figure). If someone can please help me out it would be greatly appreciated and thank you in advance. If the form is required to try and troubleshoot this problem please let me know and I'll provide it to you.

//host, username, and password are missing for security purposes.

Code: Select all

<?php
$host="";
$username="";
$password="";
$db_name="mysql";
$tbl_name="yearbook";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$ftype = $_FILES['fileUpload']['type'];
if (($ftype=="image/jpeg")||($ftype=="image/tiff")||($ftype=="image/gif")){
$file_name = $_FILES['fileUpload']['name'];
$new_file_name=$_POST['fName'].' '.$_POST['mName'].' '.$_POST['lName'].' | '.$file_name;

$path= "uploads/yearBookImages/".$new_file_name;
$fileUpload=$new_file_name;
if($fileUpload){
if(copy($_FILES['fileUpload']['tmp_name'], $path)){
}else{
echo "Error";
}
}
$fName=$_POST['fName'];
$mName=$_POST['mName'];
$lName=$_POST['lName'];
$chapter=$_POST['chapter'];
$year1=$_POST['year1'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$email=$_POST['email'];
$month=$_POST['month'];
$year2=$_POST['year2'];
$business=$_POST['business'];
$title=$_POST['title'];
$skills=$_POST['skills'];
$interests=$_POST['interests'];

$insert = "INSERT INTO yearbook (fileUpload, fName, mName, lName, chapter, year1, address, city, state, zip, email, month, year2, business, title, skills, interests)
VALUES ('$fileUpload','$fName','$mName','$lName','$chapter','$year1','$address','$city','$state','$zip','$email','$month','$year2','$business','$title','$skills','$interests')";

$add_yearbook = mysql_query($insert);
?>

<div id="subHeaderText"><small>Yearbook info has been uploaded successfully...</small></div>
<hr />
<div id="subHeaderText" align="right"><small><a href="index.php">Back to home...</a></small></div>
<?php

}else{

?>
<div id="subHeaderText"><small>Yearbook info was not uploaded successfully. <u>Please double-check the file format type.</u></small></div>
<?php
}
?>
Last edited by Benjamin on Mon Nov 07, 2011 4:23 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP image upload and rename script not working

Post by social_experiment »

Are you receiving any error messages when you attempt to upload the file?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ReubenDog
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2011 4:02 pm

Re: PHP image upload and rename script not working

Post by ReubenDog »

Yes, it states "Error, Yearbook info has been uploaded successfully..." although nothing gets sent to the database. Before I would get the error but the at least the info would get stored but the image wouldn't get moved and renamed, now nothing. Let me know if you would like to see a screen shot, and thanks so much for your help.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP image upload and rename script not working

Post by social_experiment »

Code: Select all

<?php $add_yearbook = mysql_query($insert) or die(mysql_error()); ?>
While the code is in production it helps to use mysql_error() to see any errors if they occur during database transactions.

The reason why you are receiving the success message although data is being added is the placement inside the first if statement; your the code will display if your file type is of a specific type. To see the success message, or to receive it at the proper time you need to modify the code

Code: Select all

<?php
 $query = mysql_query($sql) or die(mysql_error());
 if ($query) {
    // echo success message here
 }
 else {
    // query failed, display error message
 }
?>
Depending on the type of sql statement, on success $query will return true so you can use that to determine which message to display.

Can you paste the form that you are using to upload the files?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ReubenDog
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2011 4:02 pm

Re: PHP image upload and rename script not working

Post by ReubenDog »

Here you go, and thanks again for all your help. My instructor was supposed to help me with this project but he doesn't seem to understand what's wrong (go figure).

Code: Select all

<form action="CB_yearBookProcessing.php" method="post" id="yearbook" enctype="multipart/form-data" />
<table width="700" border="0" cellpadding="2" style="font-family:Arial, Helvetica, sans-serif">
  <tr>
    <td colspan="3"><div id="headerText">Please fill out the form below... </div><div id="subHeaderText"><small>Attachment file must be 2GB maximum and in formats .jpg, .tif, or .gif.</small></div></td>
  </tr>
  <tr>
    <td colspan="3"></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><div id="subHeaderText"><small>Select image file to upload... </small>
      <input type="file" name="fileUpload" id="fileUpload" /></div></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Name</small></div></td>
  </tr>
  <tr>
    <td width="158"><div id="subHeaderText"><small><small>First</small></small></div></td>
    <td width="161"><div id="subHeaderText"><small><small>Middle</small></small></div></td>
    <td width="226"><div id="subHeaderText"><small><small>Last</small></small></div></td>
  </tr>
  <tr>
    <td><input type="text" name="firstName" /></td>
    <td><input type="text" name="middleName" /></td>
    <td><input type="text" name="lastName" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Chapter of Initiation</small></div></td>
  </tr>
  <tr>
    <td colspan="3"><input type="text" name="chapter" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Year of Initiation</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
        <select name="year1">
          <option>2011</option>
          <option>2010</option>
          <option>2009</option>
          <option>2008</option>
          <option>2007</option>
          <option>2006</option>
          <option>2005</option>
          <option>2004</option>
          <option>2003</option>
          <option>2002</option>
          <option>2001</option>
		  <option>2000</option>
        </select>
      </form></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Address</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
        <input name="address" type="text" size="50" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td><div id="headerText"><small>City</small></div></td>
    <td><div id="headerText"><small>State</small></div></td>
    <td><div id="headerText"><small>Zip Code</small></div></td>
  </tr>
  <tr>
    <td>
        <input type="text" name="city" /></td>
    <td>
        <select name="state">
          <option value="AL">Alabama</option>
          <option value="AK">Alaska</option>
          <option value="AZ">Arizona</option>
          <option value="AR">Arkansas</option>
          <option value="CA">California</option>
          <option value="CO">Colorado</option>
          <option value="CT">Connecticut</option>
          <option value="DE">Delaware</option>
          <option value="DC">District of Columbia</option>
          <option value="FL">Florida</option>
          <option value="GA">Georgia</option>
          <option value="HI">Hawaii</option>
          <option value="ID">Idaho</option>
          <option value="IL">Illinois</option>
          <option value="IN">Indiana</option>
          <option value="IA">Iowa</option>
          <option value="KS">Kansas</option>
          <option value="KY">Kentucky</option>
          <option value="LA">Louisiana</option>
          <option value="ME">Maine</option>
          <option value="MD">Maryland</option>
          <option value="MA">Massachusetts</option>
          <option value="MI">Michigan</option>
          <option value="MN">Minnesota</option>
          <option value="MS">Mississippi</option>
          <option value="MO">Missouri</option>
          <option value="MT">Montana</option>
          <option value="NE">Nebraska</option>
          <option value="NV">Nevada</option>
          <option value="NH">New Hampshire</option>
          <option value="NJ">New Jersey</option>
          <option value="NM">New Mexico</option>
          <option value="NY">New York</option>
          <option value="NC">North Carolina</option>
          <option value="ND">North Dakota</option>
          <option value="OH">Ohio</option>
          <option value="OK">Oklahoma</option>
          <option value="OR">Oregon</option>
          <option value="PA">Pennsylvania</option>
          <option value="RI">Rhode Island</option>
          <option value="SC">South Carolina</option>
          <option value="SD">South Dakota</option>
          <option value="TN">Tennessee</option>
          <option value="TX">Texas</option>
          <option value="UT">Utah</option>
          <option value="VT">Vermont</option>
          <option value="VA">Virginia</option>
          <option value="WA">Washington</option>
          <option value="WV">West Virginia</option>
          <option value="WI">Wisconsin</option>
          <option value="WY">Wyoming</option>
        </select>
      </form></td>
    <td>
        <input type="text" name="zip" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Personal E-mail :</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
        <input name="email" type="text" size="50" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Birth date (Month & Year)</small></div></td>
  </tr>
  <tr>
    <td>
        <select name="month">
          <option>January</option>
          <option>February</option>
          <option>March</option>
          <option>April</option>
          <option>May</option>
          <option>June</option>
          <option>July</option>
          <option>August</option>
          <option>September</option>
          <option>October</option>
          <option>November</option>
          <option>December</option>
        </select>    </td>
    <td>
        <select name="year2">
          <option>2011</option>
          <option>2010</option>
          <option>2009</option>
          <option>2008</option>
          <option>2007</option>
          <option>2006</option>
          <option>2005</option>
          <option>2004</option>
          <option>2003</option>
          <option>2002</option>
          <option>2001</option>
          <option>2000</option>
          <option>1999</option>
          <option>1998</option>
          <option>1997</option>
          <option>1996</option>
          <option>1995</option>
          <option>1994</option>
          <option>1993</option>
          <option>1992</option>
          <option>1991</option>
          <option>1990</option>
          <option>1989</option>
          <option>1988</option>
          <option>1987</option>
          <option>1986</option>
          <option>1985</option>
          <option>1984</option>
          <option>1983</option>
          <option>1982</option>
          <option>1981</option>
          <option>1980</option>
          <option>1979</option>
          <option>1978</option>
          <option>1977</option>
          <option>1976</option>
          <option>1975</option>
          <option>1974</option>
          <option>1973</option>
          <option>1972</option>
          <option>1970</option>
          <option>1969</option>
          <option>1968</option>
          <option>1967</option>
          <option>1965</option>
          <option>1964</option>
          <option>1963</option>
          <option>1962</option>
          <option>1961</option>
          <option>1960</option>
        </select>  </td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Business Name</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
        <input name="business" type="text" size="30" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Title</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
        <input name="title" type="text" size="30" /></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Skills</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
      <textarea name="skills" id="textarea" cols="80" rows="0"></textarea></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
    <td colspan="3"><div id="headerText"><small>Interests</small></div></td>
  </tr>
  <tr>
    <td colspan="3">
        <textarea name="interests" cols="80"></textarea></td>
  </tr>
  <tr>
    <td colspan="3"><hr color="#CCCCCC" size="1px"/></td>
  </tr>
  <tr>
  <td colspan="3" align="right"><div id="subHeaderText"><small>Upon finishing the form, please...</small>
    <input name="submit" type="submit" value="Submit" /></div></td>
  </tr>
</table>
</form>
Last edited by Benjamin on Wed Nov 09, 2011 2:55 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
ReubenDog
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2011 4:02 pm

Re: PHP image upload and rename script not working

Post by ReubenDog »

I just noticed a couple of form closing tags that might be out of place or unneeded. Please let me know what you think.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP image upload and rename script not working

Post by social_experiment »

Code: Select all

<input type="hidden" value="102400" name="MAX_FILE_SIZE" />
Add this line to your form; it creates a hidden input value that is the (maximum) size of the file you want to accept from users. It's not 100% foolproof but it does help a bit.

Here's a basic idea of idea of how a file upload should work

1. Check if $_FILES['fileUpload']['name'] contains anything, if not no file was selected for upload
2. If the name is set, use is_uploaded_file() to see that the file you are working with has actually been uploaded via the form
3. Use move_uploaded_file() to move the file to your specified folder
4. $_FILES['fileUpload']['error'] retuns a value which you can check to see if the file was correctly uploaded

Use these steps as a guideline and you should get the script working. Once you have another copy, paste it back for more assistance :)

Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ReubenDog
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2011 4:02 pm

Re: PHP image upload and rename script not working

Post by ReubenDog »

Thanks a lot for all your help, I'll give it a shot in the morning. I'll keep you posted, thanks again.
Post Reply