TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Struggling with image uploads (http://www.talkphp.com/absolute-beginners/5605-struggling-image-uploads.html)

cliffgs 10-14-2010 07:09 AM

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

Village Idiot 10-14-2010 04:22 PM

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

cliffgs 10-15-2010 05:09 AM

Quote:

Originally Posted by Village Idiot (Post 31122)
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 10-16-2010 11:34 PM

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


All times are GMT. The time now is 08:03 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0