$php_mysql$ Posted August 8, 2011 Share Posted August 8, 2011 friends how can i disable temp image being uploaded to directory if the file field is empty, if users do not upload an image? my current code is uploading 52798461_ type of files in dir function insertit($postData) { global $uploadPath; $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(225,250); $resize_rename = $uploadPath.$randomnum._.str_replace(' ', '_', $_FILES['image']['name']); $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', name = '".$postData['name']."', category = '".$postData['category']."', adtype = '".$postData['ads_type']."', state = '".$postData['state']."', location = '".$postData['ads_location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/ Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 is_uploaded_file Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254164 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 this is my other bit of coding how can i make it to not upload temp images please help me out print 'file name'.$file['name']; if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { echo "The file was uploaded successfully."; return $path; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $file['error'] . ")"; } } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254168 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 any help? Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254190 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 where does $file come from? don't you mean $_FILES ? Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254196 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 1. as webstyles stated, your script will never work since you have $file substituted for the appropriate $_FILES array.. 2. why do you not want to upload a temp image, the image needs to be stored temporarily before it can be modified.. Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254199 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 $file and $_files both doing the same job, my issue is make image upload not necessary, so ifa users wishes to upload an image then file uploads and link to file get stored in database but if users selects no image i want image filed in database to be blank and no 23424234_ files get uploaded to my directory. i hope im not confusing you guys>? Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254225 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 sounds like a simple if else conditional statement is needed..checking if the user has chosen to upload a file or not Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254244 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 i know i have tried but none helping, maybe someone has better idea for my coding? Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254297 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 i don't understand..if a user does not choose a file to be uploaded via the file type input field...no tmp file will be uploaded in the first place.. Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254302 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 dunno but it is being uploaqded, when a user selects an image and submits form the image is uploaded like 34534534_imagename.jpg and it gets saved in DB the same way but if user submits form without image still in DB 234234_ gets saved in image field and a 234234_ file gets created in my image directory. could you please make my code in a better way for i got no idea how to get it fixed. i also need to check file type and restrict also image size. help me out please Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254310 Share on other sites More sharing options...
TeNDoLLA Posted August 8, 2011 Share Posted August 8, 2011 Can't you just check against the $_FILES array values if there is image or not? e.g Check against $_FILES['image']['size'] > 0 or check the $_FILES['image']['error'] if it exists etc. in IF condition. And do not save the data in database if everything is not all right. You can see more info about the upload errors here: http://php.net/manual/en/features.file-upload.errors.php . Pseudo if (image data is good) { // save to db. } else { // dont save and show error or something.. } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254399 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 Can't you just check against the $_FILES array values if there is image or not? e.g Check against $_FILES['image']['size'] > 0 or check the $_FILES['image']['error'] if it exists etc. in IF condition. And do not save the data in database if everything is not all right. You can see more info about the upload errors here: http://php.net/manual/en/features.file-upload.errors.php . Pseudo if (image data is good) { // save to db. } else { // dont save and show error or something.. } did this function insertit($postData) { if($_FILES['image'] == 1){ global $uploadPath; $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(225,250); $resize_rename = $uploadPath.$randomnum._.str_replace(' ', '_', $_FILES['image']['name']); $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', name = '".$postData['name']."', category = '".$postData['category']."', adtype = '".$postData['ads_type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); }else{ } } nothing is getting saved in database Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254417 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 you want if(!empty($_FILES['image']["name"])){ also, is your "uploadfile" and "executeSQL" functions custom? EDIT: edited array Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254426 Share on other sites More sharing options...
TeNDoLLA Posted August 8, 2011 Share Posted August 8, 2011 you want if(!empty($_FILES['image'])){ also, is your "uploadfile" function custom? That will not be empty. It will contain error data in case there was no uploaded file. Actually you could do it with is_uploaded_file() like WebStyles mentioned much earlier. if (is_uploaded_file($_FILES['image']['tmp_name'])) { // Save to db } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254432 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 true sorry about that, however i was under the impression that is_uploaded_file was already attempted here..? Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254433 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 you want if(!empty($_FILES['image']["name"])){ //this is what I had intended.. also, is your "uploadfile" function custom? That will not be empty. It will contain error data in case there was no uploaded file. Actually you could do it with is_uploaded_file() like WebStyles mentioned much earlier. if (is_uploaded_file($_FILES['image']['tmp_name'])) { // Save to db } EDIT: changed post a little Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254435 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 not working still the global $uploadPath; $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(225,250); $resize_rename = $uploadPath.$randomnum._.str_replace(' ', '_', $_FILES['image']['name']); $image->save($resize_rename); unlink($imagepath); //delete the original file gets executed and also i get error file nameThe file was not uploaded successfully.(Error Code:4) Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in C:\wamp\www\scr\image_resize.php on line 14 Warning: imagesx() expects parameter 1 to be resource, null given in C:\wamp\www\scr\image_resize.php on line 52 Warning: imagesy() expects parameter 1 to be resource, null given in C:\wamp\www\scr\image_resize.php on line 55 Warning: imagecopyresampled() expects parameter 2 to be resource, null given in C:\wamp\www\scr\image_resize.php on line 74 Warning: unlink() [function.unlink]: No error in C:\wamp\www\scr\functions\functions.inc.php on line 226 [code] and temp image still gets created in the directory and in DB at image field i find images/83093261_ Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254438 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 you want if(!empty($_FILES['image']["name"])){ //this is what I had intended.. also, is your "uploadfile" function custom? That will not be empty. It will contain error data in case there was no uploaded file. Actually you could do it with is_uploaded_file() like WebStyles mentioned much earlier. if (is_uploaded_file($_FILES['image']['tmp_name'])) { // Save to db } EDIT: changed post a little my is_uploaded_file is in a function like this function uploadFile($file, $uploadPath) { print 'file name'.$file['name']; if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { echo "The file was uploaded successfully."; return $path; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $file['error'] . ")"; } } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254440 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 cool this solved my issue if(!empty($_FILES['image']["name"])){ global $uploadPath; $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(225,250); $resize_rename = $uploadPath.$randomnum._.str_replace(' ', '_', $_FILES['image']['name']); $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', name = '".$postData['name']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); }else{ $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '', name = '".$postData['name']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); } but is this the right way for im executing sql twice Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254465 Share on other sites More sharing options...
TeNDoLLA Posted August 8, 2011 Share Posted August 8, 2011 You are not executing it twice. You execute it once, depending on the IF condition. Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254467 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 my next issue :-) i want to make only jpg, png, and gif to be uploaded and also add blacklisted file types and make only 5mb file upload able, how can i achieve this in this function please help me in this too. function uploadFile($file, $uploadPath) { print 'file name'.$file['name']; if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { echo "The file was uploaded successfully."; return $path; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $file['error'] . ")"; } } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254470 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 $file_type = array("image/gif","image/jpg","image/png"); $image_type = $_FILES["image"]["type"]; if(in_array($image_type,$file_type)){ // correct file type } $max_size = 1048576; $file_size = $_FILES['image']['size']; if($file_size > $max_size){ // too big } incorporate these into your script Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254476 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 this is what im bad with, my if else logic is very bad, i tried this but html file got uploaded with which i was testing function uploadFile($file, $uploadPath) { print 'file name'.$file['name']; if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; $file_type = array("image/gif","image/jpg","image/png"); $image_type = $file["image"]["type"]; if(in_array($image_type,$file_type)){ // correct file type }else{ echo "Wrong file type"; } if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { echo "The file was uploaded successfully."; return $path; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $file['error'] . ")"; } } Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254483 Share on other sites More sharing options...
$php_mysql$ Posted August 8, 2011 Author Share Posted August 8, 2011 someone help with this if else statement. im doing it all wrong Quote Link to comment https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/#findComment-1254550 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.