Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-17-2005, 07:39 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
metazai is on a distinguished road
form file field check

Ok, I am stumped yet again. I'm writing a backend where users can upload images and information for an online store. I wrote an "upload new categories" script that works fine and writes the files perfectly. I then took the exact same script and added a few lines to create an "update category" script, which allows you to change information about the category. The trouble is that it seems to be running the upload file part of my script no matter what. In other words, if there is already a picture there and the user is just correcting a spelling in the description, when it uploads it uploads the empty data of the file form field and attempts (and fails) to write a blank file, then overwrites the correct URL stored in the MySQL database with the incomplete URL of the non-existent image it shouldn't be trying to process but is. Here's the form:

PHP Code:
<form method="post" action="<?php 
echo $_SERVER['/admin/PHP_SELF']; ?>" enctype="multipart/form-data">
    <span class="verd12">
<input type="hidden" name="action" value="update">
 Category <br />
    <input name="catg" type="text" id="catg" size="40" value="<?php echo($row2['name']); ?>" />
    </span>
<p class="verd12">Current Image:<br /><img src="<?php echo($row2['image']); ?>"></p>
<p class="verd12">Change Image:<br />
        <input name="userfile" type="file" id="userfile" />
    </p>
    <p class="verd12">New Description<br />
    <textarea name="new_desc" cols="40" rows="5" id="new_desc"><?php echo($row2['description']); ?></textarea>
</p>
</form>
And here's the relevant part of what it feeds into, after checking the "action" form variable and getting "update".

PHP Code:
if  ($_FILES['userfile'])  {
$uploaddir '/var/www/html/newsite/productimages/';
$uploadfile $uploaddir basename($_FILES['userfile']['name']);
$image_url 'http://www.thissite.com/newsite/productimages/'.basename($_FILES['userfile']['name']);
if (
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo 
"<span class=\"verd12\">File is valid, and was successfully uploaded.</span>\n";
} else {
   echo 
"<span class=\"verd12\">ERROR! File was not uploaded  . . .</span>\n";
}
$newfilestring=" image='$image_url', ";}
else {
$newfilestring=" ";} 
$updquery "UPDATE categories SET name='$catg',".$newfilestring."description='$new_desc' WHERE id=$id"
Any ideas? Banging my head into the wall on this one. Again, this script works without the test for the file form field when used on a "new category" page, and it updates everything else correctly too.
metazai is offline   Reply With Quote
Old 08-17-2005, 08:51 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
just by looking real quick, maybe you should be chkecking for $_POST['userfile'] instead of the actual file in the $_FILE array first. not sure though.
__________________
Mike
sde is offline   Reply With Quote
Old 08-18-2005, 12:55 AM   #3 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
This is my least favorite part of php (not any other language would make it easier). Here's a class method of mine that delt with image uploading
PHP Code:
$picture = new image;
$picture->insert($_POST['author'], $_POST['image_title'], $_POST['description'], $_POST['width'], $_POST['height'], $_POST['date'], basename($_FILES['image_file']['name']));
#snip----------------------------------------------------
     
