Jump to content

Recommended Posts

Hi Everyone

I have this code that works perfectly thanks to alot of help on PHPFreaks.

But there is one problem.

If i upload multible images and (1) or more of those files is not of valid image size, type or the file exists, it still inserts a record.

I want the code to not allow insert record if one or more of the image upload fails.

In a nutshell, If one fails, all fail and dont insert record.

I'll be happy with that.

Code below

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);

// Connects to your Database 
mysql_connect("localhost", "root", "*********") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 

// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop

for($x=0;$x<$uploadNeed;$x++)
{
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
$Valid = false;
if(valid_ext($file_name))
{
	echo "valid ext";
	$Valid = true;
}else{
	echo "invalid Type";
}
echo "<br>";
if(valid_size($Size))
{
	echo "valid size";
}else{
	$Valid = false;
	echo "invalid Size";
}
if($Valid)
{
	// strip file_name of slashes
	$file_name = stripslashes($file_name);
	$file_name = str_replace("'","",$file_name);
	if(file_exists($path . $file_name) ) {
		echo "<br>The file {$file_name} already exists.";
	}else {
		$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);
	}	
}
}


// check if successfully copied

if($copy)
{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
 //Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;
}else{
echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br />
<br />
Please go <a href=\"property_add.php\">back</a> and try again";

}
// end of loop


//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}
?>

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/
Share on other sites

I know its got to be an edit in the area of my code here

if($copy)
{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
 //Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;
}else{
echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br />
<br />
Please go <a href=\"property_add.php\">back</a> and try again";

 

If ($copy)

I need a check like, if $copy all files then return true, else error

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/#findComment-371384
Share on other sites

Right, so you only want to insert this record into the database if all the files they attempt to upload are actually successful? I think what you want to do is increment a counter each time the upload is successful, and compare this to the number of files which were supposed to be uploaded:

 

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);

// Connects to your Database 
mysql_connect("localhost", "root", "*********") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 

// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop
$copied = 0;//the number of files successfully uploaded
for($x=0;$x<$uploadNeed;$x++)
{
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
$Valid = false;
if(valid_ext($file_name))
{
	echo "valid ext";
	$Valid = true;
}else{
	echo "invalid Type";
}
echo "<br>";
if(valid_size($Size))
{
	echo "valid size";
}else{
	$Valid = false;
	echo "invalid Size";
}
if($Valid)
{
	// strip file_name of slashes
	$file_name = stripslashes($file_name);
	$file_name = str_replace("'","",$file_name);
	if(file_exists($path . $file_name) ) {
		echo "<br>The file {$file_name} already exists.";
	}else {
		$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);
		$copied++;//increment our counter
	}	
}
}


// check if successfully copied

if($copied == $uploadNeed)
{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
 //Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;
}else{
echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br />
<br />
Please go <a href=\"property_add.php\">back</a> and try again";

}
// end of loop


//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/#findComment-371393
Share on other sites

On the right path champ  ;):);D

Didnt add the record.

guess thats the question i asked so you solved it  ;)

Only now i have found another problem, it still uploads the valid images, so when my client tries to re-upload after correcting his/her mistake it will return the error (image already exists), can i include in the code that not only (not insert record if 1 of x amount of image fails  to upload) but not to upload anything?

This is the final alteration i need in my code and will be perfected if i can solve it.

Thanks for taking the time to solve my problem, I am extreamly gratefull.

 

;)

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/#findComment-372141
Share on other sites

solved my own question for a change,  :o

I perfected the code, at least for my own requirements anyway  :P

Heres what i did incase anyone wanted to know.

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);

// Connects to your Database 
mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; 
mysql_select_db("gcproperty") or die(mysql_error()) ; 

// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop
$copied = 0;//the number of files successfully uploaded
for($x=0;$x<$uploadNeed;$x++)
{
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
$Valid = false;
if(valid_ext($file_name))
{
	echo "valid ext";
	$Valid = true;
}else{
	echo "invalid Type";
}
echo "<br>";
if(valid_size($Size))
{
	echo "valid size";
}else{
	$Valid = false;
	echo "invalid Size";
}
if($Valid)
{
	// strip file_name of slashes
	$file_name = stripslashes($file_name);
	$file_name = str_replace("'","",$file_name);
	if(file_exists($path . $file_name) ) {
		echo "<br>The file {$file_name} already exists.";
	}else {
		$copied++;//increment our counter
	}	
}
}


// check if successfully copied

if($copied == $uploadNeed)
{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
 //Writes the information to the database

 		$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ;
}else{
echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br />
<br />
Please go <a href=\"property_add.php\">back</a> and try again";

}
// end of loop


//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}
?>

Hope someone can rip some usefull code from this, there has been alot of help from some of the whiz's that are members here on PHPFreaks  and i must say i now owe at least two full days assisting others on PHPFreaks solving there problems.

I cant believe that in such a fucked up society and world that we live in there are still kind caring ppl out there that like to help other ppl out there without asking for something in return.

Insane, i am blown away.

Thanks everyone that has assisted me on my problem(s)

Im stoked beyond words. ;D ;D ;D ;D

 

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/#findComment-372151
Share on other sites

Well, please don't take this wrong, as my intentions are pure at heart... but your site users should not have to do more work than necessary, due to your inability to code something more complicated. Creating a temporary 'tracking' table in MySQL (using IP or user_id) would be able to update the uploader on what has succeeded and what has failed, and allow them to fix just those things. Once all tasks have been satisfied, the record can be removed.

 

Realistically, there are some things that can become too complicated for one script and one MySQL table. Trying to force it that way is bad coding and will certainly end up a bad experience for the end user. I would seriously rethink (in the sense of a flowchart) how this process should proceed, and how to keep the end-user updated on the overall process.

 

Best,

PhREEEk

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/#findComment-372184
Share on other sites

If some of the files were uploaded correctly, why would you unlink them and require the user to re-upload ALL of the files from scratch? Why not report back to them the filenames that failed and allow them to upload only those files?

 

PhREEEk

Thats a good idea, then i would have to insert the record for the successfull files, and then update that record when the other successfull files were uploaded, and then only allow them to send an amount of images that did not let them exceed (9) images in total, as my database only has fields up to (9) images, (so if 5 were uploaded successfully, then the option of 4 images would be displayed) Just means alot of work and i have done heaps already, that would be the ice on the cake to implement what you suggested,the code would be perfected.

Thanks, do you have a Fix? this is beyound my own ability, i'd need assistance.

Id be happy to (if fail all fail) for now and get it up and running, i will look into what you said down the line, unless someone has a easy quick solution.

If there is one  :D

Link to comment
https://forums.phpfreaks.com/topic/73610-insert-record-help/#findComment-373020
Share on other sites

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.