The Little Guy Posted December 26, 2006 Share Posted December 26, 2006 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 More sharing options...
utexas_pjm Posted December 26, 2006 Share Posted December 26, 2006 I think you might be looking for this: http://www.php.net/manual/en/control-structures.continue.php Link to comment https://forums.phpfreaks.com/topic/31913-foreach-breakskip/#findComment-148090 Share on other sites More sharing options...
emehrkay Posted December 26, 2006 Share Posted December 26, 2006 here is a foolish example (if you want to totally ignore continue)[code=php:0]<?php$a = array(12,34,56,34,546,64567,34563456,21243513245);foreach($a as $key => $value){ if($value < 10000){ echo $value."<br />"; }else{ break; }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31913-foreach-breakskip/#findComment-148092 Share on other sites More sharing options...
The Little Guy Posted December 27, 2006 Author Share Posted December 27, 2006 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 27Here is part of the code (line 27 is the first line.):[CODE]<?phpforeach ($_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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.