Jump to content

Recommended Posts

Ok so I have an image upload form that validates files. I want to get the value named $error_message out of the if statement so i can post it to the screen. I have to do this because the header script at the start of my script makes it so the errors that are in the die function don't display at all. I had it working with sessions but that quickly got scrapped because after a while the script would stop uploading pictures.

 


if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) {
	$error_message = "file is too big"
    die("<BR><BR>Error: Upload file size too large: (<b>" . $_FILES['userfile']['size'] . "</b>). Must not exceed ".$max_size."kb.");
	}


     $array = explode(".", $_FILES['userfile']['name']); 
     $nr    = count($array); 
     $ext  = $array[$nr-1];
     if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg")) 
	$error_message = "extension is wrong"
     die("<BR><BR>Error: file extension un-recognized. Be sure your image follows the correct extension (.JPG or .PNG)");

if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png")) {
	$error_message = "extension is wrong"
die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
}


if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) {
	$error_message = "extention is wrong"
die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
}

 

 

 

I'm not sure what you wish to do with the variable but everything within the if block is available outside the if block as well so you may do whatever you wish with the error message.

 

but when I echo $error_message it does not return anything. it is just left blank even when i upload a gif or a file that is too large.

but when I echo $error_message it does not return anything. it is just left blank even when i upload a gif or a file that is too large.

 

Probably because the script is told to die right after you set $error_message ;)

 

If I was to remove the die() it would make it so people can upload anything to the server. Is there anyway I can make it so I can keep the die but still return the error message on a case by case bases?

 

 

funnily enough even with the die commands removed it still does not print out the message :( It's been messed up ever since I redirected the page with header: index.php and i only did that because the form kept submitting the same image to the server when it was refreshed.

Something like:

<?php

$error_message = array();

if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) {
$error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb.";
}

$array = explode(".", $_FILES['userfile']['name']); 
$nr    = count($array); 
$ext   = $array[$nr-1];

// I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much.
if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && 
($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && 
($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && 
($info['mime'] != "image/png")) {
$error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed.";
}

if(count($error_message)>0) {
// we had errors:
foreach($error_message as $message) {
	echo '<strong>ERROR:</strong> '.$message.'<br>';
}

// die, exit, or show form again. whatever you want here.


} else {
// we didn't have errors, continue with uploading

// ...
}
?>

Something like:

<?php

$error_message = array();

if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) {
$error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb.";
}

$array = explode(".", $_FILES['userfile']['name']); 
$nr    = count($array); 
$ext   = $array[$nr-1];

// I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much.
if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && 
($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && 
($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && 
($info['mime'] != "image/png")) {
$error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed.";
}

if(count($error_message)>0) {
// we had errors:
foreach($error_message as $message) {
	echo '<strong>ERROR:</strong> '.$message.'<br>';
}

// die, exit, or show form again. whatever you want here.


} else {
// we didn't have errors, continue with uploading

// ...
}
?>

 

 

thanks for taking the time to write this up.

Something like:

<?php

$error_message = array();

if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) {
$error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb.";
}

$array = explode(".", $_FILES['userfile']['name']); 
$nr    = count($array); 
$ext   = $array[$nr-1];

// I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much.
if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && 
($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && 
($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && 
($info['mime'] != "image/png")) {
$error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed.";
}

if(count($error_message)>0) {
// we had errors:
foreach($error_message as $message) {
	echo '<strong>ERROR:</strong> '.$message.'<br>';
}

// die, exit, or show form again. whatever you want here.


} else {
// we didn't have errors, continue with uploading

// ...
}
?>

 

 

thanks for taking the time to write this up

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.