SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload Files in forms

    Hi,

    I am currently developing an advertising site. Where people can upload information about what they are advertising and also add a picture.

    The advert is uploaded via PHP and into a MYSQL database.

    When the advert is submitted the image is given a unique Id (eg. DGH67JSU89) and stored in an images file as a .file (not in the database as a BLOB) and then the linking info to this is sent to the database with all the other information. eg file name, height, width, size.

    The problem is when you submit the form without addin a image which some people will do I get an error as there is no image being uploaded so nothing to link to.

    Is there any javascript, can I give the "file" field a pre-set image value unless one is added by the user? Basically is there anything I can do so that if the form is submitted with the "file" field blank, it takes a default image from my server and uploads that. So I have a image eg. "images/nopic.jpg" and I want this to be the image that is shown with all the adverts that dont upload their own images.

    Hope this makes sense..

    Looking forward to any replies

  2. #2
    SitePoint Wizard LiquidReflex's Avatar
    Join Date
    Aug 2003
    Location
    Minnesota
    Posts
    1,861
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What I normally do is check the size of the file upon upload using PHP. If the file size is zero, then don't try to upload it (avoiding an error) and leave the field blank in the database. Then, if you are loading the page with a record that has a blank image field, load up your default image. I believe this would be easier than having a default image uploaded to each one. You're just filling your database / server with the same data / images. Loading the same image from your server would be better than adding it over and over again when it's not necessary.

    The PHP code I use to check the file is (where 'file' is the name of your image input field:
    PHP Code:
    if ($_FILES['file']['size'] != '0') {
    do 
    your upload ...
    }
    else {
    only insert the data (without image upload) ...

    Kevin Hauge : Modern Leaf Design : Follow Us on Facebook
    Client Axis v.08 - client / project management script

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    The problem I have is the uniq id that is being generated is being done using the file information and some random generating, So even with that cade it wont work, I just get a parse error.

    The main file is stored on my web space and not in my database, the only info that is added to my db is the size width height etc.

    The adverts and everything linked to them will be deleted every 14 days also so space is not really an issue.

    Do you have any suggestgion on how I could get it to submit a default image if nothing is uploaded by the user?

    I can see the benifits of the way you have said but im fairly ne to PHP and have spend the past month and a half developing the upload and search script. To do it your way means starting from scratch on the whole lot for me..

    Thanks

    Mark

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, that was cool. I was looking for something similar. Not a default image, but I want the script to do something else. Else is a great thing.

  5. #5
    SitePoint Wizard LiquidReflex's Avatar
    Join Date
    Aug 2003
    Location
    Minnesota
    Posts
    1,861
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hmm ... I guess I don't understand the whole process you are doing. So basically people are uploading ads to your server but some don't have images? And the only way you track them is by putting all the images in a certain directory with a random file name? Seems odd that some wouldnt' have images ... but again, I don't think I quite grasp your situation.

    Well, as for taking an image already on the server if they didn't upload one, perhaps you could use the rename() function in PHP. Basically take the default image you have on the server already, basically rename it as your unique / random ID and put it in the folder you want.

    Something like this (where /your/docroot/ is the document root of your site, default.jpg is your image and the 1234random.jpg is the new name you want it to be):

    PHP Code:
    if ($_FILES['file']['size'] != '0') { 
    do 
    your upload as normal ...

    else { 
    rename("/your/docroot/default.jpg""/your/docroot/1234random.jpg");

    I haven't tried the rename with jpg's, but I believe it should work.
    Kevin Hauge : Modern Leaf Design : Follow Us on Facebook
    Client Axis v.08 - client / project management script

  6. #6
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Im not being clear.

    People fill out a form with all the information they want to add to the advert and then they press submit. This then runs my preview.php which shows everything they have submitted so they can check its right. They then get to upload their image and give it a name. Heres the code:


    PHP Code:
    <?php

    // This gets all the post vars
    $title $HTTP_POST_VARS["title"];
    $title2 $HTTP_POST_VARS["title2"];
    $ped $HTTP_POST_VARS["ped"];
    $dogs $HTTP_POST_VARS["dogs"];
    $*****
    $HTTP_POST_VARS["*****s"];
    $bdd $HTTP_POST_VARS["bdd"];
    $bmm $HTTP_POST_VARS["bmm"];
    $byy $HTTP_POST_VARS["byy"];
    $rdd $HTTP_POST_VARS["rdd"];
    $rmm $HTTP_POST_VARS["rmm"];
    $ryy $HTTP_POST_VARS["ryy"];
    $parents $HTTP_POST_VARS["parents"];
    $inject $HTTP_POST_VARS["inject"];
    $descr $HTTP_POST_VARS["descr"];
    $price $HTTP_POST_VARS["price"];
    $location $HTTP_POST_VARS["location"];
    $owner $HTTP_POST_VARS["owner"];
    $ownernumber $HTTP_POST_VARS["ownernumber"];
    $owneremail $HTTP_POST_VARS["owneremail"];

    ?>
    //They are then all shown (Ive deleted most of this as its pointless including it)
            </p>
            <table width="541" height="1047" border="0" align="center" bordercolor="#ECE9D8" background="images/puppreview.jpg">
              <tr>
                <td valign="top"><div align="center"> <br />
                    <br />
                    <table width="521" border="1" align="center" cellpadding="1" 
    .................................


                  <form action="/upload.php" method="post" enctype="multipart/form-data" name="upform" id="upform">
                          <p>
    //This is all the details from above being posted again to next page
    <input type="hidden" name="title" value="<?php echo $title?>" />
    <input type="hidden" name="title2" value="<?php echo $title2?>" />
    <input type="hidden" name="ped" value="<?php echo $ped?>" />
    <input type="hidden" name="dogs" value="<?php echo $dogs?>" />
    <input type="hidden" name="*****s" value="<?php echo $*****s?>" />
    <input type="hidden" name="bdd" value="<?php echo $bdd?>" />
    <input type="hidden" name="bmm" value="<?php echo $bmm?>" />
    <input type="hidden" name="byy" value="<?php echo $byy?>" />
    <input type="hidden" name="rdd" value="<?php echo $rdd?>" />
    <input type="hidden" name="rmm" value="<?php echo $rmm?>" />
    <input type="hidden" name="ryy" value="<?php echo $ryy?>" />
    <input type="hidden" name="parents" value="<?php echo $parents?>" />
    <input type="hidden" name="inject" value="<?php echo $inject?>" />
    <input type="hidden" name="descr" value="<?php echo $descr?>" />
    <input type="hidden" name="price" value="<?php echo $price?>" />
    <input type="hidden" name="location" value="<?php echo $location?>" />
    <input type="hidden" name="owner" value="<?php echo $owner?>" />
    <input type="hidden" name="owneremail" value="<?php echo $owneremail?>" />
    <input type="hidden" name="ownernumber" value="<?php echo $ownernumber?>" />
            
                  </p>
    <table width="515" border="0" align="center">
                            <tr>

    //This is where you get to upload your file
                              <td width="243">&nbsp;</td>
                              <td width="347"><span class="style14">Please upload only images that end in: </span>
                              <td height="50"><div align="right"><span class="style12 style11"><strong>Select Picture:</strong></span></div></td>
                              <td width="347" height="50"><input type="file" name="uploadfile" /></td>
                            </tr>
                            <tr>
                              <td height="50"><div align="right"><span class="style12 style11"><strong>Short Picture Description:</strong></span></div></td>
                              <td width="347" height="50"><input name="alt" type="text" maxlength="20" /></td>
                            </tr>
                            <tr>
                              <td><div align="center">
                                  <input  class="red" type="button" name="Submit" value="Submit" />
                              </div></td>
                              <td width="347">&nbsp;</td>
                            </tr>
                        </table>
                  </form>
                </div></td>
              </tr>
            </table>

    And here is my upload.php script:

    PHP Code:
    <?php
    // define the base image dir 
    $base_img_dir "./img/";

    // location of image
    $img_conv_dir "./bin/";

    // database table
    $img_table "images";

    // connect with database
    mysql_connect("localhost""root""");
    mysql_select_db("dogs");

    // generate unique id for use in filename
    $uniq uniqid("");

    // new file name
    $filename $base_img_dir.$uniq;

    // move uploaded file to destination
    move_uploaded_file($HTTP_POST_FILES["uploadfile"]["tmp_name"],$filename);

    // retrieve image info
    $imginfo getimagesize($filename);

    // handle image according to type
    switch ($imginfo[2]) {
        
    // gif
            // check gif image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromgif($filename);
            
    imagegif($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "GIF";
            break;

        
    // jpeg
            // check jpeg image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromjpeg($filename);
            
    imagejpeg($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "JPG";
            break;

        default:
            break;
    }

    // retrieve image file size
    $imgbytes filesize($filename);

    // insert image into db
    mysql_query("INSERT INTO $img_table (img_file, img_type, img_height,
       img_width, img_bytes, img_title, img_descr, img_alt, img_location, img_title2, img_ped, img_dogs, img_*****s, img_bdd, img_bmm, img_byy, img_rdd, img_rmm, img_ryy, img_parents, img_inject, img_price, img_you, img_email, img_phone)
      VALUES('
    $uniq', '$img_type', ".$imginfo[1].", ".$imginfo[0].",$imgbytes
    '"
    .addslashes($HTTP_POST_VARS["title"])."', 
    '"
    .addslashes($HTTP_POST_VARS["descr"])."',
    '"
    .addslashes($HTTP_POST_VARS["alt"])."', '".addslashes($HTTP_POST_VARS["location"])."', '".addslashes($HTTP_POST_VARS["title2"])."', '".addslashes($HTTP_POST_VARS["ped"])."', '".addslashes($HTTP_POST_VARS["dogs"])."', '".addslashes($HTTP_POST_VARS["*****s"])."', '".addslashes($HTTP_POST_VARS["bdd"])."', '".addslashes($HTTP_POST_VARS["bmm"])."', '".addslashes($HTTP_POST_VARS["byy"])."', '".addslashes($HTTP_POST_VARS["rdd"])."', '".addslashes($HTTP_POST_VARS["rmm"])."', '".addslashes($HTTP_POST_VARS["ryy"])."', '".addslashes($HTTP_POST_VARS["parents"])."', '".addslashes($HTTP_POST_VARS["inject"])."', '".addslashes($HTTP_POST_VARS["price"])."', '".addslashes($HTTP_POST_VARS["owner"])."', '".addslashes($HTTP_POST_VARS["owneremail"])."', '".addslashes($HTTP_POST_VARS["ownernumber"])."');");

    // This displays a message
    echo "Your Advert Has Been Uploaded"

    ?>

    It is for a dog advertising site. So not all people will have access to digital camera to upload an image. At the moment if no image is uploaded the whole advert wont upload.

    So now you have seen the code can you see a way around it?

    Cheers

  7. #7
    SitePoint Wizard LiquidReflex's Avatar
    Join Date
    Aug 2003
    Location
    Minnesota
    Posts
    1,861
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, that helps a little bit ...

    I think the main issue there is that you're trying to upload an image whether or not they have one input. I modified the code to use the if/else to check if one is input or not ... if it is, proceed to move_upload, if not, take the default image and create a file using your random ID code.

    I haven't tested this obviously, so it may not work just by copying and pasting it, you may need to tweak. One thing you do need to do is create your default image (I named it default_img.jpg) and put it in your $base_img_dir. If you name it something else or put it elsewhere, you'll need to modify the code to reflect that.

    This code essentially does the same input to the database, just changes the file being used as the source ... one uses the file being uploaded, the other uses the file already on the server.

    PHP Code:
    <?php
    // define the base image dir
    $base_img_dir "./img/";

    // location of image
    $img_conv_dir "./bin/";

    // database table
    $img_table "images";

    // connect with database
    mysql_connect("localhost""root""");
    mysql_select_db("dogs");

    // generate unique id for use in filename
    $uniq uniqid("");

    // new file name
    $filename $base_img_dir.$uniq;

    // MODIFIED CODE where $default_img path is the path to your default image, in my example in your $base_img_dir named default_img.jpg

    // Determine if user is uploading file
    if ($HTTP_POST_FILES['uploadfile']['size'] != '0') { 

        
    // move uploaded file to destination
        
    move_uploaded_file($HTTP_POST_FILES["uploadfile"]["tmp_name"],$filename);
    }
    else {
        
    // location of default image
        
    $default_img $base_img_dir "default_img.jpg";

        
    // create image from default image
        
    rename($default_img$filename);
    }

    // END MODIFIED CODE

    // retrieve image info
    $imginfo getimagesize($filename);

    // handle image according to type
    switch ($imginfo[2]) {
            
    // gif
            // check gif image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromgif($filename);
            
    imagegif($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "GIF";
            break;

            
    // jpeg
            // check jpeg image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromjpeg($filename);
            
    imagejpeg($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "JPG";
            break;

            default:
                break;
    }

    // retrieve image file size
    $imgbytes filesize($filename);

    // insert image into db
    mysql_query("INSERT INTO $img_table (img_file, img_type, img_height,
       img_width, img_bytes, img_title, img_descr, img_alt, img_location, img_title2, img_ped, img_dogs, img_*****s, img_bdd, img_bmm, img_byy, img_rdd, img_rmm, img_ryy, img_parents, img_inject, img_price, img_you, img_email, img_phone)
      VALUES('
    $uniq', '$img_type', ".$imginfo[1].", ".$imginfo[0].",$imgbytes,
    '"
    .addslashes($HTTP_POST_VARS["title"])."',
    '"
    .addslashes($HTTP_POST_VARS["descr"])."',
    '"
    .addslashes($HTTP_POST_VARS["alt"])."', '".addslashes($HTTP_POST_VARS["location"])."', '".addslashes($HTTP_POST_VARS["title2"])."', '".addslashes($HTTP_POST_VARS["ped"])."', '".addslashes($HTTP_POST_VARS["dogs"])."', '".addslashes($HTTP_POST_VARS["*****s"])."', '".addslashes($HTTP_POST_VARS["bdd"])."', '".addslashes($HTTP_POST_VARS["bmm"])."', '".addslashes($HTTP_POST_VARS["byy"])."', '".addslashes($HTTP_POST_VARS["rdd"])."', '".addslashes($HTTP_POST_VARS["rmm"])."', '".addslashes($HTTP_POST_VARS["ryy"])."', '".addslashes($HTTP_POST_VARS["parents"])."', '".addslashes($HTTP_POST_VARS["inject"])."', '".addslashes($HTTP_POST_VARS["price"])."', '".addslashes($HTTP_POST_VARS["owner"])."', '".addslashes($HTTP_POST_VARS["owneremail"])."', '".addslashes($HTTP_POST_VARS["ownernumber"])."');");

    // This displays a message
    echo "Your Advert Has Been Uploaded"

    ?>
    Kevin Hauge : Modern Leaf Design : Follow Us on Facebook
    Client Axis v.08 - client / project management script

  8. #8
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parse error: parse error, expecting `T_CASE' or `T_DEFAULT' or `'}'' in c:\phpdev\www\upload.php on line 221

    PHP Code:
    <?php
    // define the base image dir
    $base_img_dir "./img/";

    // location of image
    $img_conv_dir "./bin/";

    // database table
    $img_table "images";

    // connect with database
    mysql_connect("localhost""root""");
    mysql_select_db("dogs");

    // generate unique id for use in filename
    $uniq uniqid("");

    // new file name
    $filename $base_img_dir.$uniq;

    // MODIFIED CODE where $default_img path is the path to your default image, in my example in your $base_img_dir named default_img.jpg

    // Determine if user is uploading file
    if ($HTTP_POST_FILES['uploadfile']['size'] != '0') {

        
    // move uploaded file to destination
        
    move_uploaded_file($HTTP_POST_FILES["uploadfile"]["tmp_name"], $filename);
    }
    else {
        
    // location of default image
        
    $default_img $base_img_dir "nopic.jpg";

        
    // create image from default image
        
    rename($default_img$filename);
    }

    // END MODIFIED CODE

    // retrieve image info
    $imginfo getimagesize($filename);

    //
    // handle image according to type
    switch ($imginfo[2]) {
            
    // gif
            // check gif image by loading and saving the file
            //  to prevent wrong uploaded files and errors
           
    // THIS IS LINE 221, SO THE IMAGE CHECK IS CONFLICTING WITH YOUR CODE,
     
    $img imagecreatefromgif($filename);
            
    imagegif($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "GIF";
            break;

            
    // jpeg
            // check jpeg image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromjpeg($filename);
            
    imagejpeg($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "JPG";
            break;

            default:
                break;
    }

    // retrieve image file size
    $imgbytes filesize($filename);

    // insert image into db
    mysql_query("INSERT INTO $img_table (img_file, img_type, img_height,
       img_width, img_bytes, img_title, img_descr, img_alt, img_location, img_title2, img_ped, img_dogs, img_*****s, img_bdd, img_bmm, img_byy, img_rdd, img_rmm, img_ryy, img_parents, img_inject, img_price, img_you, img_email, img_phone)
      VALUES('
    $uniq', '$img_type', ".$imginfo[1].", ".$imginfo[0].",$imgbytes,
    '"
    .addslashes($HTTP_POST_VARS["title"])."',
    '"
    .addslashes($HTTP_POST_VARS["descr"])."',
    '"
    .addslashes($HTTP_POST_VARS["alt"])."', '".addslashes($HTTP_POST_VARS["location"])."', '".addslashes($HTTP_POST_VARS["title2"])."', '".addslashes($HTTP_POST_VARS["ped"])."', '".addslashes($HTTP_POST_VARS["dogs"])."', '".addslashes($HTTP_POST_VARS["*****s"])."', '".addslashes($HTTP_POST_VARS["bdd"])."', '".addslashes($HTTP_POST_VARS["bmm"])."', '".addslashes($HTTP_POST_VARS["byy"])."', '".addslashes($HTTP_POST_VARS["rdd"])."', '".addslashes($HTTP_POST_VARS["rmm"])."', '".addslashes($HTTP_POST_VARS["ryy"])."', '".addslashes($HTTP_POST_VARS["parents"])."', '".addslashes($HTTP_POST_VARS["inject"])."', '".addslashes($HTTP_POST_VARS["price"])."', '".addslashes($HTTP_POST_VARS["owner"])."', '".addslashes($HTTP_POST_VARS["owneremail"])."', '".addslashes($HTTP_POST_VARS["ownernumber"])."');");

    // This displays a message
    echo "Your Advert Has Been Uploaded"

    ?>
    Ive spent days editing about the code but I cant figure out why its giving the error, even if I upload an image it now gives this error aswell...?

    So if I do or dont upload an image I get this error?

    Thanks for all the help... I promise ill go away when I get my head round this!

  9. #9
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok forget the above,
    SOrted the stupid parse error!

    Now I get by the parse error and get:

    Warning: rename() failed (No such file or directory) in c:\phpdev\www\upload.php on line 208

    Warning: getimagesize: Unable to open './img/45251d695b5de' for reading. in c:\phpdev\www\upload.php on line 214

    Warning: stat failed for ./img/45251d695b5de (errno=2 - No such file or directory) in c:\phpdev\www\upload.php on line 245


    Ive never used the rename command so im really at a dead end with what to start editing?
    Ideas?

    Thanks

  10. #10
    SitePoint Wizard LiquidReflex's Avatar
    Join Date
    Aug 2003
    Location
    Minnesota
    Posts
    1,861
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hmm ... well you obviously have that code in another file since there are not 245 lines in the code above. I'd need to see the entire file to have a better idea of what else you're doing in the file and what the line actually is that is having the problem.

    In general, it sounds like it can't find the file for the rename() function. So my first question would be:

    In this section of the code:
    Code:
    else {
        // location of default image
        $default_img = $base_img_dir . "nopic.jpg";
    
        // create image from default image
        rename($default_img, $filename);
    }
    You are declaring that there is an image is your base image directory entitled nopic.jpg (./img/nopic.jpg) ... did you create an image called nopic.jpg and upload it to the img directory?
    Kevin Hauge : Modern Leaf Design : Follow Us on Facebook
    Client Axis v.08 - client / project management script

  11. #11
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"><!-- InstanceBegin template="/Templates/main.dwt" codeOutsideHTMLIsLocked="false" -->

    <!-- InstanceBeginEditable name="EditRegion4" -->EditRegion4<!-- InstanceEndEditable -->

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <!-- InstanceBeginEditable name="doctitle" -->
    <title>Untitled Document</title>
    <!-- InstanceEndEditable -->
    <!-- InstanceBeginEditable name="head" -->
    <!-- InstanceEndEditable -->
    <script type="text/JavaScript">
    <!--
    function MM_swapImgRestore() { //v3.0
      var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
    }

    function MM_preloadImages() { //v3.0
      var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
    }

    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_swapImage() { //v3.0
      var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
       if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
    }
    //-->
    </script>
    </head>

    <body onload="MM_preloadImages('images/menu%20dogs%20for%20sale%202.jpg','images/menu%20breeders%202.jpg','images/menu%20stud%20dogs%202.jpg','images/menu%20dog%20services%202.jpg','images/menu%20online%20shops%202.jpg','images/menu%20dogs%20litter%202.jpg','images/menu%20breeder%20add%202.jpg','images/menu%20stud%20dog%20add%202.jpg','images/menu%20services%20add%202.jpg','images/menu%20online%20store%20add%202.jpg','images/menu%20breed%20info%202.jpg','images/menu%20buying%20info%202.jpg','images/menu%20contact%20us%202.jpg')">
    <div align="center">
      <table width="939" height="145" border="0" background="images/puppy.jpg">
        <tr>
          <td width="935" align="right" valign="bottom"><div align="right">
            <table width="694" height="88" border="0">
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
            </table>
            <br />
            <font face="Arial" size="1" color="#000000"><b>
    <span id="clock">

    <SCRIPT LANGUAGE="JavaScript">

    var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    function getthedate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
    year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10)
    daym="0"+daym
    var hours=mydate.getHours()
    var minutes=mydate.getMinutes()
    var seconds=mydate.getSeconds()
    var dn="AM"
    if (hours>=12)
    dn="PM"
    if (hours>12){
    hours=hours-12
    }
    {
     d = new Date();
     Time24H = new Date();
     Time24H.setTime(d.getTime() + (d.getTimezoneOffset()*60000) + 3600000);
     InternetTime = Math.round((Time24H.getHours()*60+Time24H.getMinutes()) / 1.44);
     if (InternetTime < 10) InternetTime = '00'+InternetTime;
     else if (InternetTime < 100) InternetTime = '0'+InternetTime;
    }
    if (hours==0)
    hours=12
    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds
    var cdate=dayarray[day]+", "+montharray[month]+"-"+daym+"-"+year+" | "+hours+":"+minutes+":"+seconds+" "+dn+""
    if (document.all)
    document.all.clock.innerHTML=cdate
    else if (document.getElementById)
    document.getElementById("clock").innerHTML=cdate
    else
    document.write(cdate)
    }
    if (!document.all&&!document.getElementById)
    getthedate()
    function goforit(){
    if (document.all||document.getElementById)
    setInterval("getthedate()",1000)
    }
    window.onload=goforit
    </script></div></td>
        </tr>
      </table>
      <table width="939" border="0">
        <tr>
          <td width="52" align="center" valign="top"><table width="141" height="501" border="0" background="images/menumain.jpg">
            <tr>
              <td align="center" valign="top"><div align="center">
                <table width="118" border="0">
                  <tr>
                    <td height="49" align="center" valign="middle"></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="search_dogs.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('DogsForSale','','images/menu%20dogs%20for%20sale%202.jpg',1)"><img src="images/menu%20dogs%20for%20sale%201.jpg" name="DogsForSale" width="115" height="23" border="0" id="DogsForSale" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Breeders','','images/menu%20breeders%202.jpg',1)"><img src="images/menu%20breeders%201.jpg" name="Breeders" width="115" height="23" border="0" id="Breeders" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('StudDogs','','images/menu%20stud%20dogs%202.jpg',1)"><img src="images/menu%20stud%20dogs%201.jpg" name="StudDogs" width="115" height="23" border="0" id="StudDogs" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('DogServices','','images/menu%20dog%20services%202.jpg',1)"><img src="images/menu%20dog%20services%201.jpg" name="DogServices" width="115" height="23" border="0" id="DogServices" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('OnlineShops','','images/menu%20online%20shops%202.jpg',1)"><img src="images/menu%20online%20shops%201.jpg" name="OnlineShops" width="115" height="23" border="0" id="OnlineShops" /></a></td>
                  </tr>
                  <tr>
                    <td height="34" align="center" valign="middle"></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="add_litter.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Dogs/Litter','','images/menu%20dogs%20litter%202.jpg',1)"><img src="images/menu%20dogs%20litter%201.jpg" name="Dogs/Litter" width="115" height="23" border="0" id="Dogs/Litter" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('BreederAdd','','images/menu%20breeder%20add%202.jpg',1)"><img src="images/menu%20breeder%20add%201.jpg" name="BreederAdd" width="115" height="23" border="0" id="BreederAdd" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('StudDogAdd','','images/menu%20stud%20dog%20add%202.jpg',1)"><img src="images/menu%20stud%20dog%20add%201.jpg" name="StudDogAdd" width="115" height="23" border="0" id="StudDogAdd" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('ServicesAdd','','images/menu%20services%20add%202.jpg',1)"><img src="images/menu%20services%20add%201.jpg" name="ServicesAdd" width="115" height="23" border="0" id="ServicesAdd" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('OnlineStoreAdd','','images/menu%20online%20store%20add%202.jpg',1)"><img src="images/menu%20online%20store%20add%201.jpg" name="OnlineStoreAdd" width="115" height="23" border="0" id="OnlineStoreAdd" /></a></td>
                  </tr>
                  <tr>
                    <td height="34" align="center" valign="middle"></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('BreedInfo','','images/menu%20breed%20info%202.jpg',1)"><img src="images/menu%20breed%20info%201.jpg" name="BreedInfo" width="115" height="23" border="0" id="BreedInfo" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('BuyingInfo','','images/menu%20buying%20info%202.jpg',1)"><img src="images/menu%20buying%20info%201.jpg" name="BuyingInfo" width="115" height="23" border="0" id="BuyingInfo" /></a></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('ContactUs','','images/menu%20contact%20us%202.jpg',1)"><img src="images/menu%20contact%20us%201.jpg" name="ContactUs" width="115" height="23" border="0" id="ContactUs" /></a></td>
                  </tr>
                </table>
              </div></td>
            </tr>
          </table></td>
          <td width="731" align="left" valign="top"><div align="left"><!-- InstanceBeginEditable name="EditRegion3" --><div align="center">
              <?php
    // define the base image dir 
    $base_img_dir "./img/";

    // define location of image conversion programs
    $img_conv_dir "./bin/";

    // define database table containing image info
    $img_table "images";

    // connect with database
    mysql_connect("localhost""root""");
    mysql_select_db("images");

    // generate unique id for use in filename
    $uniq uniqid("");

    // new file name
    $filename $base_img_dir.$uniq;

    // MODIFIED CODE where $default_img path is the path to your default image, in my example in your $base_img_dir named default_img.jpg

    // Determine if user is uploading file
    if ($HTTP_POST_FILES['uploadfile']['size'] != '0') {

        
    // move uploaded file to destination
        
    move_uploaded_file($HTTP_POST_FILES["uploadfile"]["tmp_name"], $filename);
    }
    else {
        
    // location of default image
        
    $default_img $base_img_dir "nopic.jpg";

        
    // create image from default image
        
    rename($default_img$filename);
    }

    // END MODIFIED CODE

    // retrieve image info
    $imginfo getimagesize($filename);

    // handle image according to type
    switch ($imginfo[2]) {
        case 
    1// gif
            // check gif image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromgif($filename);
            
    imagegif($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "GIF";
            break;

        case 
    2// jpeg
            // check jpeg image by loading and saving the file
            //  to prevent wrong uploaded files and errors
            
    $img imagecreatefromjpeg($filename);
            
    imagejpeg($img$filename);
            
    imagedestroy($img);
            
            
    // set image type to jpeg
            
    $img_type "JPG";
            break;

        default:
            break;
    }

    // retrieve image file size
    $imgbytes filesize($filename);

    // insert image into db
    mysql_query("INSERT INTO $img_table (img_file, img_type, img_height,
       img_width, img_bytes, img_title, img_descr, img_alt, img_location, img_title2, img_ped, img_dogs, img_*****s, img_bdd, img_bmm, img_byy, img_rdd, img_rmm, img_ryy, img_parents, img_inject, img_price, img_you, img_email, img_phone)
      VALUES('
    $uniq', '$img_type', ".$imginfo[1].", ".$imginfo[0].",
             
    $imgbytes, '".addslashes($HTTP_POST_VARS["title"])."', '".
             
    addslashes($HTTP_POST_VARS["descr"])."',
             '"
    .addslashes($HTTP_POST_VARS["alt"])."', '".addslashes($HTTP_POST_VARS["location"])."', '".addslashes($HTTP_POST_VARS["title2"])."', '".addslashes($HTTP_POST_VARS["ped"])."', '".addslashes($HTTP_POST_VARS["dogs"])."', '".addslashes($HTTP_POST_VARS["*****s"])."', '".addslashes($HTTP_POST_VARS["bdd"])."', '".addslashes($HTTP_POST_VARS["bmm"])."', '".addslashes($HTTP_POST_VARS["byy"])."', '".addslashes($HTTP_POST_VARS["rdd"])."', '".addslashes($HTTP_POST_VARS["rmm"])."', '".addslashes($HTTP_POST_VARS["ryy"])."', '".addslashes($HTTP_POST_VARS["parents"])."', '".addslashes($HTTP_POST_VARS["inject"])."', '".addslashes($HTTP_POST_VARS["price"])."', '".addslashes($HTTP_POST_VARS["owner"])."', '".addslashes($HTTP_POST_VARS["owneremail"])."', '".addslashes($HTTP_POST_VARS["ownernumber"])."');");

    // display some information
    echo "Your Advert Has Been Uploaded<br><img src=\"img.php?f($uniq)+x(300)\"><br>You Will Be Re-Directed In A Moment"

    ?>
              </div><!-- InstanceEndEditable --></div></td>
          <td width="142" align="center" valign="top"><img src="images/menu.png" width="141" height="500" /></td>
        </tr>
      </table>
    </div>
    </body>
    <!-- InstanceEnd --></html>
    So here is the entire code. The rest of the code I ommited is only a frame.
    Yes I have made a /img/nopic.jpg,

    So what u think?

  12. #12
    SitePoint Wizard LiquidReflex's Avatar
    Join Date
    Aug 2003
    Location
    Minnesota
    Posts
    1,861
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hmmm ... try the copy functioni instead. Perhaps that will work better since you are saving them to the same directory.

    Replace:
    // create image from default image
    rename($default_img, $filename);

    With:
    // create image from default image
    copy($default_img, $filename);

    See if that works ...
    Kevin Hauge : Modern Leaf Design : Follow Us on Facebook
    Client Axis v.08 - client / project management script


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •