Help with logic in an if else statement form for image uploads.

The php script I’m attempting to use is :

[php]
function getExtension($str) {
$i = strrpos($str,".");
if (!$i){return “”;}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;}
$errors = 0;
if( isset( $_POST[‘submit’] ) ){
$image = $_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
if ($image){
$filename = stripslashes($_FILES[‘file’][‘name’]);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != “jpg”) || ($extension != “jpeg”) || ($extension != “png”) || ($extension != “gif”)) {
// Do something
$errors = 1;}
$size=filesize($_FILES[“file”][“tmp_name”]);
else if ($size > 60000){
// Do something else
$errors = 1;}
else if(file_exists(BIO_IMAGES_PATH . $_FILES[“file”][“name”])){
// Do something else
$errors = 1;}
//If no errors registred
else { $errors = 0;
if ($extension == “jpg” || $extension == “jpeg” || $extension == “jpeg”){
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefromjpeg($uploadedfile);}
else if ($extension ==“png”){
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefrompng($uploadedfile);}
else {
$src = imagecreatefromgif($uploadedfile);}
list($width,$height)=getimagesize($uploadedfile);
$newwidth = 290;
$newheight = 190;
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = BIO_IMAGES_PATH . $_FILES[“file”][“name”];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// Do something else
}
}
}
[/php]

The html form is as follows:

<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" autofocus /> 
<button type="submit" name="submit">Submit</button>
</form>

I’m actually a noob in php, but i master css and html. I’ve been learning php for over three years on my own and can accomplish some things but when I attempt to get deeper I find myself lost. Any help would be greatly appreciative and would help my learning to master php. I’ve only been working on my local server under localhost. The script here as it stands errors out. I’ve been working on this simple form for over a week. I can get this script to work half way, meaning successful upload and resize but scripts for errors fail except the if file_exists. The other error scripts never execute.

you don’t need all that code just to get the file’s extension, its already contained in $_FILES[‘file’][‘type’]. This is how i usually do that stuff
[php]
$permitted = array(‘image/jpeg’, ‘image/pjpeg’);

foreach ($permitted as $img_type) {
if ($img_type == $_FILES[‘pic’][‘type’]) {
$typeOK = true;
break;
}
}[/php]

You need to work on using indents to clean up the code, so you can see what’s what. I noticed you’re not using { } on your if statements, if you don’t, you have to have a line between them, otherwise, it thinks is just a bunch of nested statements. For me, i never do it, but its not wrong to do it that way, just makes it difficult to track down errors, like what you’re having.

Other than that, what are the errors you’re getting?

The problem that I’m having is when I submit a file it takes any file type and not the ones I want allowed. So the script I want to run on that error is not executing and the upload executes, which I don’t want. Also image size error is not executing as well, it takes any sizes The only one that will execute is if image_exists. I need my scripts for each error to execute. Not a general fix but a script to run differently for each error. One for allowed file types, another for file size, and of course the one that does execute - if image_exists. The current script shown errors out completely. The script below works as already stated above.

[php]
function getExtension($str) {
$i = strrpos($str,".");
if (!$i){return “”;}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;}
$errors = 0;
if( isset( $_POST[‘submit’] ) ){
$image = $_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
if ($image){
$filename = stripslashes($_FILES[‘file’][‘name’]);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != “jpg”) || ($extension != “jpeg”) || ($extension != “png”) || ($extension != “gif”)) {
// Do something
$errors = 1;}
$size=filesize($_FILES[“file”][“tmp_name”]);
if ($size > 60000){
// Do something else
$errors = 1;}
if(file_exists(BIO_IMAGES_PATH . $_FILES[“file”][“name”])){
// Do something else
$errors = 1;}
//If no errors registred
else { $errors = 0;
if ($extension == “jpg” || $extension == “jpeg” || $extension == “jpeg”){
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefromjpeg($uploadedfile);}
else if ($extension ==“png”){
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefrompng($uploadedfile);}
else {
$src = imagecreatefromgif($uploadedfile);}
list($width,$height)=getimagesize($uploadedfile);
$newwidth = 290;
$newheight = 190;
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = BIO_IMAGES_PATH . $_FILES[“file”][“name”];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
$results[‘imgLoaded’] = $_FILES[“file”][“name”];
// Do other things

}}}
[/php]

I’m sorry in the way I write php. I’m writing it as if it were css but I find it more elegible and it has become a habit. I do apologize for that.

I’ve just come up with this on on your suggestions and am not exactly sure how to implement it. The php :

