Jump to content

Recommended Posts

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);

    }

Link to comment
https://forums.phpfreaks.com/topic/244204-stop-temp-image-being-uploaded/
Share on other sites

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'] . ")";
      }
    }

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..

$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>?

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

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..
}

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

 

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
}

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

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_

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'] . ")";

      }

    }

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

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'] . ")";
      }
    }   

$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

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'] . ")";
      }
    }	

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.