1 Replies - 438 Views - Last Post: 22 May 2012 - 10:53 PM Rate Topic: -----

#1 ycpc55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 08

Error handling and image type not working

Posted 22 May 2012 - 10:41 PM

I been working on this image script for too long now, and I still can't seem to get two things to work - the image type and error handling (if the fields are empty). what its doing is still uploading the images no matter what type i upload and for the error handling if i upload one image and leave the other fields empty it will still upload the image other then the image type and the error handling the rest of the script is working any help would be great thanks...

code:
$error_message="";
$MaxSize = "600000";
if (isset($_POST['btn_update'])){
function createRandomPassword() {
$chars = "abcde!@#%^fghijkmnoABCDEFGHIJKpqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWZ!@#%^&";
srand((double)microtime()*10000000);
$i = 0;
$pass = '' ;
while ($i <= 19) {
$num = rand() % 60;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

[color="#FF0000"][b]//START OF NOT WORKING[/b][/color]

if (empty($_FILES) || empty($_FILES['aMyUploads0']) || empty($_FILES['aMyUploads1']) || empty($_FILES['aMyUploads2']))
{
$error_message = "Please fill in att fields.";
}
[b][color="#FF0000"]//END OF CODE THATS NOT WORKING[/color][/b]

if ($_FILES['aMyUploads0']['size'] > $MaxSize || $_FILES['aMyUploads1']['size'] > $MaxSize || $_FILES['aMyUploads2']['size'] > $MaxSize)
{
$error_message = "ERROR: File too big!";

[color="#FF0000"][b]//START OF NOT WORKING[/b][/color]

$allowed_filetypes = array("jpg", "gif", "jpeg", "png");
$ext = pathinfo($aFile['name'], PATHINFO_EXTENSION);
if (!in_array($ext, $allowed_filetypes))
{
$error_message = "Only Jpg/Gif/Png are allowed.";
}
[color="#FF0000"][b]//END OF CODE THATS NOT WORKING[/b][/color]


}
$aMyUploads = array();
$password = createRandomPassword();
foreach($_FILES as $aFile)
{
$newLocation = 'uploads/'.$password .$aFile["name"];
if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation)))
{
$aMyUploads[] = $newLocation;
}
else
{
$aMyUploads[] = '';
}
}
$error_message="Journal successfully saved.";
$connection = mysql_connect("localhost", "????", "???");
mysql_select_db("????", $connection);
$insert = "INSERT INTO photos (image1, image2, image3) VALUES
(
' ".$aMyUploads[0]." ',
' ".$aMyUploads[1]." ',
' ".$aMyUploads[2]." '
)";
$add_member = mysql_query($insert) or die(mysql_error());
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error handling and image type not working

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Error handling and image type not working

Posted 22 May 2012 - 10:53 PM

line #20: $_FILES["field_name"] itself is an array, which is not empty even if there is no file uploaded (you’ll at least have the error number for "no file uploaded").

line #33: check with var_dump() what values you really have (it may be that you have upper-case extensions, for which your test would fail).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1