[php]
$allowedExts = array(“image/jpg”, “image/jpeg”, “image/pjpeg”, “image/png”, “image/gif”);
foreach ($allowedExts as $img_type) {
if ($img_type == $_FILES[“file”][“type”]) {
$typeOK = true;
break;
}}
[/php]

[php]
if(isset( $_POST[‘submit’] ) ){
$image = $_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
if ($image){
$filename = stripslashes($_FILES[‘file’][‘name’]);
$allowedExts = array(“image/jpg”, “image/jpeg”, “image/pjpeg”, “image/png”, “image/gif”);
foreach ($allowedExts as $img_type) {
if ($img_type == $_FILES[“file”][“type”]) {
$typeOK = true;
break;
} else {
$typeOK = false;
$error[] = “Wrong file type!”;
}
}
} else {
$error[] = “File not selected!”;
}
// reset of code
}
[/php]

I added in some error messages so you could see where to put them.

Thanks alot for the help richei, it’s much appreciated. I’m no longer allowing for other image formats other than jpeg images. I actually tried all types of stuff and finally came up with something not too long ago. The php :

[php]
function getExtension($str) {
$i = strrpos($str,".");
if (!$i){return “”;}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
if (isset($_POST[‘submit’])) {
$filetype = $_FILES[“file”][“type”] == “image/jpeg” || $_FILES[“file”][“type”] == “image/pjpeg”;
if (!$filetype) {
//Do something
}
else {
$image = $_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$filename = stripslashes($_FILES[‘file’][‘name’]);
$extension = getExtension($filename);
$extension = strtolower($extension);
if ($image && $filetype) {
if (file_exists(BIO_IMAGES_PATH . $_FILES[“file”][“name”])) {
//Do something
}
//If no errors registered
else {
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth = 290;
$newheight = 190;
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = BIO_IMAGES_PATH . $_FILES[“file”][“name”];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
$results[‘imgLoaded’] = $_FILES[“file”][“name”];
$results[‘statusMessage’] = "The image “. $_FILES[“file”][“name”] . " has been added to the server. Continue adding more images as necessary or until you have added all images before proceeding to edit barber(s). If you are done adding images proceed to edit barber(s). Link to the form is in your admin panel located above.”;
}
}
}
}
[/php]

This is working as I want but is the way your showing me better in any way, as far as being faster or lighter on the server. I’m really interested in knowing as I want to master php or close to mastering it anyways. Once again thanks a lot for your time.

Its not better persay, but its definitely lighter since you’re taking advantage of what’s already there instead of wasting code trying to find the same thing. All you had to do was explode the file name on the period and use the second part. All your code could be condensed down to
[php]function getExtension($str) {
$tmp = explode(’.’, $str);
return $tmp[1];
}[/php]

Basically, it puts each part of the string into an array. by telling it to use just part 2 ([1]), you’re not allowing for files like pic.png.exe. Your code would return png.exe, which would error your code since you’re not looking for it.

Your $filetype is invalid too. Needs to be converted to an if statement. You’re not comparing anything either, so all its doing is using what’s under else. You need to make sure the file type and size are correct before you do anything else.

I went ahead and went with the code you provided and it works great. I do however have a problem with some jpeg files coming out all black. I’m currently looking this up though to find why this is happening. It is only happening to jpeg files with caps like this JPG or JPEG but not all of them some still work as expected. I even have a situation where some jpeg files with caps (JPG or JPEG) are considered a file type not excepted. It’s a weird situation if you ask me. It’s like some are excepted and it works as planned (resized and stored) and some are excepted (resized and stored) but the image is all black and then others are not excepted at all (wrong file type). This is only an issue with jpeg’s that are with caps. I guess I’ll have to fish around for others who may have been having this problem as well and how they resolved it. I know for sure this has nothing to do with the script you provided as it was happening prior to using the script. Thanks a bunch for your help richei, I really do appreciate this very much.

You could try making all ext lowercase with the function strtolower() see if that makes a difference.

@Noodles - I figured what the problem was. The issue was that those images were to large in file size to handle on the server so the wrong file type error was occurring. I just changed the file type error to include that if the file type was correct than the image file was too large in file size. I guess later I’ll work something out where the error won’t be shared but more specific. If you have any suggestions on that I’ll give it a try though (as far as implementing it with the script as how richei posted). Thanks for taking the time on your suggestion anyways, always appreciated to receive help.

Sponsor our Newsletter | Privacy Policy | Terms of Service