Jump to content

Problem with picture upload after upgrading PHP to ver.5.3.27


Go to solution Solved by mac_gyver,

Recommended Posts

Hi all.

I have a web page where user can upload pictures for publishing obituaries.

everything worked fine until upgrading server to PHP 5.3.27

 

Now new pictures are  not visible even in my CMS backoffice.

Photos uploaded before upgrade are still visible.

I can see that pictures are uploaded in folder /images/content/

anyone have some idea?

below is code for uploading pictures.

Thanks in advance .

 

 

 

 

{

if (is_uploaded_file($_FILES['slika']['tmp_name'])) 

{

$path = getenv('DOCUMENT_ROOT').'/images/content/'; 

 

$brojac = 1;

while (file_exists($path.$brojac.'.jpg')) $brojac = $brojac+1;

$slika = $brojac;

 

$filesaved = copy($_FILES['slika']['tmp_name'], $path.'o_'.$slika.'.jpg'); 

 

list($width, $height) = getimagesize($path.'o_'.$slika.'.jpg');

 

$v = exec("convert ".$path.'o_'.$slika.".jpg -resize 150x500 ".$path.$slika.'.jpg');

$v = exec("convert ".$path.$slika.".jpg -crop 150x200+0+0 ".$path.$slika.'.jpg');

}

 

anyone have some idea?

Turn on the error_reporting functions on the top of this page, which help you/us what could be causing this problem!

Edited by jazzman1

Turn on the error_reporting functions on the top of this page, which help you/us what could be causing this problem!

i have put this in first line

<?php
 
// Report all PHP errors
error_reporting(-1);
 
and where do I see errors now?

 

Put these two lines of code on the top of this file which code you posted above:

 error_reporting(E_ALL);
 ini_set("display_errors", 1);

I have done that now and no errors are displayed.

But i have noticed now that picture is saved in correct folder and named o_503.jpg

The letter o stands for original picture, and now it should be copied, resized and named 503.jpg without letter o in front.

 

$filesaved = copy($_FILES['slika']['tmp_name'], $path.'o_'.$slika.'.jpg');

 

That's wrong! You cannot use a copy function before the image has been successfully uploaded on the fileserver. Instead a copy function use the move_uploaded_file() !

That's wrong! You cannot use a copy function before the image has been successfully uploaded on the fileserver. Instead a copy function use the move_uploaded_file() !

No, image is uploaded on the server, but then it is supposed to be copied for editing ( crop, resize).

 

I have tried with move_uploaded_file() istead copy function, but it is the same.

 

Is there other way to do the same thing?

"$v = exec("convert ".$path.'o_'.$slika.".jpg -resize 150x500 ".$path.$slika.'.jpg');"

 

Does the server still allow you to use exec()? Becauset that's a very dangerous thing and is usually disabled.

Using the gd functions of PHP is safer and does the same thin to the image.

"$v = exec("convert ".$path.'o_'.$slika.".jpg -resize 150x500 ".$path.$slika.'.jpg');"

 

Does the server still allow you to use exec()? Becauset that's a very dangerous thing and is usually disabled.

Using the gd functions of PHP is safer and does the same thin to the image.

vinny, i think that you have found a problem. this function is not working.

everything is done before that, and than when it should be resized and renamed, nothing happens.

can you tell me witch gd function will do the job , because i want to have possibility in CMS to crop picture, and than that cropped image is displayed in size 150 width, and height is proportionally to resize %.

This is size for all images displayed on page, 150 width.

here is link to see http://www.osmrtnica.net/posljednji_pozdravi/str/3/

Thanks a lot.

Edited by Tomislav

Look at imagecopyresampled(), it is a little tricky but if you google for something like "php imagecopyresampled resize example" you should find tons of people who did this.

 

Note; there is also imagecopyresized(), but imagecopyresampled() gives much better results when the image is resized.

Look at imagecopyresampled(), it is a little tricky but if you google for something like "php imagecopyresampled resize example" you should find tons of people who did this.

 

Note; there is also imagecopyresized(), but imagecopyresampled() gives much better results when the image is resized.

tried code below, still not working, going to sleep maybe tomorrow will be better day :)

 

$path = getenv('DOCUMENT_ROOT').'/images/content/'; 
 
$brojac = 1;
while (file_exists($path.$brojac.'.jpg')) $brojac = $brojac+1;
$slika = $brojac;
 
$filesaved = copy($_FILES['slika']['tmp_name'], $path.'o_'.$slika.'.jpg'); 
 
// The file
$filename = $path.'o_'.$slika.'.jpg'';
 
// Set a maximum height and width
$width = 150;
$height = 500;
 
// Content type
header('Content-type: image/jpeg');
 
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
 
$ratio_orig = $width_orig/$height_orig;
 
