Pergunta

Im uploading images using a uploadImage function.

Now Im trying to upload also pdf,making some changes, with same function and not creating other, but Im not having sucess.

Is there any method like imagejpeg() and so on for pdf, so I can do someting like this to upload to my folder:

case 'pdf': filepdf($new_img, $folder.$name); break;

Sure I dont want the resize part and treat the effects of transparency,blending, etc, but I just wanted to take the part of upload the file to the folder but its not working.

function uploadImage($tmp, $name, $width, $folder){
        $ext = substr($name,-3);

        switch($ext){
            case 'jpg': $img = imagecreatefromjpeg($tmp); break;
            case 'png': $img = imagecreatefrompng($tmp); break;
            case 'gif': $img = imagecreatefromgif($tmp); break; 

        }       
        $x = imagesx($img);
        $y = imagesy($img);
        $height = ($width*$y) / $x;
        $new_img = imagecreatetruecolor($width, $height);

        imagealphablending($new_img,false);
        imagesavealpha($new_img,true);
        imagecopyresampled($new_img, $img, 0, 0, 0, 0, $width, $height, $x, $y);

        switch($ext){
            case 'jpg': imagejpeg($new_img, $folder.$name, 100); break;
            case 'png': imagepng($new_img, $folder.$name); break;
            case 'gif': imagegif($new_img, $folder.$name); break;   
        }
        imagedestroy($img);
        imagedestroy($new_img);
    }
Foi útil?

Solução

I haven't tested this code but you could try something like this... It checks for MIME types, which is better than your current method. Might make sense to rename it something like uploadFile rather than uploadImage as well. And the $width variable no longer makes sense for non-image files.

function uploadImage($tmp, $name, $width, $folder) {
    // initialize variables
    $img = $ext = null;

    // supported file types
    $valid_mimes = array('pdf' => 'application/pdf',
                         'jpg' => 'image/jpeg',
                         'png' => 'image/png',
                         'gif' => 'image/gif');

    $finfo = new finfo(FILEINFO_MIME_TYPE);
    if ($ext = array_search($finfo->file($tmp), $valid_mimes, true)) {
        switch($ext) {
            case 'pdf': move_uploaded_file($tmp, $folder.$name); break;
            case 'jpg': $img = imagecreatefromjpeg($tmp); break;
            case 'png': $img = imagecreatefrompng($tmp); break;
            case 'gif': $img = imagecreatefromgif($tmp); break;
        }

        if (isset($img)) {
            $x = imagesx($img);
            $y = imagesy($img);
            $height = ($width*$y) / $x;
            $new_img = imagecreatetruecolor($width, $height);

            imagealphablending($new_img,false);
            imagesavealpha($new_img,true);
            imagecopyresampled($new_img, $img, 0, 0, 0, 0, $width, $height, $x, $y);

            switch($ext) {
                case 'jpg': imagejpeg($new_img, $folder.$name, 100); break;
                case 'png': imagepng($new_img, $folder.$name); break;
                case 'gif': imagegif($new_img, $folder.$name); break;
            }
            imagedestroy($img);
            imagedestroy($new_img);
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top