Upload script making images 0kb in new hosting

Discussion in 'PHP' started by cervantes01, Oct 14, 2008.

  1. #1
    Hi Guys, I'm new to this forum and php in general, I've taken over a website and inherited some code which worked perfectly previously. I've moved the site's hosting to a new provider which as a more modern version of php installed. Presently it's showing as php5.2.5. The old server had 2.1 (I think). I am also using MySQL5.0.6.7 anyway I have a script to upload a picture and resize it. The resized images are showing as 0kb in the site directory once uploaded. I never had this problem before.

    The guys who manage the new hosting cant see any reason why I should have this problem now. I've examinded the code and have traced the 'fault' to the part of the script that resizes the image. Code is as follows:

    ___________________________________________________________________

    <?
    if( isset( $_SESSION['valid_user'] ) )
    {
    //unlink('images/properties/000247/test2.jpg');
    // Get Post Values
    $recno = $_POST['p_recno'];
    $element_id = $_POST['p_element_id'];
    //echo "<br>Recno = $recno<br><br>";


    // SQL to get the P_Code
    $p_code_sql = "SELECT P_Code FROM Property WHERE Recno = '$recno'";
    $result = mysql_query( $p_code_sql ) or Die(mysql_error());
    $row = mysql_fetch_array( $result );
    //echo "SQL to get P_Code = $p_code_sql<br><br>P_Code = " . $row['P_Code'] . "<br><br>";


    // Path to file location
    $dirname = "images/properties/" . $row['P_Code'];
    //echo "Directory name = $dirname<br><br>";

    // File to process
    //$file = $_FILES['p_file'];

    // Prepare filename for use (strip whitespace etc)
    $filename = str_replace( "%", "_", $_FILES['p_file']['name'] );
    $filename = str_replace( " ", "_", $filename );
    //echo "Filename to use = $filename<br><br>";
    //echo "Temp File = " . $_FILES['p_file']['tmp_name'] . "<br><br>";

    // Full path including filename
    $newfile = $dirname . "/$filename";
    //echo "Full file path including filename = $newfile<br><br>";

    // Get the file extension of the photo ...
    $pext = getFileExtension( $filename );
    //echo "File Extension of the uploaded file = $pext<br>";
    // ... make the extension lowercase
    $pext = strtolower($pext);
    //echo " ... and as lowercase = $pext<br><br>";


    // Check to see if the file extension is correct
    if (($pext != "jpg") && ($pext != "jpeg"))
    {
    print "<h1>Image must be a JPEG or JPG</h1><hr />";
    print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg<br><br>";
    print "The file you uploaded had the following extension: <b>$pext</b></p>\n";

    // delete uploaded file
    unlink($_FILES['p_file']['tmp_name']);
    exit();
    }

    // Check to see if the directory exists ...
    if( !file_exists( $dirname ))
    {
    // ... if it doesn't exists create it
    mkdir( $dirname );
    }

    // Get the size of the image
    $imgsize = GetImageSize( $_FILES['p_file']['tmp_name'] );

    //foreach( $imgsize as $item )
    //{
    // echo"Part of imgsize array = $item<br>";
    //}
    //echo "<br>";

    // Set Required Image Dimensions here
    $x = 640;
    $y = 480;

    // Check if the Image is larger than 250x200. If so ...
    if (($imgsize[0] > $x) || ($imgsize[1] > $y))
    {
    $file = $_FILES['p_file']['tmp_name'];
    // ... create a temporary filename to use ...
    $tmpimg = tempnam("/tmp", "MKUP");
    // ... decompress jpeg to pnm using a system command. Store it at our temporary filename ...
    system("djpeg $file > $tmpimg");
    // ... scale image and output as jpeg ...
    system("pnmscale -xy $x $y $tmpimg | cjpeg -smoo 10 -qual 50 > $file");
    // ... and finally, Destroy the temporary file
    unlink( $tmpimg );
    }

    //$imgsize = GetImageSize( $_FILES['p_file']['tmp_name'] );
    //foreach( $imgsize as $item )
    //{
    // echo"Part of imgsize array after = $item<br>";
    //}
    //echo "<br>";

    // Security check to move the file safely
    if (move_uploaded_file($_FILES['p_file']['tmp_name'], $newfile))
    {
    // Store location of photo in database
    $path = "/" . $row['P_Code'] . "/$filename";
    //echo "Path for Database = $path<br>";
    $sql = "INSERT INTO Sales_Particulars (Property_ID, SP_Element_ID, SP_Show_Element, SP_Picture_Path)
    VALUES('$recno', $element_id, 1, '$path')";
    //echo "SQL for path in database = $sql<br><br>";
    mysql_query( $sql ) or Die(mysql_error());

    // Store history details
    $username = $_SESSION['valid_user'];
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $timestamp = time();

    $history = "INSERT INTO history (user, user_ip, timestamp, action) VALUES ('$username', '$user_ip', '$timestamp', 'UPLOADED PHOTO FOR $recno');";
    mysql_query( $history ) or Die(mysql_error());

    // Show successful and display some navigation
    echo "<h1>Photograph has been uploaded successfully</h1><hr />
    <div id-\"content\">
    <div id=\"subPage\" class=\"navVert\">
    <ul>
    <li><a href=\"?page=property_detail.php&recno=$recno\">Return to Property Details</a></li>
    <li><a href=\"?page=property_edit.php&recno=$recno\">Edit Property</a></li>
    <li><a href=\"?page=admin\">Return to Admin Page</a></li>
    </ul>
    </div>
    </div>";
    }
    else
    {
    echo "Possible file upload attack!\n";
    }

    }
    else
    {
    echo "<h1>You are not logged in</h1><hr /> <a href=\"?page=admin\">Click to login</a>";
    }
    ?>

    _________________________________________________________________

    The script processes an uploaded image and stores data and image location in a MySQL database. I've checked this hundreds of times and cant see any reason why it should suddenly stop working. Can Anyone Help!

    Cheers

    Matt
     
    cervantes01, Oct 14, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    I also can't see what the problem is :? it should work fine!
     
    EricBruggema, Oct 15, 2008 IP
  3. cervantes01

    cervantes01 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for taking a look at it, I think it may have something to do with server porting or being built to work with an older version of php. I've narrowed the problem down to resizing of the images, as anything above 100kb (or approx) fails to upload and shows as 0kb.
     
    cervantes01, Oct 15, 2008 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    It could be a PHP problem, have you tried to install another webserver like XAMP?
     
    EricBruggema, Oct 15, 2008 IP
  5. cervantes01

    cervantes01 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The problem is that the new server doesn't support pnms scale. I've amended the code to use imagemagick which the server does support and tried to integrate it. The upload works but the recaling doesn't code is as follows: Any ideas anyone.

    <?
    if( isset( $_SESSION['valid_user'] ) )
    {
    //unlink('images/properties/000247/test2.jpg');
    // Get Post Values
    $recno = $_POST['p_recno'];
    $element_id = $_POST['p_element_id'];
    //echo "<br>Recno = $recno<br><br>";


    // SQL to get the P_Code
    $p_code_sql = "SELECT P_Code FROM Property WHERE Recno = '$recno'";
    $result = mysql_query( $p_code_sql ) or Die(mysql_error());
    $row = mysql_fetch_array( $result );
    //echo "SQL to get P_Code = $p_code_sql<br><br>P_Code = " . $row['P_Code'] . "<br><br>";


    // Path to file location
    $dirname = "images/properties/" . $row['P_Code'];
    //echo "Directory name = $dirname<br><br>";

    // File to process
    //$file = $_FILES['p_file'];

    // Prepare filename for use (strip whitespace etc)
    $filename = str_replace( "%", "_", $_FILES['p_file']['name'] );
    $filename = str_replace( " ", "_", $filename );
    //echo "Filename to use = $filename<br><br>";
    //echo "Temp File = " . $_FILES['p_file']['tmp_name'] . "<br><br>";

    // Full path including filename
    $newfile = $dirname . "/$filename";
    //echo "Full file path including filename = $newfile<br><br>";

    // Get the file extension of the photo ...
    $pext = getFileExtension( $filename );
    //echo "File Extension of the uploaded file = $pext<br>";
    // ... make the extension lowercase
    $pext = strtolower($pext);
    //echo " ... and as lowercase = $pext<br><br>";


    // Check to see if the file extension is correct
    if (($pext != "jpg") && ($pext != "jpeg"))
    {
    print "<h1>Image must be a JPEG or JPG</h1><hr />";
    print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg<br><br>";
    print "The file you uploaded had the following extension: <b>$pext</b></p>\n";

    // delete uploaded file
    unlink($_FILES['p_file']['tmp_name']);
    exit();
    }

    // Check to see if the directory exists ...
    if( !file_exists( $dirname ))
    {
    // ... if it doesn't exists create it
    mkdir( $dirname );
    }

    // Get the size of the image
    $imgsize = GetImageSize( $_FILES['p_file']['tmp_name'] );

    //foreach( $imgsize as $item )
    //{
    // echo"Part of imgsize array = $item<br>";
    //}
    //echo "<br>";

    // Set Required Image Dimensions here
    $x = 600;
    $y = 400;

    // Check if the Image is larger than 600x400. If so ...
    if (($imgsize[0] > $x) || ($imgsize[1] > $y))
    {
    $file = $_FILES['p_file']['tmp_name']; //create a temporary file name to use
    $src = imagecreatefromjpeg ($file); //create an image from the temporary file to do the resize
    list ($width, $height)=getimagesize($file); //capture the original image size
    $newwidth=600;
    $newheight=($height/$width)*600;
    $tmp=imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($tmp,$src,0,0,0,0, $newwidth,$newheight,$width,$height);
    imagedestroy ($src);
    imagedestroy ($tmp); // Note this will clean up temp file

    //---------------------------------------------------------------------------------
    //--------old code uses pnmsscale -----------------------------------------------------------------
    //$file = $_FILES['p_file']['tmp_name'];
    // ... create a temporary filename to use ...
    //$tmpimg = tempnam("/tmp", "MKUP");
    // ... decompress jpeg to pnm using a system command. Store it at our temporary filename ...
    //system("djpeg $file > $tmpimg");
    // ... scale image and output as jpeg ...
    //system("pnmscale -xy $x $y $tmpimg | cjpeg -smoo 10 -qual 50 > $file");
    // ... and finally, Destroy the temporary file
    //unlink( $tmpimg );
    //--------------------------------------------------------------------------------------------------

    }

    //--------------------------------------------------------------------------------------------------

    //$imgsize = GetImageSize( $_FILES['p_file']['tmp_name'] );
    //foreach( $imgsize as $item )
    //{
    // echo"Part of imgsize array after = $item<br>";
    //}
    //echo "<br>";

    // Security check to move the file safely
    if (move_uploaded_file($_FILES['p_file']['tmp_name'], $newfile))
    {
    // Store location of photo in database
    $path = "/" . $row['P_Code'] . "/$filename";
    //echo "Path for Database = $path<br>";
    $sql = "INSERT INTO Sales_Particulars (Property_ID, SP_Element_ID, SP_Show_Element, SP_Picture_Path)
    VALUES('$recno', $element_id, 1, '$path')";
    //echo "SQL for path in database = $sql<br><br>";
    mysql_query( $sql ) or Die(mysql_error());

    // Store history details
    $username = $_SESSION['valid_user'];
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $timestamp = time();

    $history = "INSERT INTO history (user, user_ip, timestamp, action) VALUES ('$username', '$user_ip', '$timestamp', 'UPLOADED PHOTO FOR $recno');";
    mysql_query( $history ) or Die(mysql_error());

    // Show successful and display some navigation
    echo "<h1>Photograph has been uploaded successfully</h1><hr />
    <div id-\"content\">
    <div id=\"subPage\" class=\"navVert\">
    <ul>
    <li><a href=\"?page=property_detail.php&recno=$recno\">Return to Property Details</a></li>
    <li><a href=\"?page=property_edit.php&recno=$recno\">Edit Property</a></li>
    <li><a href=\"?page=admin\">Return to Admin Page</a></li>
    </ul>
    </div>
    </div>";
    }
    else
    {
    echo "Possible file upload attack!\n";
    }

    }
    else
    {
    echo "<h1>You are not logged in</h1><hr /> <a href=\"?page=admin\">Click to login</a>";
    }
    ?>
     
    cervantes01, Oct 21, 2008 IP