dongwuxie5112 2015-10-14 23:20
浏览 34

多次上传后修改图像

Maybe it's an there are a lot of topics aout this argument, but I've read all of them and I can't find the solution(maybe it's very stupid and I can't see it). I'm trying to modificy(if neccessary) and move the file I've uploaded to a certain dir. The problemi is that I cannot getimagesize info

Here the form

echo' <form enctype="multipart/form-data" action="',$_SERVER['REQUEST_URI'],'" method="post">
<input type="hidden" name="file" value="file_upload" />
<input type="file" multiple="multiple" name="upload_immagini" /><br />
<input type="hidden" value="',$id,'" name="id_prodotto" />
<input type="submit" value="Carica" />
</form>';

$id it's from another page and it works fine. I need to create the directory where I want to move the files after the resize

//UPLOAD
    if(isset($_POST['file']) and $_POST['file']=='file_upload'){
//Set $id
$id=(int)$_POST['id_prodotto'];
$dir='../media/images/prodotti/'.$id;

//Check if something was uploaded
if(!empty($_FILES['upload_immagini'])){
    $d=count($_FILES['upload_immagini']['name']);
    foreach((array)$_FILES['upload_immagini']['name'] as $i => $nome)
    {
        //Check the extension
        $estensione=explode(".", $_FILES['upload_immagini']['name'][$i]);

        if(!$estensione == 'png' || !$estensione == 'jpg' || !$estensione=='jpeg'){
            echo 'Il file ',$_FILES['upload_immagini']['name'][$i],' non è un\'immagine';
        }//If it's an image, I resize it
    else{
        //Info from the original one
            list($width, $height, $type, $attr) = getimagesize($_FILES['upload_immagini']['tmp_name'][$i]);
            //Start Resize
                if($width > $height and $width > 900){
                    $img_width=900;//max wisth
                    $img_height=round(($height/$width)*$img_width);
                }elseif($height > $width and $height > 900){
                    $img_height=900;//max height
                    $img_width=round(($height/$width)*$img_height);
                }
                $img_ridimensionata=imagecreatetruecolor($img_width,$img_height);
                    if($estensione=='png'){
                        $source=imagecreatefrompng($_FILES['upload_immagini']['name'][$i]);
                    }else{
                        $source=imagecreatefromjpeg($_FILES['upload_immagini']['name'][$i]);
                        }
                        //Check the directory existence
                        if (is_dir($dir)) {
                        echo 'Cartella già esistente';
                        } else {
                         mkdir($dir, 0777);
                        imagecopyresized($img_ridimensionata, $source, 0, 0, 0, 0,$img_width, $img_height, $width, $height);
                        imagepng($img_ridimensionata,$dir, 8);

                        imagedestroy($_FILES['upload_immagini']['name'][$i]);
                        }
        }
    }
}

I'm getting those errors:

"Warning: imagecreatetruecolor(): Invalid image dimensions in /home/sakeh0/public_html/sportlife/admin/c/prodotti.php on line 128

Warning: imagecreatefromjpeg(0): failed to open stream: No such file or directory in /home/sakeh0/public_html/sportlife/admin/c/prodotti.php on line 132".

I think because in getimagesize() there's no value.

  • 写回答

1条回答 默认 最新

  • dongwo5589 2015-10-15 21:20
    关注

    I think the problem is the path and the name of the file. If try to print $_FILES['upload_immagini']['tmp_name'][$i] $_FILES['upload_immagini']['name'][$i] I get '/' for the first and '0' for the second. If try to print print_r($_FILES) I get the name, the temporary path, the dimension, ecc...of the file I have uploaded. Where's my mistake inside the foreach(I think it's there)?

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装