picture uploading... big problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

picture uploading... big problems

Post by jente »

Hi you all,

I'm fairly new to php. For my job, I have to create a website. I have designed a system where all employers can add products from our company to the website. That al worked (works) like a charm. But then I've got ambitious :). I want to give the users a possibility to upload pictures from the product too. If searched and found a script on the net, which works fine in his standard way. But when I try to implement it in my existing code, it does not work. It returns the (programmed) error messager "Het uploaden van is niet gelukt". But I don't know where my error is... It would be extremely kind of y'all if it's possible to check it out...

Here is my code

webedit.php:

Code: Select all

<?php 

session_start();  

if ($Login == '1') {  
?>
 
<? include ("navigation.inc")?> 

<p align="left"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif">[<a href="webedit.php?action=addproduct">Add a product to the website</a> ] </font></p> 


<? 
if ($HTTP_POST_VARS['Submit']) 
{ 


include ("pictureupload.php");


$db = mysql_connect("localhost", "user", "pass") or die ("Connection was not possible"); 
mysql_select_db("integra", $db); 
$sql1 = "INSERT INTO integraweb_products(productname, productinfo, productcat, picturepath) VALUES ('$productnaam','$productinfo', '$productcategorie', 'http://www.integra-belgium.com/full/".$file."') "; 
$resultset = mysql_query($sql1); 
echo "<font color="#666666" size="2" face="Tahoma, Arial"><strong>Product has been successfully added to the website.</strong></font>"; 


} 
?> 

<? 
 if ($_GET['action'] == "addproduct"){ 
  
include ("addproduct.php");
} 
?> 
    </div> 
     
    </td> 

  </tr> 


<?php 
} 
else {  
echo "<script type='text/javascript' language='JavaScript'>document.location.href='/intranet/index.php';</script>"; 
}  
?>
pictureupload.php:

Code: Select all

<?
$thumbdir = "thumb/";  
$fulldir = "full/";  

$file = basename($_FILES['image']['name']);  
$ext = explode(".", $file);  
$ext_num = count($ext);  
$path = "$thumbdir";  
$file_extension = $ext[$ext_num-1];  
$n=$naam;    
while (file_exists($path.$n.".".strtoupper($file_extension))) {      
$n = $n + 1;  
}   
# filename that is saved as image (no extension) 
$filename = "$thumbdir$n";  

# resize values 
$resizewidth = 200;  
$resizeheight = 200;  

# resize 

include("function.php");  

if(isset($_FILES['image']['size'])) {  
    if($_FILES['image']['type'] == "image/pjpeg") {  
        $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);  
     }  
    elseif($_FILES['image']['type'] == "image/x-png") {  
        $im = imagecreatefrompng($_FILES['image']['tmp_name']);  
    }elseif($_FILES['image']['type'] == "image/gif") {  
        $im = imagecreatefromgif($_FILES['image']['tmp_name']);  
   }  
    if(isset($im)){  
        if(file_exists("$filename.jpg")) {  
            unlink("$filename.jpg");  
  }  
        ResizeImage($im,$resizewidth,$resizeheight,$filename);  
        ImageDestroy ($im);  
 }  
}  
# saving the original
$path = "$fulldir";  
$file_size = round($_FILES['image']['size'] / 1024);     
$file_type = $_FILES['image']['type'];      
$file = basename($_FILES['image']['name']);     
$ext = explode(".", $file);     
$ext_num = count($ext);    
$file_extension = $ext[$ext_num-1];    

$n=$naam;    
    while (file_exists($path.$n.".".strtoupper($file_extension))) {      
        $n = $n + 1;      
  }      
    if(move_uploaded_file($_FILES['image']['tmp_name'], $path.$n.".".strtoupper($file_extension))) {     
       echo"The picture<b>".$file."</b> with a filesize of <b>".$file_size."</b> kb, with extension<b>.".$file_extension."</b> is succesfully uploaded to<b>".$path."</b> as <b>".$n.".".$file_extension."</b>";   
 }  
    else {     
           echo"File: <b>".$file."</b> could not be uploaded. There was an error.";    
}  

?>
addproduct.php:

Code: Select all

<?php

echo '<form name="form1" id="form1" method="post" action=""> 
  <table width="100%"  border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <th width="25%" align="left" scope="row"><span class="style7">Productname</span></th> 
      <td width="75%"><input type="text" name="productnaam" /></td><br> 
    </tr> 
    <tr align="left"> 
      <th scope="row"><span class="style7">Productinformation</span></th> 
      <td><textarea name="productinfo" cols="50" rows="10" wrap="OFF"></textarea>      </td> 
    </tr> 

     <tr align="left"> 
      <th scope="row"><span class="style7">Categorie</th> 
      <td><select name="Category"> 
          <option>Make your choice!</option> 
          <option>HDL</option> 
          <option>braillenotitietoestellen</option> 
          <option>brailledrukkers</option> 
          <option>Low Vision</option> 
          <option>brailledisplays</option> 
          <option>software</option> 
              </select> 
      <br></td> 
    </tr> 
    <tr align="left"> 
      <th scope="row"><br></th> 
      <tr> 
      <th width="25%" align="left" scope="row"><span class="style7">Image</span></th> 
      <td width="75%"><input type="file" name="image" /></td><br> 
    </tr> 
    <tr> 
      <th width="25%" align="left" scope="row"><span class="style7">Imagename</span></th> 
      <td width="75%"><input type="text" name="naam" /></td><br> 
    </tr> 
      <tr> 
      <th width="25%" align="left" scope="row"></th> 
      <td width="75%"></td><br> 
    </tr> 
    <tr> 
      <td><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
  </table> 
</form>'; 

?>
Thanks in advance...
Last edited by jente on Fri Nov 14, 2003 3:13 am, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

anyway you could translate some of that foreign language to english so we americans and english-speaking only people could understand it and the error messages more clearly? thanks :)
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

Post by jente »

I translated the whole thing. Sorry I forgot :).
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

sweet, now what does the error message "Het uploaden van is niet gelukt" mean ? :)
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

Post by jente »

That was a piece of sourcecode, also translated now. With the current code, it would say:

File: could not be uploaded.There was an error

This is generated from this code;

Code: Select all

<?php
echo"File: <b>".$file."</b> could not be uploaded. There was an error.";    
?>
Indeed, the variable $file is empty. But I don't have any clue what the reason is of that... :?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

This statement seems to be teh problem :

Code: Select all

<?php
$sql1 = "INSERT INTO integraweb_products(productname, productinfo, productcat, picturepath) VALUES ('$productnaam','$productinfo', '$productcategorie', 'http://www.integra-belgium.com/full/".$file."') "; 
?>
notice that when you are calling $file, you are forgetting to not only end the " marks before calling it, but also forgetting to call the ' mark. so the code should read instead like this :

Code: Select all

<?php

$sql1 = "INSERT INTO integraweb_products(productname, productinfo, productcat, picturepath) VALUES ('$productnaam','$productinfo', '$productcategorie', 'http://www.integra-belgium.com/full/'".$file."') "; 

?>
hope that helps :)
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

Post by jente »

I've made that change, but it doesn't work. In my honest opinion the problem is situated earlier, because the errormsg I am getting is located in the code of pictureupload.php

