多次上传后修改图像

时间:2015-10-14 23:20:20

标签: php forms upload

也许这是一个关于这个论点的很多主题,但我已经阅读了所有这些,我找不到解决方案(也许它非常愚蠢,我看不到它)。 我正在尝试修改(如果需要)并将我上传的文件移动到某个目录。问题是我不能格式化信息

这里是表格

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来自另一个页面,它运行正常。我需要在调整大小后创建我想要移动文件的目录

//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]);
                        }
        }
    }
}

我收到了这些错误:

"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".

我认为因为getimagesize()没有价值。

1 个答案:

答案 0 :(得分:0)

我认为问题是文件的路径和名称。如果尝试打印 $_FILES['upload_immagini']['tmp_name'][$i] $_FILES['upload_immagini']['name'][$i] 第一个得到'/',第二个得到'0'。如果尝试打印 $_FILES['upload_immagini']['tmp_name'][$i] $_FILES['upload_immagini']['name'][$i]  我得到了我上传的文件的名称,临时路径,尺寸,ecc .... 在foreach中我的错误在哪里(我认为它在那里)?