Current location: Hot Scripts Forums » Programming Languages » PHP » Image save driving me nuts!


Image save driving me nuts!

Reply
  #1 (permalink)  
Old 05-08-09, 06:47 AM
herghost herghost is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Image save driving me nuts!

Hi all,

hope you can help me with this:

PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<?php
session_start
();
include(
'include/database.php');
include(
'include/header.php');
include(
'include/auth.php');



$userid $_SESSION['SESS_USERID'];
$albumno $_SESSION['albumno'];

?>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />


<title>Welcome to fanjunky.com! - New Music - Bands- MP3 - Tickets - Tour Dates - The Latest Band = 

<?php $sql "SELECT * FROM user WHERE userid = '$userid'";
$result mysql_query($sql) or die(mysql_error());
while(
$row mysql_fetch_array($result))
{
   {
        echo 
$row['bandname'];
    }

   
?>
</title>

<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="page">
   <div id="content">
      <div class="post greenbox">
         <div class="title">
            <h1>band discography - Album Cover</h1>
         </div>
   <div class="entry">
    <p>Please upload the album cover</p>
    <p>
   
   

<?php
$query 
"SELECT `albumname`, `userid`, `albumno` FROM `banddisco` WHERE userid='$userid'";
$result mysql_query($query);

echo 
"<select name='albumname'>";
for(
$i=0$i mysql_num_rows($result); $i++) {

    
$albumname mysql_fetch_object($result);
    echo 
"<option value='"$albumname->userid ."'>"$albumname->albumname ."'>"$albumname->albumno ."</option>";

}
echo 
"</select>";  
 




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

//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, otherwize 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;
}


$newname="users/$userid/" .$albumname->albumno;
//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)
{
header("location: ../member_home.php?cover=1 ");
      exit();
}

?>   
   
    <p>Upload Cover </p>
    <form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>

   
</p></div>
         <div class="btm">
            <div class="l">
               <div class="r">
                  <p class="meta">&nbsp;</p>
               </div>
            </div>
         </div>
      </div>
        <br/>
        <div class="two-columns">
        <div class="columnA">
            <div class="title red">
               <h2>sponsers</h2>
            </div>
            <div class="content">
               
<script type="text/javascript">
google_ad_client = "pub-1087428711969548";

google_ad_slot = "3081294343";
google_ad_width = 250;
google_ad_height = 250;

</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

               
            </div>
        </div>
         <div class="columnB">
            <div class="title blue">
               <h2>Random Bands</h2>
            </div>
            <div class="content">
<?php 

$sql 
"SELECT * FROM user ORDER BY RAND() LIMIT 10";
$result mysql_query($sql ,$con);
while(
$myrow mysql_fetch_array($result))
{
echo 
"<table width ='90%'>";
echo 
"<tr>";
echo 
"<td>";
echo  
$myrow['bandname'];
echo 
"</td>";
echo 
"<td>";
echo  
"<a href='newprofile.php?userid=";
echo 
$myrow['userid'];
echo 
"'>View</a>";
echo 
"</td>";
echo 
"</tr>";
echo 
"</table>";
}
?>
                </div>
         </div>
         <div class="btm">&nbsp;</div>
      </div>
  </div>
   <!-- end content -->
   <!-- start sidebar -->
<div id="sidebar">
      <ul>
         <li>
         <h2>Sponsers</h2>
            <ul>
               <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
            </ul>
         
          
   
<h2>Sponsers</h2>
            
                <ul>
            <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
</ul>
      </li>
        </ul>   
</div>

   <!-- end sidebar -->
   <!-- end sidebar -->
   <!-- end sidebar -->
</div>
<!-- end page -->
<div style="clear: both;">&nbsp;</div>
<?php

include('include/footer.php')
?>



</body>
</html>
basically its meant to pull the last entry into a database from a band uploading album details and then name and save a file of the albumno (auto_increment).

it basically works for the 1st band. It will for example, create a file called 31 with the correct image. If however the same band uploads another album, and the correct album is displayed in the drop down box, then when you save the file it just updates 31 instead of creating 32.

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 05-08-09, 10:03 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You have a lot to learn my friend.
You are having big problems with your logic.
You need to understand the logic flow before you are going to have any success.
And I think you were told before, your session won't work because you are starting HTML output before you start the session.
session_start() must be called before any HTML output is started.
Also you can not use the header() command after any HTML output has started.

The above commands must be run before the doctype is declared and before any HTML is output.


I am assuming you included the dropdown box so you can select an album to upload a cover for.
I have made the dropdown box useful.
You select an album from the dropdown box and it will assign the album number to the image file.
PHP Code:

<?php
session_start
();
include(
'include/database.php');
include(
'include/auth.php');
$userid $_SESSION['SESS_USERID'];
$albumno $_SESSION['albumno'];
$query "SELECT `albumname`, `userid`, `albumno` FROM `banddisco` WHERE userid='$userid'";
$result mysql_query($query);
$select "<select name='albumname' onchange='get_albumno(this.value);'>";
$select .= "<option value='0>0'>Select an album ...</option>";
for(
$i=0$i mysql_num_rows($result); $i++)
{
 
$albumname mysql_fetch_object($result);
 
$select .= "<option value='".$albumname->albumname.">".$albumname->albumno."'>"$albumname->albumname ." > "$albumname->albumno ."</option>";
 }
$select .= "</select>";
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1024");
//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, otherwize 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;
    }
   
$newname=$_POST["temp_albumno"]?$_POST["temp_albumno"]:"temp_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(!
$errors)
   {
    
header("location: ../member_home.php?cover=1");
    exit();
    }
   }
  }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Welcome to fanjunky.com! - New Music - Bands- MP3 - Tickets - Tour Dates - The Latest Band =
<?php $sql "SELECT * FROM user WHERE userid = '$userid'";
$result mysql_query($sql) or die(mysql_error());
while(
$row mysql_fetch_array($result))
{
 echo 
$row['bandname'];
 }
   
?>
</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script>
function get_albumno(v)
{
 var v = v.split(">");
 if(v[0] == 0)
 {
  document.getElementById("cover_msg").style.color = "#f00";
  document.getElementById("cover_msg").innerHTML = "You must select and album first."
  }
 else
 {
  document.getElementById("cover_msg").style.color = "#00f";
  document.getElementById("cover_msg").innerHTML = "Album <b>'" + v[0] + "'</b> selected.";
  document.getElementById("temp_albumno").value = v[1];}
 }
</script>
</head>
<body>
<?php
include('include/header.php');
?>
<div id="page">
   <div id="content">
      <div class="post greenbox">
         <div class="title">
            <h1>band discography - Album Cover</h1>
         </div>
   <div class="entry">
    <p>Please upload the album cover</p>
    <p id="cover_msg" style="color:#f00;font-size:14px;font-style:italic;">Select an album from the dropdown box.</p>
<?php
echo $select;
?>
    <p>Upload Cover</p>
    <form name="newad" method="post" enctype="multipart/form-data" action="">
    <input id="temp_albumno" name="temp_albumno" type="hidden">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</div>
         <div class="btm">
            <div class="l">
               <div class="r">
                  <p class="meta">&nbsp;</p>
               </div>
            </div>
         </div>
      </div>
        <br />
        <div class="two-columns">
        <div class="columnA">
            <div class="title red">
               <h2>sponsers</h2>
            </div>
            <div class="content">
<script type="text/javascript">
google_ad_client = "pub-1087428711969548";

google_ad_slot = "3081294343";
google_ad_width = 250;
google_ad_height = 250;
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
            </div>
        </div>
         <div class="columnB">
            <div class="title blue">
               <h2>Random Bands</h2>
            </div>
            <div class="content">
<?php

