filipposan87 Posted November 20, 2012 Share Posted November 20, 2012 here are 2 functions : this is called upon like upfile('foto1',$_SESSION['userid']) function upfile($file,$path){ $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES[$file]["name"])); if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/png") || ($_FILES[$file]["type"] == "image/pjpeg")) && ($_FILES[$file]["size"] < 2000000) && in_array($extension, $allowedExts)) { if ($_FILES[$file]["error"] > 0) { echo "Return Code: " . $_FILES[$file]["error"] . "<br />"; } else { echo "Upload: " . $_FILES[$file]["name"] . "<br />"; echo "Type: " . $_FILES[$file]["type"] . "<br />"; echo "Size: " . ($_FILES[$file]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES[$file]["tmp_name"] . "<br />"; if (file_exists("usrpics/".$path .'/'. $_FILES[$file]["name"])) { echo $_FILES[$file]["name"] . " already exists. "; } else { move_uploaded_file($_FILES[$file]["tmp_name"], "usrpics/".$path.'/'. $_FILES[$file]["name"]); echo "Stored in: " . "usrpics/" .$path. $_FILES[$file]["name"]; } } } else { echo "Invalid file"; } $_SESSION['path'] ='usrpics/'.$path.'/'.$_FILES["name"] ; return TRUE ; } upload('foto1',$_SESSION['userid']) function upload($file_id,$folder) { $types="jpg,png,gif,jpeg"; $folder = 'usrpics/'.$folder ; if(!$_FILES[$file_id]['name']) return array('','No file specified'); $file_title = $_FILES[$file_id]['name']; //Get file extension $ext_arr = split("\.",basename($file_title)); $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension //Not really uniqe - but for all practical reasons, it is $uniqer = substr(md5(uniqid(rand(),1)),0,5); $file_name = $uniqer . '_' . $file_title;//Get Unique Name $all_types = explode(",",strtolower($types)); if($types) { if(in_array($ext,$all_types)); else { $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any. return array('',$result); } } //Where the file must be uploaded to if($folder) $folder .= '/';//Add a '/' at the end of the folder $uploadfile = $folder . $file_name; $result = ''; //Move the file from the stored location to the new location if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) { $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any. if(!file_exists($folder)) { $result .= " : Folder don't exist."; } elseif(!is_writable($folder)) { $result .= " : Folder not writable."; } elseif(!is_writable($uploadfile)) { $result .= " : File not writable."; } $file_name = ''; } else { if(!$_FILES[$file_id]['size']) { //Check if the file is made @unlink($uploadfile);//Delete the Empty file $file_name = ''; $result = "Empty file found - please use a valid file."; //Show the error message } else { chmod($uploadfile,0777);//Make it universally writable. } } return array($file_name,$result); } the first one returnsvthe route /usrpics/ but lacks the filename and extension, but always prints out img error, regardless of the picture the second one doesnt do anything. it must be a function, it needs to process several photos wich fields are called fotoX(1,2,3,and so on) i just can't figure out what is wrong with these 2 functions. i'd be really grateful if you could tell me what is wrong. PS: im only using 1 function at a time. Quote Link to comment https://forums.phpfreaks.com/topic/270935-file-upload-functions-what-am-i-doing-wrong/ Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 upfile('foto1',$_SESSION['userid']) function upfile($file,$path){ $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES[$file]["name"])); if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/png") || ($_FILES[$file]["type"] == "image/pjpeg")) && ($_FILES[$file]["size"] < 2000000) && in_array($extension, $allowedExts)) { if ($_FILES[$file]["error"] > 0) { echo "Return Code: " . $_FILES[$file]["error"] . "<br />"; } else { echo "Upload: " . $_FILES[$file]["name"] . "<br />"; echo "Type: " . $_FILES[$file]["type"] . "<br />"; echo "Size: " . ($_FILES[$file]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES[$file]["tmp_name"] . "<br />"; if (file_exists("usrpics/".$path .'/'. $_FILES[$file]["name"])) { echo $_FILES[$file]["name"] . " already exists. "; } else { move_uploaded_file($_FILES[$file]["tmp_name"], "usrpics/".$path.'/'. $_FILES[$file]["name"]); echo "Stored in: " . "usrpics/" .$path. $_FILES[$file]["name"]; } } } else { echo "Invalid file"; } $_SESSION['path'] ='usrpics/'.$path.'/'.$_FILES["name"] ; return TRUE ; } upload('foto1',$_SESSION['userid']) function upload($file_id,$folder) { $types="jpg,png,gif,jpeg"; $folder = 'usrpics/'.$folder ; if(!$_FILES[$file_id]['name']) return array('','No file specified'); $file_title = $_FILES[$file_id]['name']; //Get file extension $ext_arr = split("\.",basename($file_title)); $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension //Not really uniqe - but for all practical reasons, it is $uniqer = substr(md5(uniqid(rand(),1)),0,5); $file_name = $uniqer . '_' . $file_title;//Get Unique Name $all_types = explode(",",strtolower($types)); if($types) { if(in_array($ext,$all_types)); else { $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any. return array('',$result); } } //Where the file must be uploaded to if($folder) $folder .= '/';//Add a '/' at the end of the folder $uploadfile = $folder . $file_name; $result = ''; //Move the file from the stored location to the new location if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) { $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any. if(!file_exists($folder)) { $result .= " : Folder don't exist."; } elseif(!is_writable($folder)) { $result .= " : Folder not writable."; } elseif(!is_writable($uploadfile)) { $result .= " : File not writable."; } $file_name = ''; } else { if(!$_FILES[$file_id]['size']) { //Check if the file is made @unlink($uploadfile);//Delete the Empty file $file_name = ''; $result = "Empty file found - please use a valid file."; //Show the error message } else { chmod($uploadfile,0777);//Make it universally writable. } } return array($file_name,$result); } Just helping you get more replies. Quote Link to comment https://forums.phpfreaks.com/topic/270935-file-upload-functions-what-am-i-doing-wrong/#findComment-1393730 Share on other sites More sharing options...
Muddy_Funster Posted November 20, 2012 Share Posted November 20, 2012 it would probably help if we could see a sample vardump of your $_FILES array. Also, have you tried passing in a filename with extension to the upload function? such as upload('foto1.png', $_SESSION['userid']) Quote Link to comment https://forums.phpfreaks.com/topic/270935-file-upload-functions-what-am-i-doing-wrong/#findComment-1393736 Share on other sites More sharing options...
AyKay47 Posted November 20, 2012 Share Posted November 20, 2012 Also, post what error(s) you are receiving. Quote Link to comment https://forums.phpfreaks.com/topic/270935-file-upload-functions-what-am-i-doing-wrong/#findComment-1393777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.