Jump to content

Problems with Image Upload script


fRAiLtY-

Recommended Posts

Hi guys,

 

I've got an image uploader based on Uploadify, first let me set out what I want the script to do and what it currently does/doesn't do:

 

[*]Upload files of various types into folders named with the Order ID - OK

[*]Process the files by checking the file type and acting accordingly - OK

[*]Create thumbnails of the images in the /thumbs subdirectory - OK

[*]If the file is a PDF, create JPEG images of all pages and subsequent thumbnails - OK

[*]If the file is a ZIP, unzip and re-process the contents based on the above rules - FAIL

 

As you can see I'm nearly at the end of my wishlist with this script, currently when it receives a ZIP file is unzip's it correctly placing the contents in the parent (Order-ID) directory however I'm then left with the same situation I have at the very beginning of the script, for example:

 

File in -> "What kind of file are you?" -> Process file based on the answer.

 

What I need to do is:

 

File in -> "What kind of file are you? Oh! a Zip File, let's unzip and ask the same question of the contents!" -> Process contents.

 

So basically I need to ask the same question twice, once of the zip and once of the contents of the unzipped file. This is where I'm struggling, someone told me to use a function which I tried but it kept breaking and I wasn't sure why. Therefore I have attached 2 files. 1 which currently works 99% except for "asking the question again" and one which doesn't work but I was told is the right way to do it  :-\

 

File 1: Works 99%.

 



<?php
/*
TomBigFile v2.0
Based on Uploadify v2.1.4

Automatic folder creation dependant on order number with a thumbnail subdirectory.
All files in are subject to a process which determines their type and acts
accordingly. PDF's are automatically converted on the fly to 2 sets of JPEG's and
stored accordingly to be accessed by Customer Accounts and Sales Order details area.

Revised May 2011
*/

// RECEIVE THE DATA FROM THE UPLOAD AND ASSIGN THE VARIABLES
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

// $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes  = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts  = pathinfo($_FILES['Filedata']['name']);

// if (in_array($fileParts['extension'],$typesArray)) {

// CREATE THE PARENT DIRECTORY BASED ON ORDER ID AND THE THUMBS SUBDIRECTORY
mkdir(str_replace('//','/',$targetPath), 0755, true);
mkdir($targetPath . "thumbs", 0755, true); // create thumbs dir

move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
 
}

// BEGIN CHECKING THE IMAGE LOOKING AT THE FILE TYPE AND ACTING ACCORDINGLY
$imgsize = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
    case "pdf":
        $large = substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
$thumbnail = dirname($large).'/thumbs/'.basename(substr($large, 0, -4)."_thumb".strtolower(substr($large, -4)));
$cmd = "$targetFile -write $large -thumbnail 64x64 $thumbnail ";
exec("convert $cmd ");
exit;
    break;
    case "jpg":
        $image = imagecreatefromjpeg($targetFile);   
    break;
    case "png":
        $image = imagecreatefrompng($targetFile);
    break;
    case "gif":
        $image = imagecreatefromgif($targetFile);
    break;
    case "zip":
        $zip = new ZipArchive(); $zip->open($targetFile); $zip->extractTo($targetPath); $zip->close();
    break;
    default:
        exit;
    break;
}

$width = 60; //New width of image   
$height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions

$src_w = $imgsize[0];
$src_h = $imgsize[1];
   

$picture = imagecreatetruecolor($width, $height);
imagealphablending($picture, false);
imagesavealpha($picture, true);
$bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);

