Page 1 of 1

photo upload

Posted: Mon Jan 31, 2011 5:13 pm
by timoteo
Hi, I am trying to create a page to upload a photo file to database. I am trying to adapt a script which I have seen in several tutorials on the web. http://www.techsupportforum.com/forums/ ... 76804.html
However I can not get beyond the first couple of lines.

Code: Select all

$name = $_FILES['picture']['photoname']; 
This gives me unexpected T_Variable. If I remove it I got expecting T_paamayim_nekudotayim in this line

Code: Select all

list($width, $height, $typeb, $attr) = getimagesize($tmp_name); 
From what I've learnt from php when everything goes wrong there is usually something quite simple overlooked in the code. Any ideas? This is the code straight from the tutorial above. The only diference is in my database - I am using InnoDB and my field names are photoname, phototype, photosize and photocontent - I have changed the INSERT INTO correspondingly. Apart from that all is as the tutorial. But mine doesn't work :( Any one have any ideas please?

Code: Select all

<?php 
// if something was posted, start the process... 
if(isset($_POST['upload'])) 
{ 

// define the posted file into variables 
$name = $_FILES['picture']['name']; 
$tmp_name = $_FILES['picture']['tmp_name']; 
$type = $_FILES['picture']['type']; 
$size = $_FILES['picture']['size']; 

// get the width & height of the file (we don't need the other stuff) 
list($width, $height, $typeb, $attr) = getimagesize($tmp_name); 
     
// if width is over 600 px or height is over 500 px, kill it     
if($width>600 || $height>500) 
{ 
echo $name . "'s dimensions exceed the 600x500 pixel limit."; 
echo ?> <a href="form.html">Click here</a> to try again. <?php  ; 
die(); 
} 

// if the mime type is anything other than what we specify below, kill it     
if(!( 
$type=='image/jpeg' || 
$type=='image/png' || 
$type=='image/gif' 
)) { 
echo $type .  " is not an acceptable format."; 
echo ?> <a href="form.html">Click here</a> to try again. <?php  ; 
die(); 
} 

// if the file size is larger than 350 KB, kill it 
if($size>'350000') { 
echo $name . " is over 350KB. Please make it smaller."; 
echo ?> <a href="form.html">Click here</a> to try again. <?php  ; 
die(); 
}  
// if your server has magic quotes turned off, add slashes manually 
if(!get_magic_quotes_gpc()){ 
$name = addslashes($name); 
} 

// open up the file and extract the data/content from it 
$extract = fopen($tmp_name, 'r'); 
$content = fread($extract, $size); 
$content = addslashes($content); 
fclose($extract);  
  
// connect to the database 
mysql_select_db($database_recommendingpeople, $recommendingpeople);
 
$userid = $_SESSION['userid'];
// the query that will add this to the database 
$addfile = "INSERT INTO businessdescription (photoname, photosize, phototype, photocontent ) ". 
           "VALUES ('$name', '$size', '$type', '$content') WHERE userid = '$userid' "; 

mysql_query($addfile) or die(mysql_error()); 

// get the last inserted ID if we're going to display this image next 


mysql_close();  
  
echo "Successfully uploaded your picture!"; 
  
// we still have to close the original IF statement. If there was nothing posted, kill the page. 
}else{die("No uploaded file present"); 
} 
 
 
header( "Location: form.html"); 

// we still have to close the original IF statement. If there was nothing posted, kill the page. 
}else{die("No uploaded file present"); 
} 
?>  
 
// display the image 

<div align="center"> 
    <strong><?php  echo $name; ?><br> 
    </strong><img name="<?php    echo $name; ?>" src="getpicture.php?fid=<?php echo $userid; ?>" alt="Unable to view image #<?php  echo $userid; ?>"> 
    <br> 

</div> 
<?php 
// we still have to close the original IF statement. If there was nothing posted, kill the page. 
}else{die("No uploaded file present"); 
} 
?>

Re: photo upload

Posted: Mon Jan 31, 2011 5:30 pm
by mikecampbell
Did you change your form so the encoding is multi-part/form-data?

Re: photo upload

Posted: Tue Feb 01, 2011 3:10 am
by timoteo
Yes, This is my form:

Code: Select all

  <p><form action="upload.php" method = "post" enctype="multipart/form-data" name="uploadform">
<input type="hidden" name="MAX_FILE_SIZE" value="350000">
<input name="picture" type="file" id="picture" size="50">
<input name="upload" type="submit" id="upload" value="Upload Picture!">
</form></p>

Re: photo upload

Posted: Tue Feb 01, 2011 10:46 am
by social_experiment
timoteo wrote:But mine doesn't work
You mention the error, is that the main issue?

Code: Select all

$name = $_FILES['picture']['photoname']; 
This should be like your other $name variable, instead of photoname, change it to 'name'.

Re: photo upload

Posted: Tue Feb 01, 2011 3:02 pm
by timoteo
I've made that change. (I had it like that before). I just don't understand what is not working here. This script came straight off a tutorial, so it should work no?
If I run it by itself on a fresh page then it calls unexpected if statement. If I run it in the context of my page then I get unexpected T_VARIABLE from

Code: Select all

