Jump to content

[SOLVED] Delete successfull uploads if not all upload


emediastudios

Recommended Posts

This is the same file that i had issues with before. With help from this site i fixed the undefined varible.

Now i have the problem that if not all files upload successfully the record is not inserted and i have files in my images folder that cant be overwritten(at least when i get my file exists code implemented) which makes it impossible for my client to upload the news and use these images.

Can i implement a code that deletes (unlinks) the successfull uploaded images if one or two fail.

That way my client can try again.

Or is there a better way to get around this?

Link to comment
Share on other sites

LOL meeen seems like forum helpers build this whole script

any way try

<?php require_once('../Connections/p2w.php');
error_reporting(E_ALL);

//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 
//Writes the photo to the server 
$count = 0;
$file = array();
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = "../news/" . basename( $_FILES[$photo]['name']);
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/png")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")
&& ($_FILES[$photo]["size"] < 1000000)){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){
		$photo="";
	} 
	else{
		$file[] =$_FILES[$photo]['name']; //image name
	}
}
else { 
	//Gives and error if its not 
	++$count;
} 
}
if (count($file) >=1){
foreach($file as $val){
	unlink ("../news/".$val);// delete the file if path is correct
}

}
if ($count > 0){                       ///////////////////////line 42//////////////////
echo $count .'failed to upload ';

}
else
{
//Writes the information to the database 

$host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="5050888202"; // Mysql password 	 
$db_name="preparetowin"; // Database name 
    $tbl_name="news"; // Table name 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">";

}
?>

Link to comment
Share on other sites

<?php require_once('../Connections/p2w.php');
error_reporting(E_ALL);

//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 
//Writes the photo to the server 
$count = 0;
$file = array();
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = "../news/" . basename( $_FILES[$photo]['name']);
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/png")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")
&& ($_FILES[$photo]["size"] < 1000000)){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){
		$photo="";
	} 
	else{
		$file[] =$_FILES[$photo]['name']; //image name
	}
}
else { 
	//Gives and error if its not 
	++$count;
} 
}
if (($count > 0) || (count($file) >=1)){                       ///////////////////////line 42//////////////////
echo $count .'failed to upload ';
if (count($file) >=1){
	foreach($file as $val){
		unlink ("../news/".$val);// delete the file if path is correct
	}

}
}
else
{
//Writes the information to the database 

$host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="5050888202"; // Mysql password 	 
$db_name="preparetowin"; // Database name 
    $tbl_name="news"; // Table name 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">";

}
?>

 

should work

Link to comment
Share on other sites

I have this code in my delete news file

 

<?php
$array =array('photo1','photo2','photo3');
foreach($array as $val){
    $myFile = "../news/". $_POST[$val];
    if (file_exists($myFile)){
        unlink($myFile); 
    }
    else{?>

 

and it works

 

I have this one that i was helped with in this site to execute if all files dont uoload and it doesnt work.

 

<?php
if (($count > 0) || (count($file) >=1)){                     
echo $count .'failed to upload ';
if (count($file) >=1){
	foreach($file as $val){
		unlink ("../news/".$val);// delete the file if path is correct
	}

}
}
else
{?>

can i combine them or somehow intergrate the top workin code with the non working code?

I dont know how.? :)

Thanks for any help.

 

 

 

 

Link to comment
Share on other sites

I have been playing with this all day and i got the successfull uploads to delete if one or more files dont upload,

problem is if they all upload successfully they still get deleted  :D

I know why, cause i got them all in seperate if statements.

How can i take away all the if statements and have it so that if the count is wrong and the file exists then delete em.

 

here is my poor attempt for code.

 

<?php
if (($count > 0) || (count($file) >=1)){                    
echo $count .' file(s) failed to upload, please make sure that all files are of valid size, format and that the file does not exsist.<br />
Please try again. ';
}
if (file_exists("../news/".$photo1)){
unlink ("../news/".$photo1);}
if (file_exists("../news/".$photo2)){
unlink ("../news/".$photo2);}
if (file_exists("../news/".$photo3)){
unlink ("../news/".$photo3);



}
else?>

Link to comment
Share on other sites

<?php require_once('../Connections/p2w.php');
error_reporting(E_ALL);

//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 
//Writes the photo to the server 
$count = 0;
$file = array();
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = "../news/" . basename( $_FILES[$photo]['name']);
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/png")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")
&& ($_FILES[$photo]["size"] < 1000000)){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){
		$photo="";
		$file[] =$_FILES[$photo]['name']; //image name
	} 
	else{
	  ++$count;
	}
}
else { 
	//Gives and error if its not 
	++$count;
} 
}
if ($count > 0){                       ///////////////////////line 42//////////////////
echo $count .'failed to upload ';
if (count($file) >=1){
	foreach($file as $val){
	    if (file_exists("../news/".$val)){
			unlink ("../news/".$val);// delete the file if path is correct
		}
	}

}
}
else
{
//Writes the information to the database 

$host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="5050888202"; // Mysql password 	 
$db_name="preparetowin"; // Database name 
    $tbl_name="news"; // Table name 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">";

}
?>

Link to comment
Share on other sites

Thanks agin

Get these errors now

 

Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30

 

Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30

1failed to upload

Warning: unlink(../news/) [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 46

 

Warning: unlink(../news/) [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 46

Link to comment
Share on other sites

Changed the code you supplied to this below.

<?php
if ($count > 0){                       ///////////////////////line 42//////////////////
echo $count .'failed to upload ';
if (count($file) >=1){
$array =array("$photo1", "$photo2", "$photo3");

	foreach($array as $val){
	    if (file_exists("../news/".$val)){
			unlink ("../news/".$val);// delete the file if path is correct
		}
	}

}
}
else?>

It works :);):D

Just get this error

 

Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30

 

Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30

1failed to upload

 

line 30 is this $file[] =$_FILES[$photo]['name']; //image name

if i delete it the code doesnt work.

I echoed some of the varibles to see what the echoed and $file was array

Think that why it wasnt working before.

Its already defined at the top of my code somewhere

 

 

Link to comment
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.