function insert($author$image_title$description$width$height$date$image_file) {
      
$width $width 0;
      
$height $height 0;
      
      
$this->admin_connect();
      
      
$image_title $this->clean_input($image_title);
      
$description $this->clean_input($description);
      
$date $this->date_conv($date);
      
      
$url_dest "$this->docroot/images/content/" stripslashes($image_file);
      
$url =  mysql_real_escape_string('/images/content/' . (stripslashes($image_file)));
      
      if (!
is_int($height)) {
           echo(
"<P>height must be a number</P>\n");
           return 
false;
      }
      if (!
is_int($width)) {
           echo(
"<P>width must be a number</P>\n");
           return 
false;
      }

      if (!(
$description && $image_title && $date)) {
           echo(
"<P>Something's missing...</P>\n");
           return 
false;
      }

      
$SQLCheck "SELECT ID FROM Picture WHERE  title = '%s' AND Person_ID = %s";
      if (
$this->query_oneshot($SQLCheck, array($image_title$author))) {
           echo(
"<P>Picture already in database with this Title and Author</P>\n");
           return 
false;
      }
      
      
$SQLsubmit "INSERT INTO Picture VALUES(NULL, %s, '%s', '%s', '%s', '%s', '%s', '%s')";
      
$SQLargs = array($author$url$image_title$description$height$width$date);
      echo(
"<P>$sthPicture</P>");
      if (! 
move_uploaded_file($_FILES['image_file']['tmp_name'], $url_dest)) {
           
print_r($_FILES);
           echo(
"<P>Image failed to transfer :-(</p>\n");
      }
      
$results $this->submit($SQLsubmit$SQLargs);
      echo(
"<P>Image $image_title successfully uploaded</P>\n");
      return 
true
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 08-18-2005, 07:33 AM   #4 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
metazai is on a distinguished road
Quote:
Originally Posted by sde
just by looking real quick, maybe you should be chkecking for $_POST['userfile'] instead of the actual file in the $_FILE array first. not sure though.
No, that does the opposite . . . instead of always uploading the contents (or lack thereof) of the file form field, it refuses to upload the contents even if there's something there.
metazai is offline   Reply With Quote
Old 08-18-2005, 07:52 AM   #5 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
metazai is on a distinguished road
teknomage1, thanks for your post. I'm struggling with it for two reasons: a) I'm still struggling with PHP and OOP (can anyone recommend a good book on the subject?), and b) I'm not having problems uploading images, I'm having problems because the form field is optional and when it's left blank it's being accessed anyway. I'm not copacetic enough with OOP to be able to tell if your script addresses this, but it looks like it doesn't.

Not sure what the problem is with me with OOP, btw. Most other things I have taught myself have some sort of foothold I can get my brain around, but I'm still looking for one here.
metazai is offline   Reply With Quote
Old 08-18-2005, 08:20 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
i have code that does this, but i don't have access to it right now. another way to approach this is a checkbox to indicate that the user wants to also upload a file.

then, you can just check for the checkbox value. it's more work, but it might turn out feeling a little slicker of an interface. here is some non tested code.
PHP Code:
<?php
if($_POST['upload_enabled'] == 1){
  
// do upload logic here
}
?>
<script language="javascript" type="text/javascript">
function toggleUploadField(){

  var upload_enabled = document.getElementById('upload_enabled');
  var upload_file = document.getElementById('upload_file');
  var is_enabled = ( upload_enabled.checked ) ? true : false;

  // set disable or enable file field based on enable checkbox.
  upload_file.disabled = is_enabled ? false : true;
}
</script>

  
<form ...>
<input type="checkbox" id="upload_enabled" name="upload_enabled" value="1" /> Update Image <br />
<input type="file" id="upload_file" name="upload_file" onclick="toggleUpdateField();" /> <br />
<input type="submit" />
</form>
hmmm .. on second thought, .. why don't you just try to test for $_FILES['upload_file']['tmp_name'] ... i just remembered .. i think that is how i did it before.
__________________
Mike
sde is offline   Reply With Quote
Old 08-18-2005, 08:30 AM   #7 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
metazai is on a distinguished road
Stan- oops, I mean Sde comes through for me again! I do not know what I would do without these forums. Seriously. The best, friendliest, most helpful responses on the web, man.

$_FILES['upload_file']['tmp_name'] is what worked . . . and as with all good solutions, I looked at it and wondered "how come I didn't think of that?"

Thanks all.
metazai is offline   Reply With Quote
Old 08-18-2005, 06:59 PM   #8 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Glad you got it working man! I guess I kind of had it buried in the code. but this was the bit I thought you'd be interested in
PHP Code:
      if (! move_uploaded_file($_FILES['image_file']['tmp_name'], $url_dest)) {
           
print_r($_FILES);
           echo(
"<P>Image failed to transfer :-(</p>\n");
      } 
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript: check if file input is undefined sde HTML, XML, Javascript, AJAX 3 03-28-2005 08:15 AM
Form Field Change nyoung HTML, XML, Javascript, AJAX 3 03-17-2005 04:38 AM
regex for allowed characters in a form field DogTags PHP 1 03-13-2005 08:42 AM


All times are GMT -8. The time now is 11:12 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting