I can't be far wrong...

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
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

I can't be far wrong...

Post by MiniMonty »

Hi all,

trying to finish my upload images script and it all works fine except for one annoying
glitch. Users are asked to choose an image then choose a gallery to post it to from a
drop down menu. I have error checking for filetype, filesize, whether they have actually chosen
an image but I can't get the syntax right for checking if they have chosen a gallery from the
drop down. The default setting for the menu is "Which Gallery?" and at present the code will
allow me to upload an image whcih is then inserted into the db as belonging to the gallery "Which Gallery?"
instead of portraits / landscapes etc.,

Can anyone help ?

Best wishes
Monty

Drop down in html:
<select name="gowhere" id="gowhere">

The relevant part of the upload script: should be round about line 18

Code: Select all

 
if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 0)){
    $image = $_FILES['fileField']['name'];
    $filename = stripslashes($_FILES['fileField']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
   
        //Filetype check
    if (!in_array($extension,$acceptableextensions)){
        exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
        echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
        $errors=1;
       
        //Filesize check
    } else if ($_FILES['fileField']['size'] > $maxfilesize ) {
        $error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
        unlink($_FILES['fileField']['tmp_name']);
        
        // Chose a Gallery from the drop down menu check
        if (isset ($_FILES['gowhere']) && ($_FILES['gowhere'] != 'Which Gallery?')) {
          $gowhere = mysql_real_escape_string ($_FILES['gowhere']);
    } else {
         $gowhere = FALSE;
         $error_msg = "<p>Please select a Gallery to post your image to.</p>";
    }
 
    } else {
            // rename the picture incrementally
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        $groovy = sizeof(glob("members/$id/images/*"));
        $groovy = ++$groovy;
 
        $image_name=$groovy.'.'.$extension;
        $newname="".$image_name;
        $gowhere = mysql_real_escape_string($_POST['gowhere']);
        
            // NOW make the resize call !
        //img_resize ($_FILES ['fileField'] [ 'name'], $_FILES [ 'fileField'] [ 'tmp_name'], 25, $newname);
        
            // INSERT image into the DB
        $sql = mysql_query("INSERT INTO pictures
        (UID,filename,dirpath,gallery,dtadded)
        VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
        or die (mysql_error());
 
        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
        chmod ("members/$id/images/$newname", 0666);
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I can't be far wrong...

Post by requinix »

Code: Select all

if (isset ($_FILES['gowhere']) && ($_FILES['gowhere'] != 'Which Gallery?')) {
  $gowhere = mysql_real_escape_string ($_FILES['gowhere']);
$_FILES?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: I can't be far wrong...

Post by Weiry »

MiniMonty wrote: Drop down in html:
<select name="gowhere" id="gowhere">
What about the actual values for each option in the select?
If there are no values set, then you wont have anything to check against in your php script.

Code: Select all

<option>MyOption</option>
should actually be

Code: Select all

<option value="myValue">MyOption</option>
Also, your select box is not actually part of $_FILE, it is a part of $_POST (if using method='post') or $_GET (if using method='get')

As you really provided no form code, i can only assume your using POST and that you dont have any values set in your select fields.

Once you add values to each option in your select box (relating to your gallery), then you should change the following:

Code: Select all

if (isset ($_POST['gowhere']) && ($_POST['gowhere'] != 'Which Gallery?')) {
          $gowhere = mysql_real_escape_string ($_POST['gowhere']);
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: I can't be far wrong...

Post by MiniMonty »

Hey guys, thanks for the replies...

Yeah, I know...
swapping $_POST to $_FILES was my last ditch wildcard attempt to make it work after all
else failed :?

So I guess my next question is "how would you guys do it"?
Because everything I've tried (and I like to try before I post questions)
has still allowed the upload of images whether or not a gallery has been selected from the drop down.

Best wishes
Monty


here's the form code from the html part of the file:

Code: Select all

 
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
        <table width="70%"  border="0" align="center">
          <tr>
            <td height="41"><div align="center">
                <input name="fileField" type="file" id="fileField" size="0" maxlength="0">
            </div></td>
            <td><div align="center">
              <select name="gowhere" id="gowhere">
                <option selected>Which Gallery?</option>
                <option value="portraits">People</option>
                  <option value="landscapes">Landscapes</option>
                  <option value="urban">Urban</option>
                  <option value="travel">Travel</option>
                  <option value="action">Action</option>
                  <option value="reportage">Reportage</option>
                </select>
            </div></td>
            <td><div align="center">
                <input name="button" type="submit" id="button" value="Go">
                <input name="parse_var" type="hidden" value="pic" />
            </div></td>
          </tr>
        </table>
      </form
 
and the script with the error checking:

Code: Select all

 
if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 0)){
    $image = $_FILES['fileField']['name'];
    $filename = stripslashes($_FILES['fileField']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
   
        //Filetype check
    if (!in_array($extension,$acceptableextensions)){
        exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
        echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
        $errors=1;
       
        //Filesize check
    } else if ($_FILES['fileField']['size'] > $maxfilesize ) {
        $error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
        unlink($_FILES['fileField']['tmp_name']);
        
        // Chose a Gallery fromthe drop down menu check
// STILL NOT WORKING YET !!!
 
    } else {
            // rename the picture incrementally
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        $groovy = sizeof(glob("members/$id/images/*"));
        $groovy = ++$groovy;
 
        $image_name=$groovy.'.'.$extension;
        $newname="".$image_name;
        $gowhere = mysql_real_escape_string($_POST['gowhere']);
        
            // NOW make the resize call !
        //need a much better resize script...
        
            // INSERT image into the DB
        $sql = mysql_query("INSERT INTO pictures
        (UID,filename,dirpath,gallery,dtadded)
        VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
        or die (mysql_error());
                       
        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
        chmod ("members/$id/images/$newname", 0666);
 
        }
       
        $success_msg = '<span class="Big_Orange_Times">Your image has been uploaded, it may take a few moments to appear in your gallery, please be patient.</span>';
 
        } else if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 4)){ 
        //Error code 4: No image uploaded
        $error_msg = 'Please browse for an image before you press Go.';
}
?>
 
Post Reply