$sql 
"SELECT * FROM user ORDER BY RAND() LIMIT 10";
$result mysql_query($sql);
while(
$myrow mysql_fetch_array($result))
{
 echo 
"<table width ='90%'>
        <tr>
         <td width='20%'>"
.$myrow['bandname']."</td>
         <td><a href='newprofile.php?userid="
.$myrow['userid']."'>View</a></td>
        </tr>
       </table>"
;
 }
?>
                </div>
         </div>
         <div class="btm">&nbsp;</div>
      </div>
  </div>
   <!-- end content -->
   <!-- start sidebar -->
<div id="sidebar">
      <ul>
         <li>
         <h2>Sponsers</h2>
            <ul>
               <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
            </ul>
<h2>Sponsers</h2>
                <ul>
            <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
</ul>
      </li>
        </ul>
</div>
   <!-- end sidebar -->
   <!-- end sidebar -->
   <!-- end sidebar -->
</div>
<!-- end page -->
<div style="clear: both;">&nbsp;</div>
<?php
include('include/footer.php')
?>
</body>
</html>
When you look at the code you will see that all the code, starting with the doctype to the end, can be considered the transmitter.
And all the code before the doctype can be considered the receiver.
And of course some of the code is used to setup certain variables that can be used throughout the program.


Note: I did not check all your code to make sure your html is correct. And I am not sure you are getting the output you may expect for the <title> element.
__________________
Jerry Broughton

Last edited by job0107; 05-08-09 at 10:16 AM.
Reply With Quote
  #3 (permalink)  
Old 05-08-09, 10:22 AM
herghost herghost is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!

learning is what I am trying to do, and I reliased that session start had to be first, just wasn't concerntrating on that at the time, but thanks for the reminder!

The only thing I have now is that the image stores to the wrong location, it should store to:
users/$userid/ how would I adapt the code to achieve this? I have not used the
Code:
$_POST["temp_albumno"]?$_POST["temp_albumno"]:"temp_name";
before, can I just add my required destination to the line?
Reply With Quote
  #4 (permalink)  
Old 05-08-09, 10:42 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I am sorry, I forgot to change that back after I was done testing the code.

Sense I activated the dropdown box, I don't see where $_SESSION['albumno'] is of any more use.
If there is a reason why you need this variable, then maybe you can explain why it is there.

Also if $_SESSION['albumno'] should contain a value from a previous page and you always get the same value, then maybe you aren't sending the correct value.

Anyways I don't see where it is being used anymore.

If you want to reincorporate it, then tell me how it would be used.
PHP Code:

<?php
session_start
();
include(
'include/database.php');
include(
'include/auth.php');
$userid $_SESSION['SESS_USERID'];
$albumno $_SESSION['albumno'];
$query "SELECT `albumname`, `userid`, `albumno` FROM `banddisco` WHERE userid='$userid'";
$result mysql_query($query);
$select "<select name='albumname' onchange='get_albumno(this.value);'>";
$select .= "<option value='0>0'>Select an album ...</option>";
for(
$i=0$i mysql_num_rows($result); $i++)
{
 
$albumname mysql_fetch_object($result);
 
$select .= "<option value='".$albumname->albumname.">".$albumname->albumno."'>"$albumname->albumname ." > "$albumname->albumno ."</option>";
 }
$select .= "</select>";
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1024");
//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, otherwize 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;
    }
    if(!empty(
$_POST["temp_albumno"])){$newname="users/$userid/".$_POST["temp_albumno"];}
   
//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(!
$errors)
   {
    
header("location: ../member_home.php?cover=1");
    exit();
    }
   }
  }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Welcome to fanjunky.com! - New Music - Bands- MP3 - Tickets - Tour Dates - The Latest Band =
<?php $sql "SELECT * FROM user WHERE userid = '$userid'";
$result mysql_query($sql) or die(mysql_error());
while(
$row mysql_fetch_array($result))
{
 echo 
$row['bandname'];
 }
   
?>
</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script>
function get_albumno(v)
{
 var v = v.split(">");
 if(v[0] == 0)
 {
  document.getElementById("cover_msg").style.color = "#f00";
  document.getElementById("cover_msg").innerHTML = "You must select and album first."
  }
 else
 {
  document.getElementById("cover_msg").style.color = "#00f";
  document.getElementById("cover_msg").innerHTML = "Album <b>'" + v[0] + "'</b> selected.";
  document.getElementById("temp_albumno").value = v[1];}
 }
</script>
</head>
<body>
<?php
include('include/header.php');
?>
<div id="page">
   <div id="content">
      <div class="post greenbox">
         <div class="title">
            <h1>band discography - Album Cover</h1>
         </div>
   <div class="entry">
    <p>Please upload the album cover</p>
    <p id="cover_msg" style="color:#f00;font-size:14px;font-style:italic;">Select an album from the dropdown box.</p>
<?php
echo $select;
?>
    <p>Upload Cover</p>
    <form name="newad" method="post" enctype="multipart/form-data" action="">
    <input id="temp_albumno" name="temp_albumno" type="hidden">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</div>
         <div class="btm">
            <div class="l">
               <div class="r">
                  <p class="meta">&nbsp;</p>
               </div>
            </div>
         </div>
      </div>
        <br />
        <div class="two-columns">
        <div class="columnA">
            <div class="title red">
               <h2>sponsers</h2>
            </div>
            <div class="content">
<script type="text/javascript">
google_ad_client = "pub-1087428711969548";

google_ad_slot = "3081294343";
google_ad_width = 250;
google_ad_height = 250;
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
            </div>
        </div>
         <div class="columnB">
            <div class="title blue">
               <h2>Random Bands</h2>
            </div>
            <div class="content">
<?php

$sql 
"SELECT * FROM user ORDER BY RAND() LIMIT 10";
$result mysql_query($sql);
while(
$myrow mysql_fetch_array($result))
{
 echo 
"<table width ='90%'>
        <tr>
         <td width='20%'>"
.$myrow['bandname']."</td>
         <td><a href='newprofile.php?userid="
.$myrow['userid']."'>View</a></td>
        </tr>
       </table>"
;
 }
?>
                </div>
         </div>
         <div class="btm">&nbsp;</div>
      </div>
  </div>
   <!-- end content -->
   <!-- start sidebar -->
<div id="sidebar">
      <ul>
         <li>
         <h2>Sponsers</h2>
            <ul>
               <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
            </ul>
<h2>Sponsers</h2>
                <ul>
            <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
</ul>
      </li>
        </ul>
</div>
   <!-- end sidebar -->
   <!-- end sidebar -->
   <!-- end sidebar -->
</div>
<!-- end page -->
<div style="clear: both;">&nbsp;</div>
<?php
include('include/footer.php')
?>
</body>
</html>
PHP Code:

$newname$_POST["temp_albumno"]?$_POST["temp_albumno"]:"temp_name"
The above line of code is simply Boolean logic.
What it means is:
if $_POST["temp_albumno"] contains a value, then assign it to $newname.
Else assign "temp_name" as the value for $newname.

But as you can see, I have changed that part of the code back to the way you had it.
I only wrote it that way for testing purposes.
I didn't want $newname to end up without a value while testing.
__________________
Jerry Broughton

Last edited by job0107; 05-08-09 at 10:52 AM.
Reply With Quote
  #5 (permalink)  
Old 05-08-09, 11:04 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by herghost View Post
Thanks!

learning is what I am trying to do, and I reliased that session start had to be first, just wasn't concerntrating on that at the time, but thanks for the reminder!

The only thing I have now is that the image stores to the wrong location, it should store to:
users/$userid/ how would I adapt the code to achieve this? I have not used the
Code:
$_POST["temp_albumno"]?$_POST["temp_albumno"]:"temp_name";
before, can I just add my required destination to the line?
The main problem I see is that you are having a hard time understanding which part of the code is used as the transmitter and which part is the receiver.
And which part goes first.

