Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Uploading Images and keeping track of them in a database

    Ok, short thing I want to do first:

    I run a reefkeeping website, and am writing a database/php scripts to keep info on all sorts of fish and corals. When I add a new fish or coral via the scripts I want it to upload an image to the images directory, and then store its name in the table with the rest of the info of the fish or coral.

    Also, if the person so chooses to not upload an image I want to have the script just set the image name to none. (I think i have this part coveredd )

    Now, there is also an edit page. It pulls up the data for a specific creature and puts it nicely in some form for a me to edit. All fields except the unique ID are editable, including the image. That way the image can be updated if a better one is found, and then if the user chooses to not upload via this form it will check to see if a file name is listed and do this: 1. If nothing or empty: set it to none.jpg, 2. If its set to none.jpg do nothing, 3. If it has somthing other than none.jpg do nothing.

    Ok, now that what i am trying to do is out there, let me say what I am having problems with. Ive got a script that i have messed with and it works uploading the file, but I cant see the file after its uploaded (i.e. in my web browser). I checked it with my FTP program and its on the server, i even downloaded it to make sure it worked... it did. The directory is CHMOD to 777, and if i upload a picture using my FTP I can see it fine. It seems like its uploading incorrectly via the php. Here is my code for the form:

    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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form action="phpUpload.php" method="post" ENCTYPE="multipart/form-data">
    File: <input type="file" name="file" size="30"> <input type="submit" value="Upload!">
    </form>
    </body>
    </html>
    Here is the phpUpload.php file:
    Code:
    <?php
    // ==============
    // Configuration
    // ==============
    $uploaddir = "images/"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
    $allowed_ext = "jpg"; // These are the allowed extensions of the files that are uploaded
    $max_size = "500000"; // 50000 is the same as 50kb
    $max_height = "450"; // This is in pixels - Leave this field empty if you don't want to upload images
    $max_width = "450"; // This is in pixels - Leave this field empty if you don't want to upload images
    // Check Entension
    $extension = pathinfo($_FILES['file']['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ", $allowed_ext);
    for($i = 0; $i < count($allowed_paths); $i++) {
     if ($allowed_paths[$i] == "$extension") {
     $ok = "1";
     }
    }
    
    // Check File Size
    if ($ok == "1") {
    if($_FILES['file']['size'] > $max_size)
    {
    print "<font size=\"-1\" face=\"Verdana\">The image exceeded the 500KB Limit, please try again.</font>";
    exit;
    }
    
    // Check Height & Width
    if ($max_width && $max_height) {
    list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
    if($width > $max_width || $height > $max_height)
    {
    print "<font size=\"-1\" face=\"Verdana\">Image dimension are too big. Image must be 500X500 or lower.</font>";
    exit;
    }
    }
    
    // The Upload Part
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    
    $image = $_FILES['file']['name'];
    print "<font size=\"-1\" face=\"Verdana\">The image has been uploaded successfully.</font><BR><BR>";
    echo "$image";
    $imagetype = $_FILES['file']['type'];
    echo "<BR><BR>$imagetype";
    echo"<BR> <BR> <IMG SRC=$uploaddir/$image>";
    } else {
    print "<font size=\"-1\" face=\"Verdana\">Incorrect file extension, only JPG files are allowed to be uploaded.</font>";
    }
    ?>

  • #2
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for any input. Right now i just want to figure the uploading images out, im sure i can get the rest of the code down, its not too hard.

    Jasen

  • #3
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Look at these lines: To me it seems like there is an extraneous slash.
    Code:
    $uploaddir = "images/";
    
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    
    echo"<BR> <BR> <IMG SRC=$uploaddir/$image>";
    Please try this:
    Code:
    $uploaddir = "images/";
    
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.$_FILES['file']['name']);
    
    echo "<br /><br /><img src=\"$uploaddir.$image\">";
    If this doesn't work. After you upload your image, try to say view > source in your browser and post what code you have.

  • #4
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That trailing slash was removed.

    It posts fine, and it shows the code properly for the image, just doesnt show it. Even if i point my browser to the image file itself... nothing. Its almost like if i were to upload it wiht an FTP program in ASCII vice Binary.

    Jasen

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the output of the file:

    Code:
    <font size="-1" face="Verdana">The image has been uploaded successfully.</font><BR><BR>UF.jpg<BR><BR>image/jpeg<BR> <BR> <IMG SRC=images/UF.jpg>
    And, i checked my server, and the file is there!

  • #6
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    If this has something to do with the Apache server I must give up. I am really sorry. But someone else may know the answer.

    Probably you should tell us what happens when you point your browser directly to the image. What do you mean when you say that you see nothing. Is this a 'Not Found' or 'Forbidden' error message or what is it? Is it a blank screen? If it is a blank screen what happens when you say view source?

  • #7
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When i point my browser to the image say http://www.tidewater-reefers.org/com...ttom_right.jpg it gives me an error page that says no index file. But if i go to http://www.tidewater-reefers.org/com...mages/none.jpg (a file that was uploaded via FTP) it shows the file.

    Here is the file that i am using to upload pics:

    http://www.tidewater-reefers.org/com...ity/upload.php

    Try it and you will see everything that its doing...

  • #8
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Can you tell us what file permission the image has after upload? In your ftp program you would usually be able to see this after right-clicking on the file. In particular, does it have world read permission?

  • #9
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OMG... i am a freaking moron. I cant believe it, that surely was it. So... whats the code to change permisions... im feel so stupid right now.

    Jasen

  • #10
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    You may try to chmod after upload if you are allowed. Maybe it works.
    PHP Code:
    chmod($uploaddir.'/'.$image0755); 
    From a user comment on the php documentation
    jest3r at mtonic dot net 20-Oct-2005 06:10
    It seems that move_uploaded_file use the GROUP permissions of the parent directory of the tmp file location....

  • #11
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, i got the file uploading to work and i can see it! Using the link above you can test it out.

    Now, heres my next problem. The following code, while accessing the site via my MacBook, works fine. On a PC with internet explorer, it gets though everything fine, says "Added." at the end and everything which means it made it all the way through properly... but it doesnt actually upload the picture or add anything to the database!

    Code:
    <?
      require("includes/database.php");  
      $link = database_connect();
      
      //START IMAGE PROCESSING
     
     // ==============
    // Configuration
    // ==============
    $path = "/hermes/web10/b2812/pow.twr/htdocs/compatability/";
    $uploaddir = "images"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
    $allowed_ext = "jpg"; // These are the allowed extensions of the files that are uploaded
    $allowed_ext2 = "JPG";
    $allowed_ext3 = "gif";
    $allowed_ext4 = "GIF";
    $max_size = "500000"; // 50000 is the same as 50kb
    $max_height = "500"; // This is in pixels - Leave this field empty if you don't want to upload images
    $max_width = "500"; // This is in pixels - Leave this field empty if you don't want to upload images
    // Check Entension
    $extension = pathinfo($_FILES['file']['name']);
    $extension = $extension[extension];
    
    $ok = "1";
    
    // Check File Size
    if ($ok == "1") {
    if($_FILES['file']['size'] > $max_size)
    {
    print "<font size=\"-1\" face=\"Verdana\">The image exceeded the 500KB Limit, please try again.</font>";
    exit;
    }
    
    // Check Height & Width
    if ($max_width && $max_height) {
    list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
    if($width > $max_width || $height > $max_height)
    {
    print "<font size=\"-1\" face=\"Verdana\">Image dimension are too big. Image must be 500X500 or lower.</font>";
    exit;
    }
    }
    
    // The Upload Part
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    
    $image = $_FILES['file']['name'];
    
    $permission = $path.$uploaddir.'/'.$image;
    
    chmod("$permission", 0755);
    
    //if(chmod("$permission", 0755))
    //{
    //echo "Permission Set<BR><BR>";
    //}
    //else
    //{
    //echo "DOH!";
    //}
    
    //print "<font size=\"-1\" face=\"Verdana\">The image has been uploaded successfully.</font><BR><BR>";
    //echo "$image";
    //$imagetype = $_FILES['file']['type'];
    //echo "<BR><BR>$imagetype";
    //echo"<BR> <BR> <IMG SRC=$uploaddir/$image>";
    }
    
    else {
    print "<font size=\"-1\" face=\"Verdana\">Incorrect file extension, only JPG/GIF files are allowed to be uploaded.</font>";
    }
    
    //END IMAGE PROCESSING
    
    
      $result = mysql_query("INSERT INTO compatability (name, careLevel, temperment, reefSafe, acclimationTime, tankSize, lighting, Diet1, Diet2, Diet3, flow, notes, antagonist, image) VALUES(\"$HTTP_POST_VARS[name]\", \"$HTTP_POST_VARS[careLevel]\", \"$HTTP_POST_VARS[temperment]\", \"$HTTP_POST_VARS[reefSafe]\", \"$HTTP_POST_VARS[acclimationTime]\", \"$HTTP_POST_VARS[tankSize]\", \"$HTTP_POST_VARS[lighting]\", \"$HTTP_POST_VARS[Diet1]\", \"$HTTP_POST_VARS[Diet2]\", \"$HTTP_POST_VARS[Diet3]\", \"$HTTP_POST_VARS[flow]\", \"$HTTP_POST_VARS[notes]\", \"$HTTP_POST_VARS[antagonist]\", \"$image\")");
    
      if ($result) { 
    		
    		// $lastEntry = mysql_insert_id();
    		//header("Location: compatabilityProfile.php?id=$lastEntry");
    		//header("Location: compatabilityResults.php");
    	
    	echo "Added.";
      
      
       } 
    
      else { echo "<B>Failed to Add to the Database</B><BR><BR>"; echo mysql_error(); }
    
      	database_close($link);
    
      	// header("Location: compatabilityProfile.php?id=$new");
    
    ?>

  • #12
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    This suggests that there is some problem on the client side? I suggest you try to check that the uploaded file has been received at all on the server. Try to use this one-liner for phpUpload.php
    PHP Code:
    <?php
    var_dump
    ($_FILES);
    ?>
    Look to see if the array exists at all when uploading with IE, and if it does, check whether there is something unusual about the parameters. Perhaps try to post what you get with this.

  • #13
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is even further than just the image issue... it doenst do ANYTHING... it doesnt add the data thats in the form with it, etc so there is a lot more to it than just the files deal. It plain doesnt work with IE... which is odd since PHP all is executed from the server.

  • #14
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Sounds strange... You need to do some experiments. Try e.g. with Firefox instead of IE to find out whether it is the PC or the browser. With IE, remove statements one at a time from the script to find out exactly what part is causing the problems.


  •  

    Posting Permissions

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