// SAVE THE IMAGE(S)
if($bool){
    switch(strtolower(substr($targetFile, -3))){
        case "jpg":
            //header("Content-Type: image/jpeg");
            $bool2 = imagejpeg($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
        break;
        case "png":
            //header("Content-Type: image/png");
            imagepng($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
        break;
        case "gif":
            //header("Content-Type: image/gif");
            imagegif($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
        break;
    }
}

imagedestroy($picture);
imagedestroy($image);

echo '1'; // Important so upload will work on OSX

?>

 

File 2: Doesn't work for me but I was told is correct method(s).

 

<?php
/*
TomBigFile v2.0
Based on Uploadify v2.1.4

Automatic folder creation dependant on order number with a thumbnail subdirectory.
All files in are subject to a process which determines their type and acts
accordingly. PDF's are automatically converted on the fly to 2 sets of JPEG's and
stored accordingly to be accessed by Customer Accounts and Sales Order details area.

Revised May 2011
*/

if (!empty($_FILES)) {
        $tempFile = $_FILES['Filedata']['tmp_name'];
        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
        $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
       
        // $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
        // $fileTypes  = str_replace(';','|',$fileTypes);
        // $typesArray = split('\|',$fileTypes);
        // $fileParts  = pathinfo($_FILES['Filedata']['name']);
       
        // if (in_array($fileParts['extension'],$typesArray)) {
                // Uncomment the following line if you want to make the directory if it doesn't exist
                 mkdir(str_replace('//','/',$targetPath), 0755, true);
                 mkdir($targetPath . "thumbs", 0755, true); // create thumbs dir
                       
                move_uploaded_file($tempFile,$targetFile);
                echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
        // } else {
        //      echo 'Invalid file type.';
        // }
           
}

// start again here after ZIP!!!!
function fileScanner($targetFile, $targetPath)
        {
                $imgsize = getimagesize($targetFile);
                $width = 60; //New width of image    
                $height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions
               
                $src_w = $imgsize[0];
                $src_h = $imgsize[1];
                       
               
                $picture = imagecreatetruecolor($width, $height);
                imagealphablending($picture, false);
                imagesavealpha($picture, true);
                $bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
               
                if($bool){
                        switch(strtolower(substr($targetFile, -3))){
                                case "jpg":
                                        //header("Content-Type: image/jpeg");
                                        $bool2 = imagejpeg($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
                                break;
                                case "png":
                                        //header("Content-Type: image/png");
                                        imagepng($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
                                break;
                                case "gif":
                                        //header("Content-Type: image/gif");
                                        imagegif($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
                                break;
                        }
                }
               
                imagedestroy($picture);
                imagedestroy($image);
        }
       
$imgsize = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
    case "pdf":
        $large = substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
                $thumbnail = dirname($large).'/thumbs/'.basename(substr($large, 0, -4)."_thumb".strtolower(substr($large, -4)));       
                $cmd = "$targetFile -write $large -thumbnail 64x64 $thumbnail ";
                exec("convert $cmd ");
                exit;
    break;
    case "jpg":
        $image = imagecreatefromjpeg($targetFile);    
    break;
    case "png":
        $image = imagecreatefrompng($targetFile);
    break;
    case "gif":
        $image = imagecreatefromgif($targetFile);
    break;
    case "zip":
        $zip = new ZipArchive(); $zip->open($targetFile); $zip->extractTo($targetPath); $zip->close();
        fileScanner($targetFile, $targetPath);
    break;
    default:
        exit;
    break;
}

fileScanner($targetFile, $targetPath);

// $width = 60; //New width of image    
// $height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions
//
// $src_w = $imgsize[0];
// $src_h = $imgsize[1];
//    
//
// $picture = imagecreatetruecolor($width, $height);
// imagealphablending($picture, false);
// imagesavealpha($picture, true);
// $bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
//
// if($bool){
//     switch(strtolower(substr($targetFile, -3))){
//         case "jpg":
//             //header("Content-Type: image/jpeg");
//             $bool2 = imagejpeg($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
//         break;
//         case "png":
//             //header("Content-Type: image/png");
//             imagepng($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
//         break;
//         case "gif":
//             //header("Content-Type: image/gif");
//             imagegif($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
//         break;
//     }
// }
//
// imagedestroy($picture);
// imagedestroy($image);

echo '1'; // Important so upload will work on OSX

?>

 

If anyone could help me with this I'd greatly appreciate it, I've been reading so many guides on functions and arguments my brain is fried and I fear I've strayed from the subject of what I want to do!

 

Many thanks.

Link to comment
https://forums.phpfreaks.com/topic/237200-problems-with-image-upload-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.