...

View Full Version : upload file problems



king2k5
02-26-2008, 06:50 PM
I have a form that allows you to update your profile on my site. This form contains an Image, About section, and Quote section. The problem i'm having is that once you update, the image upload input goes blank, and if you dont input another file, it gives me an error. This is the code:


$target_path = "avatars/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(isset($_FILES['uploadedfile']['name']) && $_FILES["uploadedfile"]["type"]!="image/gif" && $_FILES["uploadedfile"]["type"]!="image/jpeg"){

$err.= "You must upload either a gif or jpeg image.";

}elseif (file_exists("avatars/" . $_FILES["uploadedfile"]["name"])){

$err .= "This file already exists.";

}elseif($_FILES["uploadedfile"]["size"] > 1000000)){

$err .= "This file is too big.";

}else{

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

mysql_query("UPDATE userinf SET pimage='".$_FILES['uploadedfile']['name']."',pabout='$about',pquote='$quote' WHERE username='".$userinf['username']."'", $db);

$err .= "You have successfully updated your profile!<br>";

} else{
$err .= "There was an error uploading the file, please try again!";
}

}

The problem lies with:


if(isset($_FILES['uploadedfile']['name']) && $_FILES["uploadedfile"]["type"]!="image/gif" && $_FILES["uploadedfile"]["type"]!="image/jpeg"){

Is there a way that i can make it if no file is specified, to just skip the image upload?

oesxyl
02-26-2008, 06:57 PM
sorry, I mess the if condition



if(isset($_FILES['uploadedfile']['name']) && ($_FILES["uploadedfile"]["type"]!="image/gif" || $_FILES["uploadedfile"]["type"]!="image/jpeg")){


a file can't be in the same time, gif and jpeg, you must use || instead of &&


best regards

king2k5
02-26-2008, 07:00 PM
K, changed that. Still getting the same error though

oesxyl
02-26-2008, 07:12 PM
K, changed that. Still getting the same error though



$target_path = "avatars/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(!isset($_FILES['uploadedfile']['name'])){
$err = "You must have a filename";
}elseif(isset($_FILES['uploadedfile']['name']) && $_FILES["uploadedfile"]["type"]!="image/gif" && $_FILES["uploadedfile"]["type"]!="image/jpeg"){

$err.= "You must upload either a gif or jpeg image.";

}elseif (file_exists("avatars/" . $_FILES["uploadedfile"]["name"])){

$err .= "This file already exists.";

}elseif($_FILES["uploadedfile"]["size"] > 1000000)){

$err .= "This file is too big.";

}else{

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

mysql_query("UPDATE userinf SET pimage='".$_FILES['uploadedfile']['name']."',pabout='$about',pquote='$quote' WHERE username='".$userinf['username']."'", $db);

$err .= "You have successfully updated your profile!<br>";

} else{
$err .= "There was an error uploading the file, please try again!";
}

}


try this. I add a !isset before and change the first if in elseif. It's not tested but I think will work.
Post result if not work.

best regards

meth
02-26-2008, 09:02 PM
Give this a whirl:



<?php
//SCRIPT VARS
$err = '';

//process only if $_FILES isset
if(!empty($_FILES)) {

//UPLOAD VARS
$target_path = "avatars/";
$user_fileName = strtolower($_FILES['uploadedfile']['name']);
$arr_formats = array('jpeg','jpg','gif');
$maxFileSize = 1000000;
$format_ok = false;
$file_ok = false;

//check format
foreach($arr_formats as $format) {

if(strpos($user_fileName,$format)) $format_ok = true;

}
if(!$format_ok) $err .= 'You must upload either a gif or jpeg image.<br />';

//check size
if($_FILES["uploadedfile"]["size"] > $maxFileSize) $err .= "This file is too big.<br />";

//proceed if error free
if($err == '') {

//clean filename
$new_fileName = str_replace(' ','_',$user_fileName);
$new_fileName = ereg_replace("[^A-Za-z0-9_.]", '', $new_fileName);

//check for existing file
$fileName_ok = (!file_exists($target_path.$new_fileName)) ? true : false;

//autorename if required
if(!$fileName_ok) {

$limit = 1000;

for($i=1;$i<$limit;$i++) {

$new_fileName = $i.'_'.$new_fileName;
$fileName_ok = (!file_exists($target_path.$new_fileName)) ? true : false;
if($fileName_ok) break;

}

}

if(!$fileName_ok) $err .= 'File exists, please rename your file and upload again.<br />';
//end proceed if error free
}

//proceed if error free
if($err == '') {

$file_ok = move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$new_fileName);

if($file_ok) {

$file_qry = mysql_query("UPDATE userinf SET pimage='$new_fileName',pabout='$about',pquote='$quote' WHERE username='".$userinf['username']."'", $db);

} else {

$err .= 'Unable to process your image upload.<br />';

}

//end proceed if error free
}
//end process if $_FILES
}
?>

oesxyl
02-26-2008, 09:23 PM
Give this a whirl:



<?php
//SCRIPT VARS
$err = '';

//process only if $_FILES isset
if(!empty($_FILES)) {

//end process if $_FILES
}
?>


I'm just curios, why did you check for:



if(!empty($_FILES)) {


best regards

meth
02-26-2008, 11:08 PM
There's no point running the script if the array is empty.

oesxyl
02-26-2008, 11:35 PM
There's no point running the script if the array is empty.

if exists is never empty, I thought you have another reason and I don't see.

thank you for replay and best regards

meth
02-27-2008, 12:10 AM
Yes that's true. I code on autopilot and 9/10 times, I'll use empty() instead of isset(). Just a habit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum