Jump to content

Image Upload HELP


matvespa

Recommended Posts

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"/>
               (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>
             
            <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>
             
            <?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>
                 to   
              <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"/>   (Level & Room No.)<br /><br />
            <input class="validate[required,length[0,100]] text-input" id="VenueBuilding" type="text" name="VenueBuilding"/>   (Building Name)<br /><br />
            <input class="validate[required,length[0,100]] text-input" id="VenueCountry" type="text" name="VenueCountry"/>   (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"/>   (Speaker's Name)<br /><br />
            <input class="validate[required,length[0,100]] text-input" id="SpeakerCompany" type="text" name="SpeakerCompany"/>   <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> <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" />   <font color="#666666">(Recommended Size: 305 by 245 pixels)</font></td>
          </tr>
          <tr>
            <td width="88" height="29"> </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');

 

Link to comment
https://forums.phpfreaks.com/topic/212588-image-upload-help/
Share on other sites

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');

 

Link to comment
https://forums.phpfreaks.com/topic/212588-image-upload-help/#findComment-1107495
Share on other sites

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">

Link to comment
https://forums.phpfreaks.com/topic/212588-image-upload-help/#findComment-1111341
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/212588-image-upload-help/#findComment-1111345
Share on other sites

First method, use the name_x or name_y variable -

 

<input type='image' name="submit" src="your_image_here..." >

 

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

}

 

Second method, use a hidden field -

 

<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 -

 

<input type='image' src="your_image_here..." >

 

if ($_SERVER['REQUEST_METHOD'] == "POST"){
// a form was submitted

}

Link to comment
https://forums.phpfreaks.com/topic/212588-image-upload-help/#findComment-1111374
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.