Page 1 of 1

imagejpeg() Overwrite

Posted: Wed Feb 14, 2007 10:08 pm
by AliasBDI
I am getting a error saying:
Unable to open '../eControl_repository/File/Accounts/Jacob_1000.jpg' for writing in ...
Is it possible that it will not copy this file because it already exists there? I am wanting it to overwrite. Here is my code:

Code: Select all

$dest_x = 500;
	$dest_y = 400;
	if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
		if (imagesx($i) >= imagesy($i)) {
			$img_x = $dest_x;
			$img_y = imagesy($i)*($dest_x/imagesx($i));
		} else {
			$img_x = imagesx($i)*($dest_y/imagesy($i));
			$img_y = $dest_y;
		}
	} else {
		$img_x = imagesx($i);
		$img_y = imagesy($i);
	}
	$img = imagecreatetruecolor($img_x,$img_y);
	imagecopyresampled($img, $i,0, 0, 0, 0, $img_x, $img_y, imagesx($i), imagesy($i));
	if ($typeID==1) { // is jpg
		$imageNewName = $acctFirstName."_".$acctID.".jpg";
		imagejpeg($img, "../eControl_repository/File/Accounts/".$imageNewName, 100);
	} elseif ($typeID==2) { // is gif
		$imageNewName = $acctFirstName."_".$acctID.".gif";
		imagegif($img, "../eControl_repository/File/Accounts/".$imageNewName);
	}
I also wanted to create a jpg from the gif so that no gifs are uploaded. How would that work with imagecreatefromgif()?

Posted: Thu Feb 15, 2007 12:56 am
by feyd
What's the rest of the error?

Posted: Thu Feb 15, 2007 7:31 am
by AliasBDI
Sorry, I thought that was explanatory - shows you what I know! Here you go, and thanks for the help.
Warning: imagejpeg() [function.imagejpeg]: Unable to open '../eControl_repository/File/Accounts/Jacob_1000.jpg' for writing in /mnt/Target01/331444/331704/www.domainname.com/web/content/account/exec_update.php on line 106

Posted: Thu Feb 15, 2007 7:49 am
by feyd
I was hoping the rest of the error would shed some more light, but it didn't really.

Are you sure the path you wish to write the file to exists and is writable by php? touch() may help, but if the path doesn't exist, it probably won't.

Posted: Thu Feb 15, 2007 8:54 am
by AliasBDI
I ran a touch() on the file and received this error:
Warning: touch() [function.touch]: Utime failed: Permission denied in /mnt/Target01/331444/331704/www.lukatchoo.com/web/content/account/exec_update.php on line 106

Posted: Thu Feb 15, 2007 9:19 am
by feyd
Try using realpath() on the path you've generated. Is it correct? If so, check the permissions of the directories involved for the PHP user.

Posted: Thu Feb 15, 2007 9:38 am
by AliasBDI
realpath() gave me this:
Here is the thing. This form execution is uploading an image that it uploaded before. So the permissions and path are set and correct. The problem is that it is copying to the same file with the same name. I think that it is not overwriting. In fact, when I change the name just a bit, it will indeed work fine. So the problem is that it will not overwrite the file. It is getting hung up on copying over a file.

Any ideas on rewrite?

Posted: Thu Feb 15, 2007 9:41 am
by feyd

Posted: Thu Feb 15, 2007 10:42 am
by AliasBDI
Now there is no error, but it is not doing anything. Here is the full code:

Code: Select all

if ($_FILES['acctImgPath']['size'] > 307200) {
	echo 'You cannot upload files larger than 300 KB. Please try again.';
	header("Location: /account/?updatefailed");
	exit();
}
$filename = $_FILES['acctImgPath']['name'];
$temporary_name = $_FILES['acctImgPath']['tmp_name'];
$mimetype = $_FILES['acctImgPath']['type'];
$filesize = $_FILES['acctImgPath']['size'];
switch($mimetype) {
	case "image/jpg":
		$i = imagecreatefromjpeg($temporary_name);
		$typeID = 1;
		break;
	case "image/jpeg":
		$i = imagecreatefromjpeg($temporary_name);
		$typeID = 1;
		break;
	case "image/pjpeg": //IE's weird jpeg MIME type
		$i = imagecreatefromjpeg($temporary_name);
		$typeID = 1;
		break;
	case "image/gif":
		$i = imagecreatefromgif($temporary_name);
		$typeID = 2;
		break;
}
unlink($temporary_name);
unlink("../eControl_repository/File/Accounts/".$acctImgPathOld);
unlink("../eControl_repository/File/Accounts/thumbs/".$acctImgPathOld);
// =================================== UPLOAD LARGE IMAGE // RESIZE TO LIMITS
$dest_x = 500;
$dest_y = 400;
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
	if (imagesx($i) >= imagesy($i)) {
		$img_x = $dest_x;
		$img_y = imagesy($i)*($dest_x/imagesx($i));
	} else {
		$img_x = imagesx($i)*($dest_y/imagesy($i));
		$img_y = $dest_y;
	}
} else {
	$img_x = imagesx($i);
	$img_y = imagesy($i);
}
$img = imagecreatetruecolor($img_x,$img_y);
imagecopyresampled($img, $i,0, 0, 0, 0, $img_x, $img_y, imagesx($i), imagesy($i));
if ($typeID==1) { // is jpg
	$imageNewName = $acctFirstName."_".$acctID.".jpg";
	imagejpeg($img, "../eControl_repository/File/Accounts/".$imageNewName, 100);
} elseif ($typeID==2) { // is gif
	$imageNewName = $acctFirstName."_".$acctID.".gif";
	imagegif($img, "../eControl_repository/File/Accounts/".$imageNewName);
}
// =================================== RESIZE AND MAKE THUMBNAIL
$dest_x = 50;
$dest_y = 50;
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 = imagecreate($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
if ($typeID==1) { // is jpg
	$imageThNewName = $acctFirstName."_".$acctID.".jpg";
	imagejpeg($thumb, "../eControl_repository/File/Accounts/thumbs/".$imageThNewName, 80);
} elseif ($typeID==2) { // is gif
	$imageThNewName = $acctFirstName."_".$acctID.".gif";
	imagegif($thumb, "../eControl_repository/File/Accounts/thumbs/".$imageThNewName);
}

Posted: Thu Feb 15, 2007 1:16 pm
by AliasBDI
Bro, thanks. It is working. I just needed to do a hard refresh. Do you know how I can force a hard refresh or a clear of cache to prevent others from this problem?

Posted: Thu Feb 15, 2007 4:36 pm
by feyd
Other's likely won't have the problem as they won't be looking at older code. At any rate, you can use header() to ask the browser not to cache the page code.

Randomizing the URL (with a query string of garbage) helps keep the browser from caching the page too.