Jump to content

Archived

This topic is now archived and is closed to further replies.

lyntaff

PHP file upload problem

Recommended Posts

I have a very annoying problem while running a script I created about 2 years ago.

I have a basic script which uploads a file to a server path and information to a database. But evertime I execute without a file it doesn't work. I should get an error no.4 but for some reason an IF statement that checks the error gets bypassed until the scipt dies for being a wrong file type. Can anybody understand whats wrong?

The code is this:
[size=8pt]
[color=green]

if ($_FILES['image1']['error']=="4")
  {
  mysql_query("UPDATE cars SET exterior_image = 'np.gif' WHERE car_id = $image_id");
  echo "<div align='center'>The Car details have been added to the database successfully. The Cars Reference Number is: ".$image_id."<br><br>
  <a href='addcar.php'>Continue</a></div>";  
exit;
  }

if ($_FILES['image1']['size']=="50000")
  {
  echo "One of the uploaded  images is to big, it must be under 50k. Please go back and try again";
  exit;
  }
 
if ($_FILES['image']['type'] != "image/gif" && $_FILES['image1']['type'] != "image/pjpeg" && $_FILES['image1']['type'] != "image/jpeg" && $_FILES['image']['type'] != "image/jpg")
  {
    die("<strong>Problem:</strong> Image 1  is not a valid image file, please go back and try again.<BR>Image Type: ".$_FILES['image1']['type']);
    exit; 
  }


if ($_FILES['image1']['type'] == "image/gif") {
  $ext = ".gif";
} else if ($_FILES['image1']['type'] == "image/pjpeg") {
  $ext = ".jpg";
}  else if ($_FILES['image1']['type'] == "image/jpeg") {
  $ext = ".jpg";
}  else if ($_FILES['image1']['type'] == "image/jpg") {
  $ext = ".jpg";
}

$upfile = "/home/sites/alanevansmotors.co.uk/public_html/carpics/".$image_id . $ext; // $image_id being the id of the newly inserted member

if (!move_uploaded_file($_FILES['image1']['tmp_name'], $upfile))
  {
  echo "<strong>Problem:</strong> Could not upload image onto server";
  }

mysql_query("UPDATE cars SET exterior_pic = '$image_id"."$ext' WHERE car_id = $image_id");[/color][/size]

Share this post


Link to post
Share on other sites
You could try using something along the lines of
if(!isset($_FILES['image1']['name'])){
  error stuff here...
}

That should, in theory, prevent uploading of files without names (in other words... not uploading anything).

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.