TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-14-2010, 07:09 AM   #1 (permalink)
The Wanderer
 
Join Date: Aug 2010
Location: West Coast, New Zealand
Posts: 5
Thanks: 2
cliffgs is on a distinguished road
Default Struggling with image uploads

Hello

I am really just getting to grips with php and mysql, and have a problem with a html form and php script which, apart from other details, uploads an image if required. I have got that working to my satisfaction. I also want to give the capability of editing the information. But if there is no change to the image, I want to leave that field blank. Trouble is, either an invalid file is reported, or it removes the image filename from the database when that field is left blank. I have found that if I load the file again, I can cause the script to ignore it if it already exists, so I could leave it at that, but I would rather leave the field blank.

I have spent ages on trying to figure this out. I have totally mucked up my php script that uploads the file, and I am completely confused. I have found (through Google) that the $_FILES array always returns TRUE, so how is it possible to find out if an image has been specified (or more importantly, if it has not!).

Any help with this would be greatly appreciated.

Cliff
cliffgs is offline  
Reply With Quote
Old 10-14-2010, 04:22 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Without seeing your code there is nothing we can to do help you besides refer you to Google.
__________________

Village Idiot is offline  
Reply With Quote
Old 10-15-2010, 05:09 AM   #3 (permalink)
The Wanderer
 
