Author Topic: Image Upload HELP  (Read 717 times)

0 Members and 1 Guest are viewing this topic.

Offline matvespaTopic starter

  • Enthusiast
  • Posts: 68
    • View Profile
Image Upload HELP
« on: September 05, 2010, 08:20:26 AM »
Hi guys...i found this code online. When i test it raw, it works! But when i try to implement it onto my code. It didnt work. Any clue? Image Upload File is as follow:
My Form:

<form id="AddNewPrograms" name="AddNewPrograms" action="admin_programsAddNewProcess.php" method="post" enctype="multipart/form-data">
        <
table width="707" border="0" cellpadding="10px" cellspacing="0px">
          <
tr>
            <
td id="formField" height="29">Title</td>
            <
td colspan="2" id="footNote"><input class="validate[required,length[0,21]] text-input" id="Title" type="text" name="Title" maxlength="21"/>
            &
nbsp;&nbsp;&nbsp;(Maximum 21 Characters)</td>
            </
tr>
          <
tr>
            <
td id="formField" height="14">Date</td>
            <
td colspan="2">
            <
select name="Day" id="Day">
              <
option value="01">01</option>
              <
option value="02">02</option>
              <
option value="03">03</option>
              <
option value="04">04</option>
              <
option value="05">05</option>
              <
option value="06">06</option>
              <
option value="07">07</option>
              <
option value="08">08</option>
              <
option value="09">09</option>
              <
option value="10">10</option>
              <
option value="11">11</option>
              <
option value="12">12</option>
              <
option value="13">13</option>
              <
option value="14">14</option>
              <
option value="15">15</option>
              <
option value="16">16</option>
              <
option value="17">17</option>
              <
option value="18">18</option>
              <
option value="19">19</option>
              <
option value="20">20</option>
              <
option value="21">21</option>
              <
option value="22">22</option>
              <
option value="23">23</option>
              <
option value="24">24</option>
              <
option value="25">25</option>
              <
option value="26">26</option>
              <
option value="27">27</option>
              <
option value="28">28</option>
              <
option value="29">29</option>
              <
option value="30">30</option>
              <
option value="31">31</option>
            </
select>
            &
nbsp;
            <
select name="Month" id="Month">
              <
option value="01">January</option>
              <
option value="02">February</option>
              <
option value="03">March</option>
              <
option value="04">April</option>
              <
option value="05">May</option>
              <
option value="06">June</option>
              <
option value="07">July</option>
              <
option value="08">August</option>
              <
option value="09">September</option>
              <
option value="10">October</option>
              <
option value="11">November</option>
              <
option value="12">December</option>
            </
select>
            &
nbsp;
            <?
php
	
	
	
	
$currentYear date("Y");
	
	
	
	
$nextYear $currentYear 1;
	
	
	
	
$nextTwoYears $currentYear 2;
	

	
	
	
?>
            <select name="Year" id="Year">
              <option value="<?php echo $currentYear?>"><?php echo $currentYear?></option>
              <option value="<?php echo $nextYear?>"><?php echo $nextYear?></option>
              <option value="<?php echo $nextTwoYears?>"><?php echo $nextTwoYears?></option>
            </select>
            </td>
            </tr>
          <tr>
            <td valign="top" id="formField" height="15">Time</td>
            <td colspan="2">
            <select name="StartHour" id="StartHour">
              <option>12</option>
              <option>13</option>
              <option>14</option>
              <option>15</option>
              <option>16</option>
              <option>17</option>
              <option>18</option>
              <option>19</option>
              <option>20</option>
              <option>21</option>
              <option>22</option>
              <option>23</option>
              <option>00</option>
              <option>01</option>
              <option>02</option>
              <option>03</option>
              <option>04</option>
              <option>05</option>
              <option>06</option>
              <option>07</option>
              <option>08</option>
              <option>09</option>
              <option>10</option>
              <option>11</option>
              </select>
           
	
	
	
:
              <select name="StartMinutes" id="StartMinutes">
              <option>00</option>
              <option>05</option>
              <option>10</option>
              <option>15</option>
              <option>20</option>
              <option>25</option>
              <option>30</option>
              <option>35</option>
              <option>40</option>
              <option>45</option>
              <option>50</option>
              <option>55</option>
              </select>
              &nbsp;&nbsp;&nbsp;to&nbsp;&nbsp;&nbsp;
              <select name="EndHour" id="EndHour">
              <option>12</option>
              <option>13</option>
              <option>14</option>
              <option>15</option>
              <option>16</option>
              <option>17</option>
              <option>18</option>
              <option>19</option>
              <option>20</option>
              <option>21</option>
              <option>22</option>
              <option>23</option>
              <option>00</option>
              <option>01</option>
              <option>02</option>
              <option>03</option>
              <option>04</option>
              <option>05</option>
              <option>06</option>
              <option>07</option>
              <option>08</option>
              <option>09</option>
              <option>10</option>
              <option>11</option>
              </select>
           
	
	
	
:
              <select name="EndMinutes" id="EndMinutes">
              <option>00</option>
              <option>05</option>
              <option>10</option>
              <option>15</option>
              <option>20</option>
              <option>25</option>
              <option>30</option>
              <option>35</option>
              <option>40</option>
              <option>45</option>
              <option>50</option>
              <option>55</option>
              </select>
            </td>
          </tr>
          <tr>
            <td valign="top" id="formField" height="29">Venue</td>
            <td colspan="2" id="footNote">
            <input class="validate[required,length[0,100]] text-input" id="Venue" type="text" name="Venue"/>&nbsp;&nbsp;&nbsp;(Level & Room No.)<br /><br />
            <input class="validate[required,length[0,100]] text-input" id="VenueBuilding" type="text" name="VenueBuilding"/>&nbsp;&nbsp;&nbsp;(Building Name)<br /><br />
            <input class="validate[required,length[0,100]] text-input" id="VenueCountry" type="text" name="VenueCountry"/>&nbsp;&nbsp;&nbsp;(Country)
            </td>
            </tr>
          <tr>
            <td valign="top" id="formField" height="29">Description</td>
            <td width="385" valign="top"> 
            <textarea class="validate[required] text-input" name="Description" id="Description" type="text" cols="46" rows="7"></textarea>
           
            </td>
            <td width="174" valign="top" id="footNote">(HTML Allowed)</td>
          </tr>
          <tr>
            <td valign="top" id="formField" height="29">Speaker</td>
            <td colspan="2" id="footNote">
            <input class="validate[required,length[0,100]] text-input" id="Speaker" type="text" name="Speaker"/>&nbsp;&nbsp;&nbsp;(Speaker's Name)<br /><br />
            <input class="validate[required,length[0,100]] text-input" id="SpeakerCompany" type="text" name="SpeakerCompany"/>&nbsp;&nbsp;&nbsp;<font color="#666666">(Speaker's Position & Company)</font>
            </td>
          </tr>
          <tr>
            <td id="formField" height="29"><font color="#666666">Price</font></td>
            <td colspan="2"><font color="#666666">SGD</font>&nbsp;<input class="validate[custom[onlyNumber] text-input" id="Price" type="text" name="Price"/></td>
          </tr>
          <tr>
            <td id="formField" height="29"><font color="#666666">Image</font></td>
            <td id="footNote" colspan="2"><input type="file" name="Image" id="Image" />&nbsp;&nbsp;&nbsp;<font color="#666666">(Recommended Size: 305 by 245 pixels)</font></td>
          </tr>
          <tr>
            <td width="88" height="29">&nbsp;</td>
            <td colspan="2">
       <script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        $('#submit').hover(
            function(){ // Change the input image's source when we "roll on"
                $(this).attr({ src : 'images/web_buttons/AddNew_BtnMO.png'});
            },
            function(){ // Change the input image's source back to the default on "roll off"
                $(this).attr({ src : 'images/web_buttons/AddNew_Btn.png'});             }
        );
    });
</script>
              <input name="submit" type="image" id="submit" src="images/web_buttons/AddNew_Btn.png" />
            </td>
          </tr>
        </table>
        </form>


My Image Upload

	
include(
'dbConnection/dbConfig.php');
	
include(
'dbConnection/dbOpen.php');

	
//define a maxim size for the uploaded images in Kb
 
define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
 
function getExtension($str) {
         
$i strrpos($str,".");
         if (!
$i) { return ""; }
         
$l strlen($str) - $i;
         
$ext substr($str,$i+1,$l);
         return 
$ext;
 }

//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
 
$errors=0;
//checks if the form has been submitted
 
