[英]Foreach array with different values
Need little help, i get always stupid errors, when i try to do this, i want to foreach array, and inside that do a for loop, is this possible? 需要很少的帮助,我总是会遇到愚蠢的错误,当我尝试执行此操作时,我想使用foreach数组,并且在其中执行for循环,这可能吗? Here is my code
这是我的代码
$image = new SimpleImage();
$slike=array($_FILES['slika1']['tmp_name'],$_FILES['slika2']['tmp_name'],$_FILES['slika3']['tmp_name'],$_FILES['slika4']['tmp_name']);
$koliko = count($slike);
foreach ($slike as $slika) {
for ($i = 1; $i <= $koliko; $i++)
{
$slicica="../images/usluge/".mysql_insert_id()."-".$i.".jpg";
$image->load($slika);
$image->resizeToWidth(800);
$image->save($slicica);
}
}
I want to get new pictures with this name, example.If last id was 2 i want to get this filenames etc 我想用这个名称来获取新图片,例如。如果最后一个id是2,我想获取该文件名等
Still with my code i dont get correct values :( Or maybe there is easy way to do that :) 仍然用我的代码我没有得到正确的值:(也许有简单的方法做到这一点:)
I think what you need is to replace your following line: 我认为您需要替换以下行:
$image->load($slika);
for this one: 为此:
$image->load($slike[$i-1]);
you probably have a problem in you foreach as if we do a var_dump on an array we will see the following : 您可能在foreach中遇到问题,就像我们在数组上执行var_dump一样,我们将看到以下内容:
array(3) { [0]=> string(5) "test1" [1]=> string(4) "tes2" [2]=> string(5) "test3" }
so you need to change it to : 因此您需要将其更改为:
foreach ($slike as $key => $slika) {
you can also use move_uploaded_file to achieve what you are trying to do and use linux command convert : 您还可以使用move_uploaded_file实现您要执行的操作,并使用linux命令convert :
foreach ($slike as $key => $slika) {
for ($i = 1; $i <= $koliko; $i++)
{
$slicica="../images/usluge/".mysql_insert_id()."-".$i.".jpg";
if(move_uploaded_file(slika,slicica)){
exec("convert $slicica -resize 200 $slicica");
}else{die('problem loading the file');}
}
}
$i=1;
// insert into mysql table..
// [...]
$mysqlId = mysql_insert_id();
foreach ( $_FILES as $key => $value) {
$slicica="../images/usluge/".$mysqlId."-".$i.".jpg";
if(move_uploaded_file($_FILES[$key]["tmp_name"],slicica)){
$image->load($slicica);
$image->resizeToWidth(800);
$image->save($slicica);
}
else {
// error handling
}
$i++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.