Mostly it depends on what you are trying to accomplish.
The transmitter and receiver don't always have to go in this order as long as the receiver understands that something has been transmitted.

I hope this make sense to you.
__________________
Jerry Broughton
Reply With Quote
  #6 (permalink)  
Old 05-08-09, 11:36 AM
herghost herghost is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
no that session is no longer used, so i took it out. It was basically where I was playimng around trying to get it tio work.

Ok, it again works fine for the 1st album, however the second saves as undefined.

the 1st album appear in the drop down as
Code:
goddamit>35
and the second as
Code:
>36'>maybe ill catch fire>36
I am kinda stubling through this, learning as I go, its a great feeling when you get something to work on your own for the 1st time, only to find out that it works in a way its not supposed to and you dont know why

This is the 1st time I have attempted to design a whole site from scratch, and while quite basic, its giving me a lot of pleasure seeing it come together.

Thanks for your help so far!
Reply With Quote
  #7 (permalink)  
Old 05-08-09, 12:42 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by herghost View Post
no that session is no longer used, so i took it out. It was basically where I was playimng around trying to get it tio work.

Ok, it again works fine for the 1st album, however the second saves as undefined.

the 1st album appear in the drop down as
Code:
goddamit>35
and the second as
Code:
>36'>maybe ill catch fire>36
I am kinda stubling through this, learning as I go, its a great feeling when you get something to work on your own for the 1st time, only to find out that it works in a way its not supposed to and you dont know why

This is the 1st time I have attempted to design a whole site from scratch, and while quite basic, its giving me a lot of pleasure seeing it come together.

Thanks for your help so far!
Ok, I think I found the problem.
I was using the ">" for the delimiter in the <option> value.
And that was causing confusion to the HTML interpreter.
I changed the delimiter to a "&" and that seems to work better.
PHP Code:

<?php
session_start
();
include(
'include/database.php');
include(
'include/auth.php');
$userid $_SESSION['SESS_USERID'];
$query "SELECT `albumname`, `userid`, `albumno` FROM `banddisco` WHERE userid='$userid'";
$result mysql_query($query);
$select "<select name='albumname' onchange='get_albumno(this.value);'>";
$select .= "<option value='0&0'>Select an album ...</option>";
for(
$i=0$i mysql_num_rows($result); $i++)
{
 
$albumname mysql_fetch_object($result);
 
$select .= "<option value='".$albumname->albumname."&".$albumname->albumno."'>"$albumname->albumname ." > "$albumname->albumno ."</option>";
 }
$select .= "</select>";
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1024");
//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, otherwize 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;
    }
    if(!empty(
$_POST["temp_albumno"])){$newname="users/$userid/".$_POST["temp_albumno"];}
   
//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(!
$errors)
   {
    
header("location: ../member_home.php?cover=1");
    exit();
    }
   }
  }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Welcome to fanjunky.com! - New Music - Bands- MP3 - Tickets - Tour Dates - The Latest Band =
<?php $sql "SELECT * FROM user WHERE userid = '$userid'";
$result mysql_query($sql) or die(mysql_error());
while(
$row mysql_fetch_array($result))
{
 echo 
$row['bandname'];
 }
   
?>
</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script>
function get_albumno(v)
{
 var v = v.split("&");
 if(v[0] == 0)
 {
  document.getElementById("cover_msg").style.color = "#f00";
  document.getElementById("cover_msg").innerHTML = "You must select and album first."
  }
 else
 {
  document.getElementById("cover_msg").style.color = "#00f";
  document.getElementById("cover_msg").innerHTML = "Album <b>'" + v[0] + "'</b> selected.";
  document.getElementById("temp_albumno").value = v[1];}
 }
</script>
</head>
<body>
<?php
include('include/header.php');
?>
<div id="page">
   <div id="content">
      <div class="post greenbox">
         <div class="title">
            <h1>band discography - Album Cover</h1>
         </div>
   <div class="entry">
    <p>Please upload the album cover</p>
    <p id="cover_msg" style="color:#f00;font-size:14px;font-style:italic;">Select an album from the dropdown box.</p>
<?php
echo $select;
?>
    <p>Upload Cover</p>
    <form name="newad" method="post" enctype="multipart/form-data" action="">
    <input id="temp_albumno" name="temp_albumno" type="hidden">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</div>
         <div class="btm">
            <div class="l">
               <div class="r">
                  <p class="meta">&nbsp;</p>
               </div>
            </div>
         </div>
      </div>
        <br />
        <div class="two-columns">
        <div class="columnA">
            <div class="title red">
               <h2>sponsers</h2>
            </div>
            <div class="content">
<script type="text/javascript">
google_ad_client = "pub-1087428711969548";

google_ad_slot = "3081294343";
google_ad_width = 250;
google_ad_height = 250;
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
            </div>
        </div>
         <div class="columnB">
            <div class="title blue">
               <h2>Random Bands</h2>
            </div>
            <div class="content">
<?php

$sql 
"SELECT * FROM user ORDER BY RAND() LIMIT 10";
$result mysql_query($sql);
while(
$myrow mysql_fetch_array($result))
{
 echo 
"<table width ='90%'>
        <tr>
         <td width='20%'>"
.$myrow['bandname']."</td>
         <td><a href='newprofile.php?userid="
.$myrow['userid']."'>View</a></td>
        </tr>
       </table>"
;
 }
?>
                </div>
         </div>
         <div class="btm">&nbsp;</div>
      </div>
  </div>
   <!-- end content -->
   <!-- start sidebar -->
<div id="sidebar">
      <ul>
         <li>
         <h2>Sponsers</h2>
            <ul>
               <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
            </ul>
<h2>Sponsers</h2>
                <ul>
            <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
</ul>
      </li>
        </ul>
</div>
   <!-- end sidebar -->
   <!-- end sidebar -->
   <!-- end sidebar -->
</div>
<!-- end page -->
<div style="clear: both;">&nbsp;</div>
<?php
include('include/footer.php')
?>
</body>
</html>
One more thing, don't use any apostrophes in the album names.
__________________
Jerry Broughton

Last edited by job0107; 05-08-09 at 12:59 PM.
Reply With Quote
  #8 (permalink)  
Old 05-08-09, 01:31 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Ok this is the last upgrade.

This version doesn't care if you use apostrophes in the album names.
PHP Code:

<?php
session_start
();
include(
'include/database.php');
include(
'include/auth.php');
$userid $_SESSION['SESS_USERID'];
$query "SELECT `albumname`, `userid`, `albumno` FROM `banddisco` WHERE userid='$userid'";
$result mysql_query($query);
$select "<select name='albumname' onchange='get_albumno(this.value);'>";
$select .= "<option value='0&0'>Select an album ...</option>";
for(
$i=0$i mysql_num_rows($result); $i++)
{
 
$albumname mysql_fetch_object($result);
 
$select .= "<option value='".str_replace("'","-",$albumname->albumname)."&".$albumname->albumno."'>"$albumname->albumname ." - "$albumname->albumno ."</option>";
 }
$select .= "</select>";
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1024");
//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, otherwize 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;
    }
    if(!empty(
$_POST["temp_albumno"])){$newname="users/$userid/".$_POST["temp_albumno"];}
   
//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(!
$errors)
   {
    
header("location: ../member_home.php?cover=1");
    exit();
    }
   }
  }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Welcome to fanjunky.com! - New Music - Bands- MP3 - Tickets - Tour Dates - The Latest Band =
<?php $sql "SELECT * FROM user WHERE userid = '$userid'";
$result mysql_query($sql) or die(mysql_error());
while(
$row mysql_fetch_array($result))
{
 echo 
$row['bandname'];
 }
   
?>
</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script>
function get_albumno(v)
{
 var v = v.split("&");
 if(v[0] == 0)
 {
  document.getElementById("cover_msg").style.color = "#f00";
  document.getElementById("cover_msg").innerHTML = "You must select and album first."
  }
 else
 {
  v[0]=v[0].split("-").join("'");
  document.getElementById("cover_msg").style.color = "#00f";
  document.getElementById("cover_msg").innerHTML = "Album <b>'" + v[0] + "'</b> selected.";
  document.getElementById("temp_albumno").value = v[1];}
 }
</script>
</head>
<body>
<?php
include('include/header.php');
?>
<div id="page">
   <div id="content">
      <div class="post greenbox">
         <div class="title">
            <h1>band discography - Album Cover</h1>
         </div>
   <div class="entry">
    <p>Please upload the album cover</p>
    <p id="cover_msg" style="color:#f00;font-size:14px;font-style:italic;">Select an album from the dropdown box.</p>
<?php
echo $select;
?>
    <p>Upload Cover</p>
    <form name="newad" method="post" enctype="multipart/form-data" action="">
    <input id="temp_albumno" name="temp_albumno" type="hidden">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</div>
         <div class="btm">
            <div class="l">
               <div class="r">
                  <p class="meta">&nbsp;</p>
               </div>
            </div>
         </div>
      </div>
        <br />
        <div class="two-columns">
        <div class="columnA">
            <div class="title red">
               <h2>sponsers</h2>
            </div>
            <div class="content">
<script type="text/javascript">
google_ad_client = "pub-1087428711969548";

google_ad_slot = "3081294343";
google_ad_width = 250;
google_ad_height = 250;
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
            </div>
        </div>
         <div class="columnB">
            <div class="title blue">
               <h2>Random Bands</h2>
            </div>
            <div class="content">
<?php

$sql 
"SELECT * FROM user ORDER BY RAND() LIMIT 10";
$result mysql_query($sql);
while(
$myrow mysql_fetch_array($result))
{
 echo 
"<table width ='90%'>
        <tr>
         <td width='20%'>"
.$myrow['bandname']."</td>
         <td><a href='newprofile.php?userid="
.$myrow['userid']."'>View</a></td>
        </tr>
       </table>"
;
 }
?>
                </div>
         </div>
         <div class="btm">&nbsp;</div>
      </div>
  </div>
   <!-- end content -->
   <!-- start sidebar -->
<div id="sidebar">
      <ul>
         <li>
         <h2>Sponsers</h2>
            <ul>
               <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
            </ul>
<h2>Sponsers</h2>
                <ul>
            <li><script type="text/javascript"><!--
google_ad_client = "pub-1087428711969548";
/* 250x250, created 4/14/09 */
google_ad_slot = "6665679490";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></li>
</ul>
      </li>
        </ul>
</div>
   <!-- end sidebar -->
   <!-- end sidebar -->
   <!-- end sidebar -->
</div>
<!-- end page -->
<div style="clear: both;">&nbsp;</div>
<?php
include('include/footer.php')
?>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
  #9 (permalink)  
Old 05-08-09, 02:15 PM
herghost herghost is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
hmm, he file is still being saved as name 'undefined'??

EDIT - I am a dumbass, there is an apostrophe in it !

SECOND EDIT - Actually its still saving as 'undefined'?? even without the apostrophe

3RD EDIT - Didnt see the above I will try that!

Final edit - works fantastically many many thanks for taking the time to help me with this!

Last edited by herghost; 05-08-09 at 02:24 PM.
Reply With Quote
  #10 (permalink)  
Old 05-08-09, 02:31 PM
herghost herghost is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, thought it was all working, but after 5 albums it starts using the album name instead of teh albumno?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
div css theighost CSS 11 09-14-08 02:30 AM
displaying text over image ketanco CSS 10 09-11-08 07:39 PM
Save file after image upload and resize acctman PHP 3 08-17-08 10:14 AM
simple css driving me nuts bitstomper CSS 3 05-25-07 10:28 PM
I need to save my php image, bandwidth rip iwonder PHP 1 11-15-05 08:42 AM


All times are GMT -5. The time now is 08:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.