if(isset($_POST['Submit'])) 
 {
 
	
//reads the name of the file the user submitted for uploading
 
	
$image=$_FILES['Image']['name'];
 
	
//if it is not empty
 
	
if (
$image
 
	
{
 
	
//get the original name of the file from the clients machine
 
	
	
$filename stripslashes($_FILES['Image']['name']);
 
	
//get the extension of the file in a lower case format
  
	
	
$extension getExtension($filename);
 
	
	
$extension strtolower($extension);
 
	
//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
	
//otherwise we will do more tests
 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 
	
	
{
	
	
//print error message
 
	
	
	
echo 
'<h1>Unknown extension!</h1>';
 
	
	
	
$errors=1;
 
	
	
}
 
	
	
else
 
	
	
{
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server
 
$size=filesize($_FILES['Image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size MAX_SIZE*1024)
{
	
echo 
'<h1>You have exceeded the size limit!</h1>';
	
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied copy($_FILES['Image']['tmp_name'], $newname);
if (!
$copied
{
	
echo 
'<h1>Copy unsuccessfull!</h1>';
	
$errors=1;
}}}}
//If no errors registred, print the success message
 
if(isset($_POST['Submit']) && !$errors
 {
	
 if (empty(
$newname)) 
	

    
$newname 'images/programs/default.png'
	
}
	
	
$Title1 $_POST["Title"];
	
$Date $_POST["Year"]."-".$_POST["Month"]."-".$_POST["Day"];
	
$Time $_POST["StartHour"].":".$_POST["StartMinutes"]." - ".$_POST["EndHour"].":".$_POST["EndMinutes"];
	
$Venue1 $_POST["Venue"];
	
$VenueBuilding1 $_POST["VenueBuilding"];
	
$VenueCountry1 $_POST["VenueCountry"];
	
$Description1 $_POST["Description"];
	
$Speaker1 $_POST["Speaker"];
	
$SpeakerCompany1 $_POST["SpeakerCompany"];
	
$Price $_POST["Price"];
	

	
$Title mysql_real_escape_string($Title1);
	
$Venue mysql_real_escape_string($Venue1);
	
$VenueBuilding mysql_real_escape_string($VenueBuilding1);
	
$VenueCountry mysql_real_escape_string($VenueCountry1);
	
$Description mysql_real_escape_string($Description1);
	
$Speaker mysql_real_escape_string($Speaker1);
	
$SpeakerCompany mysql_real_escape_string($SpeakerCompany1);
$SQL "INSERT INTO programs (title, date, time, venue, venue_buildingName, venue_country, description, speakers, speakers_company, price, image) VALUES ('$Title','$Date','$Time','$Venue','$VenueBuilding','$VenueCountry','$Description','$Speaker','$SpeakerCompany',$Price,'$newname')";
	

if (!
mysql_query($SQL,$conn))
  {
  die(
'Error: ' mysql_error());
  }
header('Location: admin_programs.php');
 }
	

	
include(
'dbConnection/dbClose.php');


Offline Pikachu2000

  • I hate everything.
  • Global Moderator
  • Freak!
  • *
  • Posts: 9,062
  • Gender: Male
  • Is it solipsistic in here, or is it just me?
    • View Profile
Re: Image Upload HELP
« Reply #1 on: September 05, 2010, 11:22:46 AM »
Are you getting any errors? What is or isn't happening when you run it?
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf * || Linux Help --> linuxforum.com

Offline matvespaTopic starter

  • Enthusiast
  • Posts: 68
    • View Profile
Re: Image Upload HELP
« Reply #2 on: September 05, 2010, 11:30:22 AM »
I got a blank page. It should show UPLOAD SUCCESSFUL! but nothing appear. At the bottom of my browser shows "Done" and i assume no coding error. When i check my database, nothing is being update. There is no error at all.
When i try with a simple form, it works. When i try to implement onto my code, it doesnt.

The working code is here:

	
include(
'dbConnection/dbConfig.php');
	
include(
'dbConnection/dbOpen.php');
	

//define a maxim size for the uploaded images in Kb
 
define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
 
function getExtension($str) {
         
$i strrpos($str,".");
         if (!
$i) { return ""; }
         
$l strlen($str) - $i;
         
$ext substr($str,$i+1,$l);
         return 
$ext;
 }

//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
 
$errors=0;
//checks if the form has been submitted
 
if(isset($_POST['Submit'])) 
 {
 
	
//reads the name of the file the user submitted for uploading
 
	
$image=$_FILES['Image']['name'];
 
	
//if it is not empty
 
	
if (
$image
 
	
{
 
	
//get the original name of the file from the clients machine
 
	
	
$filename stripslashes($_FILES['Image']['name']);
 
	
//get the extension of the file in a lower case format
  
	
	
$extension getExtension($filename);
 
	
	
$extension strtolower($extension);
 
	
//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
	
//otherwise we will do more tests
 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 
	
	
{
	
	
//print error message
 
	
	
	
echo 
'<h1>Unknown extension!</h1>';
 
	
	
	
$errors=1;
 
	
	
}
 
	
	
else
 
	
	
{
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server
 
$size=filesize($_FILES['Image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size MAX_SIZE*1024)
{
	
echo 
'<h1>You have exceeded the size limit!</h1>';
	
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied copy($_FILES['Image']['tmp_name'], $newname);
if (!
$copied
{
	
echo 
'<h1>Copy unsuccessfull!</h1>';
	
$errors=1;
}}}}
//If no errors registred, print the success message
 
if(isset($_POST['Submit']) && !$errors
 {
	
 if (empty(
$newname)) 
	

    
$newname 'images/programs/default.png'
	
}
	
$Title $_POST["Title"];
 
	
	
$SQL "INSERT INTO test (image, title) VALUES ('$newname', '$Title')";
	

if (!
mysql_query($SQL,$conn))
  {
  die(
'Error: ' mysql_error());
  }
	
echo 
"UPLOAD SUCCESSFUL!";
 }
 
	
include(
'dbConnection/dbClose.php');


Offline markylove

  • Irregular
  • Posts: 4
    • View Profile
Re: Image Upload HELP
« Reply #3 on: September 07, 2010, 08:00:56 PM »
Is there any difference in your code and that code that works??

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Image Upload HELP
« Reply #4 on: September 11, 2010, 10:32:58 AM »
$_POST['Submit'] may not be the same as $_POST['submit']

I suggest that you just check for POST:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
       
// The server received a post request

deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores

Offline matvespaTopic starter

  • Enthusiast
  • Posts: 68
    • View Profile
Re: Image Upload HELP
« Reply #5 on: September 15, 2010, 09:31:40 AM »
Hi. I solved this problem. It is because i am using an image submit button. Something is blocking it. I tried using the default html submit button and the image upload works. Any idea on how i could make it an image button without having any error.

Just to inform you guys in advance that the html code below for image submit button if i were to use it will make my image upload not working.

<INPUT TYPE="image" SRC="images/submit.gif" BORDER="0" ALT="Submit Form">

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Image Upload HELP
« Reply #6 on: September 15, 2010, 09:48:35 AM »
Image submit buttons send the x,y coordinates where the image was clicked. The browsers that send the value="..." using the name="..." attribute are not actually following the w3.org specification.

You can either detect the name_x or name_y value (see this link http://www.php.net/manual/en/faq.html.php#faq.html.form-image) or you can put in a hidden field with a name/value that you want to use to detect if the form was submitted, or you could use the method BlueSkyIS posted.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline matvespaTopic starter

  • Enthusiast
  • Posts: 68
    • View Profile
Re: Image Upload HELP
« Reply #7 on: September 15, 2010, 09:54:59 AM »
How to apply them on the code? The explaination is too hard for me to understand. Any sample?

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Image Upload HELP
« Reply #8 on: September 15, 2010, 10:33:29 AM »
First method, use the name_x or name_y variable -

Code: [Select]
<input type='image' name="submit" src="your_image_here..." >
if(isset($_POST['submit_x'])){
	
// the form was submitted

}


Second method, use a hidden field -

Code: [Select]
<input type='hidden' name='submit' value='submit'>
<input type='image' src="your_image_here..." >

if(isset($_POST['submit'])){
	
// the form was submitted

}


Third method, detect any POST submission -

Code: [Select]
<input type='image' src="your_image_here..." >
if ($_SERVER['REQUEST_METHOD'] == "POST"){
	
// a form was submitted

}
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline matvespaTopic starter

  • Enthusiast
  • Posts: 68
    • View Profile
Re: Image Upload HELP
« Reply #9 on: September 15, 2010, 10:49:12 AM »
Hi Thanks it works! Just to be a little advance, how do i make the the image submit button rollover? is it the normal html method?