if ($width/$height > $ratio_orig) {
  $width = $height*$ratio_orig;
} else {
  $height = $width/$ratio_orig;
}
 
// Resample
$path.$slika.'.jpg' = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($path.$slika.'.jpg', $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
 
// Output
imagejpeg($image_p, null, 100);

 

No, image is uploaded on the server, but then it is supposed to be copied for editing ( crop, resize).

 

Ah....I see that now :)

 

So, try that:

<?php

// set the path to img directory
$path = $_SERVER['DOCUMENT_ROOT'] . '/images/content/';

if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {

    $brojac = 1;

    while (file_exists($path . $brojac . '.jpg'))
        $brojac = $brojac + 1;

    $slika = $brojac;

    $filesaved = copy($_FILES['slika']['tmp_name'], $path . 'o_' . $slika . '.jpg');

    // set original image location
    $img = $path . 'o_' . $slika . '.jpg';

    // set our image canvas
    $canvas_width = 150;
    $canvas_height = 500;

    // get width and height of original image
    list($img_width, $img_height) = getimagesize($img);

    $ratio_orig = $img_width / $img_height;

    if ($canvas_width / $canvas_height > $ratio_orig) {
        $canvas_width = $canvas_height * $ratio_orig;
    } else {
        $canvas_height = $canvas_width / $ratio_orig;
    }

    // loading in our original image
    $original = imagecreatefromjpeg($img);

    // create a blank canvas
    $canvas = imagecreatetruecolor($canvas_width, $canvas_height);

    imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height);

    if (imagejpeg($canvas, $img, 100)) {

        return true;
    } else {
        return false;
    }
Edited by jazzman1
  • 1 month later...

 

Ah....I see that now :)

 

So, try that:

<?php

// set the path to img directory
$path = $_SERVER['DOCUMENT_ROOT'] . '/images/content/';

if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {

    $brojac = 1;

    while (file_exists($path . $brojac . '.jpg'))
        $brojac = $brojac + 1;

    $slika = $brojac;

    $filesaved = copy($_FILES['slika']['tmp_name'], $path . 'o_' . $slika . '.jpg');

    // set original image location
    $img = $path . 'o_' . $slika . '.jpg';

    // set our image canvas
    $canvas_width = 150;
    $canvas_height = 500;

    // get width and height of original image
    list($img_width, $img_height) = getimagesize($img);

    $ratio_orig = $img_width / $img_height;

    if ($canvas_width / $canvas_height > $ratio_orig) {
        $canvas_width = $canvas_height * $ratio_orig;
    } else {
        $canvas_height = $canvas_width / $ratio_orig;
    }

    // loading in our original image
    $original = imagecreatefromjpeg($img);

    // create a blank canvas
    $canvas = imagecreatetruecolor($canvas_width, $canvas_height);

    imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height);

    if (imagejpeg($canvas, $img, 100)) {

        return true;
    } else {
        return false;
    }

Hi,

 

still not working.

 

Image is uploaded on correct folder on server with prefix o_

 

And it is not resized, and not visible in my cms webpage.

After removing prefix o_ image is then visible but not resized, and crop function is not working, probably not supported after upgrade PHP server.

So what is to solve is resizing image and after that saving image without prefix o_, only number of picture.

 

If anyone have time to take a look for solution I would be very happy.

 

THX

  • 3 weeks later...

In fact, I've tested the script before to post it. What debugging steps have you taken so far? Did you get some error messages?

Hi, jazzman.

Had a lot of work, so i have just now tried to solve this thing again.

 

And after some time i have came to this, and that does the job, and saves image in root folder as $slika,jpg.

But it is supposed to be saved in root/images/content/ with name as number.

 

I can't find anywhere how to set destination folder.

here is code:

 

<?php
if (isset($_POST['todoaction']) && $_POST['todoaction'] == 'insert')
{
if (is_uploaded_file($_FILES['slika']['tmp_name'])) 
{
$path = getenv('DOCUMENT_ROOT').'/images/content/'; 
 
$brojac = 1;
while (file_exists($path.$brojac.'.jpg')) $brojac = $brojac+1;
$slika = $brojac;
 
$filesaved = copy($_FILES['slika']['tmp_name'], $path.'o_'.$slika.'.jpg'); 
 
// set original image location
   $img = $path . 'o_' . $slika . '.jpg';
 
   // set our image canvas
   $canvas_width = 150;
   $canvas_height = 500;
 
   // get width and height of original image
   list($img_width, $img_height) = getimagesize($img);
 
   $ratio_orig = $img_width / $img_height;
 
   if ($canvas_width / $canvas_height > $ratio_orig) {
       $canvas_width = $canvas_height * $ratio_orig;
   } else {
       $canvas_height = $canvas_width / $ratio_orig;
   }
 
   // loading in our original image
   $original = imagecreatefromjpeg($img);
 
   // create a blank canvas
   $canvas = imagecreatetruecolor($canvas_width, $canvas_height);
 
   imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height);
 
   if (imagejpeg($canvas, '$slika.jpg', 100)) {
 
       return true;
   } else {
       return false;
   }
 
}

