Forum Moderators: coopster
Might be worth posting your upload code so we can see if there is a problem. Ideally you shouldn`t do the resize until the image has been confirmed as uploaded. file_exists [uk2.php.net] is an ideal function.
dc
global $file_alerts;
$valid_extensions = array("jpg", "jpeg", "JPG", "JPEG", "gif", "GIF", "png", "PNG");
$file_alerts=array();
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG / gif / png and it's size is less than ?
$filename = basename($_FILES['uploaded_file']['name']);
$ext = getExtension($filename);
if (in_array($ext, $valid_extensions) &&
($_FILES["uploaded_file"]["type"] == "image/jpeg" ¦¦
$_FILES["uploaded_file"]["type"] == "image/jpg" ¦¦
$_FILES["uploaded_file"]["type"] == "image/gif" ¦¦
$_FILES["uploaded_file"]["type"] == "image/png")
&& ($_FILES["uploaded_file"]["size"] < 3000000)) {
//Determine the path to which we want to save this file
$newname = UPLOAD_PATH . $filename;
echo $newname;
//Check if the file with the same name exists
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
array_push ($file_alerts, "It's done! The file has been saved as: ".$newname);
return(true);
} else {
array_push ($file_alerts, "Error: A problem occurred during file upload!");
return(false);
}
} else {
array_push ($file_alerts, "Error: File ".$_FILES["uploaded_file"]["name"]." already exists");
return(false);
}
} else {
array_push ($file_alerts, "Error: Only jpg/gif/png images under 350Kb are accepted for upload");
return(false);
}
} else {
array_push ($file_alerts, "Error: No file uploaded");
return(false);
}
}
Otherwise, the code looks good.
Three tiny advices:
1/ You can try to code the all thing using a class and not a function... Easier to reuse again and again. PM me if you want to see my custom class.
2/ You are checking if there is file. You should also check that path/directory exists (you never know).
3/ If it is a public upload - meaning anyone can upload - then you should limit the number of upload (per session, IP, etc.)
Tomda