Join Date: Aug 2010
Location: West Coast, New Zealand
Posts: 5
Thanks: 2
cliffgs is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Without seeing your code there is nothing we can to do help you besides refer you to Google.
Hi, sorry about the long day, I had to drive to our nearest city (340km away) and back, that kept me off the computer :(

I have included the offending part of my script. It is an index.php file, the first once downloaded when the administrator logs in. It is a controller file: the 'add artist' and 'delete artist' are working, so I have not shown them. I have included the entire part of the script concerned with editing artists, although it is the 'Process editform' that I am having trouble with.


PHP Code:
//Show form to edit existing artist
if (isset($_POST['action']) and $_POST['action'] == 'Edit') {
    include(
$_SERVER['DOCUMENT_ROOT'] . '/../code2/admindb.inc.php');
    
    
$id mysqli_real_escape_string($link$_POST['id']);
    
$sql "SELECT * FROM artists WHERE id = '$id'";
    
$result mysqli_query($link$sql);
    if (!
$result)
    {
        
$error 'Error fetching artist details.';
    include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
    exit();
    }
    
$row mysqli_fetch_array($result);
    
    
$pagetitle 'Edit Artist';
    
$action 'editform';
    
$fname $row['fname'];
    
$lname $row['lname'];
    if (
is_null($row['filename'])) {
        
$filename '';
    } else {
        
$filename =  $row['filename'];
    }
    
$bio $row['bio'];
    
$id $row['id'];
    
$button 'Update Artist';
    
    include 
'form.html.php';
    exit();
}

//Process editform
if (isset($_GET['editform'])) { 
    if (
$_FILES["image"]["size"] > 0) {
if (((
$_FILES["image"]["type"] == "image/gif")
|| (
$_FILES["image"]["type"] == "image/jpeg")
|| (
$_FILES["image"]["type"] == "image/pjpeg"))
&& (
$_FILES["image"]["size"] < 200000))
  {
  if (
$_FILES["image"]["error"] > 0)
    {
    echo 
"Return Code: " $_FILES["image"]["error"] . "<br />";
    } else {
    if (
file_exists("../../meetArtists/" $_FILES["image"]["name"]))
      {
          
$filename $_FILES["image"]["name"];
         exit();

      } else {
      
move_uploaded_file($_FILES["image"]["tmp_name"],
      
"../../meetArtists/" $_FILES["image"]["name"]);
         
$filename $_FILES["image"]["name"];
         }
    }
  } else {
  echo 
"Invalid file";
  }
    }
include 
$_SERVER['DOCUMENT_ROOT'] . '/../code2/admindb.inc.php';

    
$fname mysqli_real_escape_string($link$_POST['fname']);
    
$lname mysqli_real_escape_string($link$_POST['lname']);
    
$bio mysqli_real_escape_string($link$_POST['elm1']);
    
$id = (int) $id mysqli_real_escape_string($link$_POST['id']);
    if (
$fname == '' || $lname == '') {
        
$error 'You must give the artist a first name and a surname. Click &lsquo;back&rsquo; and try again.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();    
    }
    if (
file_exists("../../meetArtists/" $_FILES["image"]["name"])) {
               
$filename $_FILES["image"]["name"];

$sql "UPDATE artists SET
    fname = '
$fname',
    lname = '
$lname',
    filename = '
$filename',
    bio = '
$bio'
    WHERE id = '
$id'";
    
    } else {
        
$sql "UPDATE artists SET
    fname = '
$fname',
    lname = '
$lname',
    bio = '
$bio'
    WHERE id = '
$id'";
    }
if (!
mysqli_query($link$sql)) {  //+2
    
$error 'Error updating selected artist.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();    
    }
    
header('Location: .');
    exit();

I hope this is enough. Many thanks in advance for any help I might receive.

Cliff
cliffgs is offline  
Reply With Quote
Old 10-16-2010, 11:34 PM   #4 (permalink)
The Wanderer
 
Join Date: Aug 2010
Location: West Coast, New Zealand
Posts: 5
Thanks: 2
cliffgs is on a distinguished road
Default

Hi

Probably not the best method (I am still trying to learn), but I have solved my problem:

PHP Code:
//Process editform
if (isset($_GET['editform'])) { 
include 
$_SERVER['DOCUMENT_ROOT'] . '/../code2/admindb.inc.php';

    
$fname mysqli_real_escape_string($link$_POST['fname']);
    
$lname mysqli_real_escape_string($link$_POST['lname']);
    
$bio mysqli_real_escape_string($link$_POST['elm1']);
    
$id = (int) $id mysqli_real_escape_string($link$_POST['id']);
    if (
$fname == '' || $lname == '') {
        
$error 'You must give the artist a first name and a surname. Click &lsquo;back&rsquo; and try again.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();    
    }

    if (
$_FILES["image"]["size"] > 0) {
if (((
$_FILES["image"]["type"] == "image/gif")
|| (
$_FILES["image"]["type"] == "image/jpeg")
|| (
$_FILES["image"]["type"] == "image/pjpeg"))
&& (
$_FILES["image"]["size"] < 200000))
  {
  if (
$_FILES["image"]["error"] > 0)
    {
    echo 
"Return Code: " $_FILES["image"]["error"] . "<br />";
    } else {
    if (
file_exists("../../meetArtists/" $_FILES["image"]["name"]))
      {
          
$filename $_FILES["image"]["name"];
      } else {
      
move_uploaded_file($_FILES["image"]["tmp_name"],
      
"../../meetArtists/" $_FILES["image"]["name"]);
         
$filename $_FILES["image"]["name"];
         }
    }
            
$sql "UPDATE artists SET filename = '$filename' WHERE id = '$id'";
if (!
mysqli_query($link$sql)) {
    
$error 'Error updating selected artist.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();    
    }

  } else {
  echo 
"Invalid file";
  }
    }
include 
$_SERVER['DOCUMENT_ROOT'] . '/../code2/admindb.inc.php';

    
$fname mysqli_real_escape_string($link$_POST['fname']);
    
$lname mysqli_real_escape_string($link$_POST['lname']);
    
$bio mysqli_real_escape_string($link$_POST['elm1']);
    
$id = (int) $id mysqli_real_escape_string($link$_POST['id']);
    if (
$fname == '' || $lname == '') {
        
$error 'You must give the artist a first name and a surname. Click &lsquo;back&rsquo; and try again.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();    
    }
    
$sql "SELECT filename FROM artists WHERE id = $id";
    
$result mysqli_query($link$sql);
    if (!
$result) {
    
$error 'Error updating selected artist.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();
    }
    
$row mysqli_fetch_array($result);
    
$filename $row['filename'];
$sql "UPDATE artists SET
    fname = '
$fname',
    lname = '
$lname',
    filename = '
$filename',
    bio = '
$bio'
    WHERE id = '
$id'";
if (!
mysqli_query($link$sql)) {
    
$error 'Error updating selected artist.';
        include(
$_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php');
        exit();    
    }
    
header('Location: .');
    exit();

Many thanks.

Cliff
cliffgs is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB 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
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Image Reflections in PHP Rendair Advanced PHP Programming 17 11-30-2011 08:41 AM
Create a new transparent image? danceRobot Absolute Beginners 1 08-19-2010 05:57 PM
Basic image link problem. Nor XHTML, HTML, CSS 7 03-25-2008 07:57 PM
The Big GD Guide - Part 2 Rendair Advanced PHP Programming 3 03-07-2008 12:14 AM


All times are GMT. The time now is 10:52 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design