$name = $_FILES['picture']['name']; 
Can anyone else make it work or see why it is not working. I came across about 3 or 4 'tutorials' with exactly the same script - I don't know who is copying who, but I would like to know why it doesn't work.

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
<?php 
$userid = '16'
// if something was posted, start the process... 
if(isset($_POST['upload'])) 
{ 

// define the posted file into variables 
$name = $_FILES['picture']['name']; 
$tmp_name = $_FILES['picture']['tmp_name']; 
$type = $_FILES['picture']['type']; 
$size = $_FILES['picture']['size']; 

// get the width & height of the file (we don't need the other stuff) 
list($width, $height, $typeb, $attr) = getimagesize($tmp_name); 
     
// if width is over 600 px or height is over 500 px, kill it     
if($width>600 || $height>500) 
{ 
echo $name . "'s dimensions exceed the 600x500 pixel limit."; 
echo ?> <a href="form.html">Click here</a> to try again. <?php  ; 
die(); 
} 

// if the mime type is anything other than what we specify below, kill it     
if(!( 
$type=='image/jpeg' || 
$type=='image/png' || 
$type=='image/gif' 
)) { 
echo $type .  " is not an acceptable format."; 
echo ?> <a href="form.html">Click here</a> to try again. <?php  ; 
die(); 
} 

// if the file size is larger than 350 KB, kill it 
if($size>'350000') { 
echo $name . " is over 350KB. Please make it smaller."; 
echo ?> <a href="form.html">Click here</a> to try again. <?php  ; 
die(); 
}  
// if your server has magic quotes turned off, add slashes manually 
if(!get_magic_quotes_gpc()){ 
$name = addslashes($name); 
} 

// open up the file and extract the data/content from it 
$extract = fopen($tmp_name, 'r'); 
$content = fread($extract, $size); 
$content = addslashes($content); 
fclose($extract);  
  
// connect to the database 
mysql_select_db($database_recommendingpeople, $recommendingpeople);
 
$userid = $_SESSION['userid'];
// the query that will add this to the database 
$addfile = "INSERT INTO businessdescription (photoname, photosize, phototype, photocontent ) ". 
           "VALUES ('$name', '$size', '$type', '$content') WHERE userid = '$userid' "; 

mysql_query($addfile) or die(mysql_error()); 

// get the last inserted ID if we're going to display this image next 


mysql_close();  
  
echo "Successfully uploaded your picture!"; 
  
// we still have to close the original IF statement. If there was nothing posted, kill the page. 
}else{die("No uploaded file present"); 
} 
 
 
header( "Location: form.html"); 

// we still have to close the original IF statement. If there was nothing posted, kill the page. 
}else{die("No uploaded file present"); 
} 
?>  
 
// display the image 

<div align="center"> 
    <strong><?php  echo $name; ?><br> 
    </strong><img name="<?php echo $name; ?>" src="getpicture.php?fid=<?php echo $userid; ?>" alt="Unable to view image #<?php echo $userid; ?>"> 
    <br> 

</div> 
<?php 
// we still have to close the original IF statement. If there was nothing posted, kill the page. 
}else{die("No uploaded file present"); 
} 
?>



<body>
    <p><form action="upload.php" method = "post" enctype="multipart/form-data" name="uploadform">
<input type="hidden" name="MAX_FILE_SIZE" value="350000">
<input name="picture" type="file" id="picture" size="50">
<input name="upload" type="submit" id="upload" value="Upload Picture!">
</form></p>
</body>
</html>

Re: photo upload

Posted: Tue Feb 01, 2011 4:24 pm
by social_experiment

Code: Select all

 $userid = '16' 
This line needs a semi-colon at the end of it.
timoteo wrote:This script came straight off a tutorial, so it should work no?
Never ever assume that just because something is from a tutorial that it will work on your server, your settings. You will see that many websites that create tutorials tells you something similar. As this example shows, nothing is above producing a mistake.

Re: photo upload

Posted: Tue Feb 01, 2011 4:30 pm
by timoteo
Thanks I've just seen this line too and taken it off - I actually repeat it later.
Well the tutorial I liked because I thought I understood it - well it all seems to make sense, however unless what I have learnt of php is up the spout I don't understand why I am getting T_variable error on

Code: Select all

$name = $_FILES['picture']['name'];

Re: photo upload

Posted: Tue Feb 01, 2011 4:33 pm
by social_experiment
Error messages aren't always on the given line. A guideline you can use : If your error is lets say on line 25, find line 25 and work your way up, that's an easy way to find the offending line.

Re: photo upload

Posted: Tue Feb 01, 2011 4:48 pm
by timoteo
but this is the 3rd line of my test page!!! I can't see anything before

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
<?php 
$userid = '16'
// if something was posted, start the process... 
if(isset($_POST['upload'])) 
{ 

// define the posted file into variables 
$name = $_FILES['picture']['name']; 

Re: photo upload

Posted: Tue Feb 01, 2011 4:50 pm
by timoteo
sorry copied old version with erroneous userid variable. This is what I have now with the error

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
<?php 

// if something was posted, start the process... 
if(isset($_POST['upload'])) 
{ 

// define the posted file into variables 
$name = $_FILES['picture']['name']; 

Re: photo upload

Posted: Tue Feb 08, 2011 5:48 am
by timoteo
I passed this problem to a new thread
viewtopic.php?f=1&t=127508
as the problem would seem to be a diferent one from what I originally thought