Thanks in advance :)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i know this may seem tedious, but is that the EXACT error code, or did you just sumarize it? Sorry, i just don't see anything wrong with the code for pictureupload.php.. if the error doesn't give you a PHP error ( in other words, if it doesn't tell you a specific line in which there is a problem with it compiling the code ), more then likely the problem is either with the permissions to the directory you are trying to upload to, or the permissions to the file itself. other then that, you'll probably have to wait til a more experienced developer checks the thread and helps you out from there.

btw, it wouldn't happen to be a problem within the function.php file would it? I noticed you didn't have it on this thread with the others. maybe something in it? again, sorry i couldn't be of much help.


edit :

one more thing, make sure that the path

Code: Select all

<?php

$thumbdir = "thumb/";  
$fulldir = "full/";  


?>
is correct. if the Thumb and Full directories are not in the same directory that you are running this script in, it won't be able to upload to them.
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

Post by jente »

It's the exact errormsg

the code is located at http://www.integra-belgium.com/intranet/webedit.php

But with the exact same code, but only that code, without anything else, it works;

http://www.integra-belgium.com/resize.php

resize.php

Code: Select all

<?php 

if (isset($_POST['submit']) == 1) { 
$thumbdir = "thumb/"; 
$fulldir = "full/"; 

$file = basename($_FILES['image']['name']); 
$ext = explode(".", $file); 
$ext_num = count($ext); 
$path = "$thumbdir"; 
$file_extension = $ext[$ext_num-1]; 
$n=$naam;   
while (file_exists($path.$n.".".strtoupper($file_extension))) {     
$n = $n + 1; 
}  
 
$filename = "$thumbdir$n"; 
#resize values
$resizewidth = 200; 
$resizeheight = 200; 

# resize 

include("function.php"); 

if(isset($_FILES['image']['size'])) { 
    if($_FILES['image']['type'] == "image/pjpeg") { 
        $im = imagecreatefromjpeg($_FILES['image']['tmp_name']); 
     } 
    elseif($_FILES['image']['type'] == "image/x-png") { 
        $im = imagecreatefrompng($_FILES['image']['tmp_name']); 
    }elseif($_FILES['image']['type'] == "image/gif") { 
        $im = imagecreatefromgif($_FILES['image']['tmp_name']); 
   } 
    if(isset($im)){ 
        if(file_exists("$filename.jpg")) { 
            unlink("$filename.jpg"); 
  } 
        ResizeImage($im,$resizewidth,$resizeheight,$filename); 
        ImageDestroy ($im); 
 } 
} 
# save the original
$path = "$fulldir"; 
$file_size = round($_FILES['image']['size'] / 1024);    
$file_type = $_FILES['image']['type'];     
$file = basename($_FILES['image']['name']);    
$ext = explode(".", $file);    
$ext_num = count($ext);   
$file_extension = $ext[$ext_num-1];   

$n=$naam;   
    while (file_exists($path.$n.".".strtoupper($file_extension))) {     
        $n = $n + 1;     
  }     
    if(move_uploaded_file($_FILES['image']['tmp_name'], $path.$n.".".strtoupper($file_extension))) {    
       echo"The picture <b>".$file."</b> with a filsize from <b>".$file_size."</b> kb, with extension <b>.".$file_extension."</b> is sucessfully uploaded to <b>".$path."</b> as <b>".$n.".".$file_extension."</b>";    
 } 
    else {    
           echo"Het uploaden van <b>".$file."</b> is niet gelukt";   
}  

# upload formuliertje 
?> 
<?php 
#if image is uploaded, show it
if(file_exists("$filename.jpg")) {  
echo "<br><br><img src="";  
echo ($filename.".jpg?id=".rand(0,999999));  
echo ""><br><br>"; 

exit; # don't display the form anymore
?> 
<? 
} # end submit
?> 
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> 
  <table border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <td colspan="2"><b>Resize Image</b></td> 
      <td>&nbsp;</td> 
    </tr> 
    <tr> 
      <td>Afbeelding:</td> 
      <td><input type="file" name="image"></td> 
      <td>&nbsp;</td> 
    </tr> 
    <tr> 
      <td>Naam:</td> 
      <td><input type="text" name="naam"></td> 
      <td>&nbsp;</td> 
    </tr> 
    <tr> 
      <td>&nbsp;</td> 
      <td> <input name="submit" type="submit" value="Upload"></td> 
      <td>&nbsp;</td> 
    </tr> 
  </table> 
  </form> 

?>
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

one more thing and then i quit :( try changing :

Code: Select all

<?php
$path = "$thumbdir";
?>
to

Code: Select all

<?php
$path = $thumbdir;
?>

and

Code: Select all

<?php
$path = "$fulldir";
?>
to

Code: Select all

<?php
$path = $fulldir;
?>

far as i know, variables aren't suposed to be surrounded with " marks unless you give them ".$var_name." values.


Edit : I will bet the farm on this will solve your problem :) least i hope so :P
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i'm not so sure what to make of

Code: Select all

<?php
$filename = "$thumbdir$n"; 
?>


or

Code: Select all

<?php
if(file_exists("$filename.jpg")) { 
            unlink("$filename.jpg"); 
?>
again, variable names are suposed to be taken out of the string since they hold values surrounded by " marks themselves normally...

i would think that these should look like

Code: Select all

<?php

if(file_exists("".$filename.".jpg.")) { 
            unlink("".$filename.".jpg"); 
?>
and

Code: Select all

<?php
$filename = $thumbdir.$n; 
?>

i am almost definate that this will resolve this issue...
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

Post by jente »

I did those changes, but did'nt work :s
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

sorry, i'm stumped :(
jente
Forum Newbie
Posts: 9
Joined: Fri Nov 14, 2003 2:57 am

Post by jente »

Little things can do great thing... I forgot to add "enctype="multipart/form-data"" to the form tag... and yes, that was the problem... It's solved now :). Anyway, many thanks to infolock for his good contributions.

J
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

glad to hear it worked out :)
Post Reply