Image Upload overwrite

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Image Upload overwrite

Post by sulen »

The script below worked fine for uploading and resizing images and creating a large image and a thumbnail based on dimensions. The images are uploaded to the "images" folder which has a chmod of 777 on it. But for soem reason it is not able to overwrite the exisiting image of the same name and spits out these errors. This is really frustrating. Any help will be appreciated. Thanks

Code: Select all

Warning: imagejpeg(): Unable to open 'images/S1005G.jpg' for writing in /home/soderbrg/public_html/admin.php on line 1819
Warning: imagejpeg(): Unable to open 'images/S1005G_th.jpg' for writing in /home/soderbrg/public_html/admin.php on line 1840
The code is as under

Code: Select all

//////////////////////////////////////////////////////////////////////////////////////////
//////////////////// - Image Upload and Resize Function - ////////////////////////

if ($action=='uploadimg') {
if (session_is_registered("uid")) {
$prod_id=$_GET['prod_id'];

$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];
$split = explode(".", $filename);
$name=$split[0];

switch($mimetype) {
    case "image/jpg":
    case "image/jpeg":
		case "image/pjpeg":
        $i = imagecreatefromjpeg($temporary_name);
        break;
    case "image/gif":
        $i = imagecreatefromgif($temporary_name);
        break;
    case "image/png":
        $i = imagecreatefrompng($temporary_name);
        break;
}

unlink($temporary_name);

//imagejpeg($i,"images/uploadedfile.jpg",80);

//Specify the size of the new product image
$dest_x = 300;
$dest_y = 300;

if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    if (imagesx($i) >= imagesy($i)) {
        $thumb_x = $dest_x;
        $thumb_y = imagesy($i)*($dest_x/imagesx($i));
    } else {
        $thumb_x = imagesx($i)*($dest_y/imagesy($i));
        $thumb_y = $dest_y;
    }
} else {
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
//Save the product image
imagejpeg($thumb, "images/".$name.".jpg", 80);

//Creating the thumbnail image
$dest_x = 75;
$dest_y = 75;

if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    if (imagesx($i) >= imagesy($i)) {
        $thumb_x = $dest_x;
        $thumb_y = imagesy($i)*($dest_x/imagesx($i));
    } else {
        $thumb_x = imagesx($i)*($dest_y/imagesy($i));
        $thumb_y = $dest_y;
    }
} else {
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
imagejpeg($thumb, "images/".$name."_th.jpg", 80);

$sql="update product_pages set prod_image='$filename', prod_image_th='".$name."_th.jpg' where prod_id='".$prod_id."'";
$result = mysql_query($sql);

?>
<tr>
<td class="text" width="100%" align="center">The Image  : <b><?=$filename?></b> has been added for the Product.</td>
</tr>
<tr>
<td class='text' align='center'><a href="admin.php?action=uploadfile&prod_id=<?=$prod_id?>" class="link">Click here to Upload a Document</a> | <a href="admin.php?action=admin" class="link">Admin Main Page</a></td>
</tr>
<?

} else {
?>
<script LANGUAGE="JavaScript">
<!--
location='admin.php?action=login ';
// -->
</script>
<?
}
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Is the 'images' folder in /home/soderbrg/public_html/ ? If not, your path is wrong.

What are the permissions on the images in the folder? The folder having 777 just means anyone can get into it - it has nothing to do with the individual permissions on the files.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I changed the permissions of a particular image to 777 and the ovewrite worked. Is there a way to set the permissions of all images uploaded to be 777 automatically through the upload script. If so do let me know. Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Try setting a 'sticky bit': something like ">chmod 2777". Might want to look into it a bit more.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Added this to the code to chnge the permissions on all files going in

Code: Select all

chmod("images/".$name.".jpg", 0755);
Solves the issue. Thanks for the insight
Post Reply