Jump to content

Foreach Break/Skip


The Little Guy

Recommended Posts

I have a foreach loop for uploading files, but... I have a file space allowance, that only allows users to upload a certain MB of files to their account (Currently it is 10MB) I would like the loop not to break if they go over, but to skip everything in the loop, go to the end, and start with the next file, and check the size to see if that one can be uploaded if the space isn't exceeded. I hope this makes sense.
Link to comment
https://forums.phpfreaks.com/topic/31913-foreach-breakskip/
Share on other sites

This is a different question, but it seems for some reason, that I get an error if a file is too large, otherwise my loop works fine.

Warning: Invalid argument supplied for foreach() in C:\Program Files\xampp\htdocs\tzfiles\upload_files.php on line 27

Here is part of the code (line 27 is the first line.):
[CODE]
<?php
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$sql_size1 = mysql_query("SELECT SUM(size) FROM files WHERE owner_id='{$_SESSION['id']}'");
$sum = mysql_fetch_array($sql_size1);
$tmp_name = $_FILES["file"]["tmp_name"][$key];
$name = $_FILES["file"]["name"][$key];
$name = strtolower($name);
$file = getext($name);
$image = 'sessions/'.$_SESSION[user].'/'.$row[file_name];
$size = GetImageSize($tmp_name);
$width = $size[0];
$height = $size[1];
$filesize = filesize($tmp_name);
$max = $filesize+$sum['SUM(size)'];
if($max>$space_amount){
continue;
//$_SESSION['exceed_space'] = '<span class="redtxt"><b>Sorry, but you can\'t upload all of the choosen files</b></span>';
}
$time = time();
#1 equals overwrite
if($_POST['write'] == 1 && file_exists(strtolower($name))){
if(in_array($file,$img_types)){
move_uploaded_file($tmp_name, "users/$_SESSION[user]/".strtolower($name));
if($width < 120){
createThumbnail("users/$_SESSION[user]", strtolower($name), "users/$_SESSION[user]/thumbs", $width, 80);
}else{
createThumbnail("users/$_SESSION[user]", strtolower($name), "users/$_SESSION[user]/thumbs", 120, 80);
}
}else{
move_uploaded_file($tmp_name, "users/$_SESSION[user]/".strtolower($name));
}
}
#2 equals rename new file
elseif($_POST['write'] == 2 && file_exists(strtolower($name))){
if(in_array($file,$img_types)){
$name = time().$name;
move_uploaded_file($tmp_name, "users/$_SESSION[user]/".strtolower($name));
if($width < 120){
createThumbnail("users/$_SESSION[user]", strtolower($name), "users/$_SESSION[user]/thumbs", $width, 80);
}else{
createThumbnail("users/$_SESSION[user]", strtolower($name), "users/$_SESSION[user]/thumbs", 120, 80);
}
}else{
move_uploaded_file($tmp_name, "users/$_SESSION[user]/".strtolower($name));
}
mysql_query("INSERT INTO files(`file_name`, `size`, `owner_id`, `date`) VALUES ('".strtolower($name)."', '$filesize', '$_SESSION[id]', '$time')")or die(mysql_error());
}else{
if(in_array($file,$img_types)){
move_uploaded_file($tmp_name, "users/$_SESSION[user]/".strtolower($name));
if($width < 120){
createThumbnail("users/$_SESSION[user]", strtolower($name), "users/$_SESSION[user]/thumbs", $width, 80);
}else{
createThumbnail("users/$_SESSION[user]", strtolower($name), "users/$_SESSION[user]/thumbs", 120, 80);
}
}else{
move_uploaded_file($tmp_name, "users/$_SESSION[user]/".strtolower($name));
}
mysql_query("INSERT INTO files(`file_name`, `size`, `owner_id`, `date`) VALUES ('".strtolower($name)."', '$filesize', '$_SESSION[id]', '$time')")or die(mysql_error());
}
}
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/31913-foreach-breakskip/#findComment-148099
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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