Here is the latest update to post.

I got this warning now:

 

Warning: imagejpeg() [function.imagejpeg]: Unable to open '/images/content/503.jpg' for writing: No such file or directory in/home2/smiztita/public_html/cms/required/osmrtnice.php on line 40

 

And code is:

 

(imagejpeg($canvas, "/images/content/" . $slika.'.jpg', 100))

 

Seams that we are close to solving problem.

your code has a $path variable that defines the path to where you have/want the source file and the destination file. use that $path variable everywhere you perform a file operation.

this code has finally uploaded resized picture to correct folder with correct name.

 

imagejpeg($canvas, "/home2/smiztita/public_html//images/content/" . $slika.'.jpg', 100)

 

But, seems that my agony still remains.

Now when i upload picture via my cms, nothing happens in my form, nothing else is uploaded, and nothing is shown.

If i chose not to upload picture, everything is ok.

 

Should I give you the whole code, or are you giving up on me :)

the current code is somewhat a copy/paste of what someone posted in this thread and it contains return statements after the image resizing code. that will cause your code to stop at that point and return to the calling code (or if this is your main code, terminate execution.)

 

at a minimum, you need to remove the logic containing the return statements.

the current code is somewhat a copy/paste of what someone posted in this thread and it contains return statements after the image resizing code. that will cause your code to stop at that point and return to the calling code (or if this is your main code, terminate execution.)

 

at a minimum, you need to remove the logic containing the return statements.

 Can you tell me what to remove in code :

 

<?php
if (isset($_POST['todoaction']) && $_POST['todoaction'] == 'insert')
{
if (is_uploaded_file($_FILES['slika']['tmp_name'])) 
{
$path = getenv('DOCUMENT_ROOT').'/images/content/'; 
 
$brojac = 1;
while (file_exists($path.$brojac.'.jpg')) $brojac = $brojac+1;
$slika = $brojac;
 
$filesaved = copy($_FILES['slika']['tmp_name'], $path.'o_'.$slika.'.jpg'); 
 
// set original image location
   $img = $path . 'o_' . $slika . '.jpg';
 
   // set our image canvas
   $canvas_width = 150;
   $canvas_height = 500;
 
   // get width and height of original image
   list($img_width, $img_height) = getimagesize($img);
 
   $ratio_orig = $img_width / $img_height;
 
   if ($canvas_width / $canvas_height > $ratio_orig) {
       $canvas_width = $canvas_height * $ratio_orig;
   } else {
       $canvas_height = $canvas_width / $ratio_orig;
   }
 
   // loading in our original image
   $original = imagecreatefromjpeg($img);
 
   // create a blank canvas
   $canvas = imagecreatetruecolor($canvas_width, $canvas_height);
 
   imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height);
 
   if (imagejpeg($canvas, "/home2/smiztita/public_html//images/content/" . $slika . '.jpg', 100)) {
 
       return true;
   } else {
       return false;
   }
 
}

 

But, seems that my agony still remains.

Now when i upload picture via my cms, nothing happens in my form, nothing else is uploaded, and nothing is shown.

 

Well, turn on the error_reporting() function on the top of the script and tell us if you get errors with that cms.

Well, turn on the error_reporting() function on the top of the script and tell us if you get errors with that cms.

That is already turned on:

 
<?php
 
ini_set('display_errors', 'On');
// Report all PHP errors
error_reporting(-1);
 
if (isset($_POST['todoaction']) && $_POST['todoaction'] == 'insert')
 
After upload is finished, a few seconds after i have only my menu on left side, and on right side where form is supposed to be,
everything just vanish. 
  • Solution

in your original code, after the two exec(....) statements that resized/cropped the image, you had the remainder of your code that processed the form data? you still need for that code to run.

 

change this -

   if (imagejpeg($canvas, "/home2/smiztita/public_html//images/content/" . $slika . '.jpg', 100)) {

       return true;

   } else {

       return false;

   }

to this (note that i added a ! to complement the conditional test) - 
   if (!imagejpeg($canvas, "/home2/smiztita/public_html//images/content/" . $slika . '.jpg', 100)) {


      // do something here when this process fails - some application error reporting/logging perhaps...

   }

I still don't understand which one of following (bold) statement is true and which one is false.

 

 

Now when i upload picture via my cms, nothing happens in my form, nothing else is uploaded, and nothing is shown.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.