views:

230

answers:

3

I ran the code in apache localhost and also tried in my host. In both of them, the method moved the file but the file seemed 0kb.

Here is the code:

if(isset($_POST['upload'])){

    if($_FILES['profile_foto']['size']>0&&$_FILES['profile_foto']['size']<102400){

        $image_extension=explode("/",$_FILES['profile_foto']['type']);

        if($image_extension[1]!="jpeg")
            echo "<script type='text/javascript'>alert('The extension of the profile image must be jpeg!!!')</script>";

        else{

            if($_POST['image_id']==""){

                $image_id= md5(uniqid());

                $_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";

                move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);

                list($width,$height)=getimagesize("tempo/".$image_id.".jpg");

                if($width<=0||$width>170||$height<=0||$height>200){

                    $myFile="tempo/".$image_id.".jpg";
                    $fh=fopen($myFile,'w') or die("The File could not be opened!!!");
                    fclose($fh);
                    unlink($myFile);

                    echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";

                }
                else
                    $_POST['image_id']=$fotograf_id;

            }
            else{

                $image_id= md5(uniqid());

                $_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";

                move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);

                list($width,$height)=getimagesize("tempo/".$image_id.".jpg");

                if($width<=0||$width>170||$height<=0||$height>200){

                    $myFile="tempo/".$image_id.".jpg";
                    $fh=fopen($myFile,'w') or die("The File could not be opened!!!");
                    fclose($fh);
                    unlink($myFile);

                    echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";

                }
                else{
                    $image_will_be_deleted=$_POST['image_id'];

                    $myFile="tempo/".$image_will_be_deleted.".jpg";
                    $fh=fopen($myFile,'w') or die("The File cannot be opened!!!");
                    fclose($fh);
                    unlink($myFile);
                    $_POST['image_id']=$image_id;

                }
            }
        }
    }
    else
        echo "<script type='text/javascript'>alert('The size of the profile image must be less than 100 kb!!!')</script>";

}
A: 

If you want a little function to conveniently resize you photos to whatever see below. if only $size1 is provided the image will be resized using this as its largest dimension, otherwise if $size2 is provided the image will be resized proportionally using $size1 as its largest dimension and then the rest is cropped. Might make it easier than your ($width<=0||$width>170||$height<=0||$height>200):

function resizeImg($name, $extension, $size1, $size2) {
    if (preg_match('/jpg|jpeg/',$extension)){
        $image = imagecreatefromjpeg($name);
    }
    if (preg_match('/gif/',$extension)){
        $image = imagecreatefromgif($name);
    }

    $old_width = imageSX($image);
    $old_height = imageSY($image);
    $old_aspect_ratio = $old_width/$old_height; 

    if($size2 == 0){
        $new_aspect_ratio = $old_aspect_ratio;
        if($old_width > $old_height){
            $new_width = $size1;
            $new_height = $new_width / $old_aspect_ratio;
        } else {
            $new_height = $size1;
            $new_width = $new_height * $old_aspect_ratio;
        }
    } elseif($size2 > 0){
        $new_aspect_ratio = $size1/$size2;
        //for landscape potographs
        if($old_aspect_ratio >= $new_aspect_ratio) {
            $new_width = $size1;
            $new_height = $size2;
            $x1 = round(($old_width - ($old_width * ($new_aspect_ratio/$old_aspect_ratio)))/2);
            $old_width = round($old_width * ($new_aspect_ratio/$old_aspect_ratio));
            $y1 = 0;
            //for portrait photographs
        } else{
            $new_width = $size1;
            $new_height = $size2;
            $x1 = 0;
            $y1 = round(($old_height/2) - ($new_height/2));
            $old_height = round($old_width/$new_aspect_ratio);
        }
    }

    $new_image = imagecreatetruecolor($new_width, $new_height);
    imagecopyresized($new_image, $image, 0, 0, $x1, $y1, $new_width, $new_height, $old_width, $old_height);

    return $new_image;
}
kalpaitch
Dude. http://programanddesign.com/php/resize-images-using-this-php-script/
Mark
+3  A: 

You've posted about 50 lines of code when you claim only one isn't working. Strip out the 50 lines of code and replace it with:

move_uploaded_file($_FILES['profile_foto']['temp_name'],'tempo/test.jpg');

....and find out what happens when you try to upload a file.

C.

symcbean
+1 for this approach. Always start with simple step, make it work, then add new feature one at a time, test to make sure it work, repeat. Start with a complex code will make you lost when finding bugs. You can also separate complex function into separate functions, like checking size, checking file type, doing post processing, etc.
Donny Kurnia
A: 

I think

$_FILES['profile_foto']['temp_name']

ought to be

$_FILES['profile_foto']['tmp_name']

in the statement

move_uploaded_file($_FILES['profile_foto']['temp_name'], $_FILES['profile_foto']['name']);
Ramesh Tabarna
You solved the problem thank you. The reason was I should use tmp_name instead of temp_name. Thank you very much for figuring that out from all of those complex algoritims.
Türker Php Coder