php problem

xecutech

New member
Oct 5, 2007
360
1
0
Nashville TN
www.adpals.com
Ok, I need one of you php experts to see if you can tell me what the heck is going on with a php script. I am thinking it's a folder permissions issue, but then again I didn't write this page, just trying to help a friend out.

Here is the error I get when I try to submit a pic for the gallery:

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'testimage.jpg' for writing: Permission denied in C:\Inetpub\vhosts\site.com\httpdocs\admyn\add_photo.php on line 94

Warning: copy(testimage.jpg) [function.copy]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\siteo.com\httpdocs\admyn\add_photo.php on line 96
Could not upload.

Here is the page code:

<?php
session_name ('!c0N1c0!');
session_start();
if (!isset($_SESSION['name'])) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/index.php';
header("Location: $url");
exit();
} else {
require_once('../cgi-bin/mysql_connect.php');

if(isset($_POST['id'])){
$id = $_POST['id'];
}else if(isset($_GET['id'])){
$id = $_GET['id'];
}

if(isset($_POST['gal'])){
$gal = $_POST['gal'];
}else if(isset($_GET['gal'])){
$gal = $_GET['gal'];
}

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

if(!empty($_POST['title'])) {
$title = $_POST['title'];
}

if(!empty($_POST['cat'])) {
$cat = $_POST['cat'];
}

if(!empty($_POST['gal'])) {
$gal = $_POST['gal'];
}

if (isset($_FILES['photo1'])) {

// Validate the type. Should be jpeg or jpg.
$allowed = array ('image/jpeg', 'image/jpg', 'image/pjpeg', 'image/bmp', 'image/x-xbitmap');
if (in_array($_FILES['photo1']['type'], $allowed)) {

if (is_uploaded_file ($_FILES['photo1']['tmp_name'])) {
define(IMAGE_BASE, '../gallery');
define(MAX_WIDTH, 800);
define(MAX_HEIGHT, 600);

# Get image location
$image_file = $_FILES['photo1']['tmp_name'];
$image_path = $_FILES['photo1']['tmp_name'];

# Load image
$img = @imagecreatefromjpeg($image_path);

# If an image was successfully loaded, test the image for size
if ($img) {

# Get image size and scale ratio
$width = imagesx($img);
//echo 'width: ' . $width . '<br />';
$height = imagesy($img);
//echo 'height: ' . $height . '<br />';
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
//echo 'scale: ' . $width . '<br />';

# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);

# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);

# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}

} else {

echo 'no $img';

}

imagejpeg($img, $_FILES['photo1']['name']);

if (copy($_FILES['photo1']['name'], "../gallery/".$_FILES['photo1']['name']) or die ('Could not upload.')) {



} else { // Couldn't move the file
echo '<p><font color="red">The file could not be moved.</font></p>';
$i = FALSE;
}
$i = $_FILES['photo1']['name'];
} else {
$i = FALSE;
}

} else {
echo '<p><font color="red">Please select .jpg or .jpeg file</font></p>';
}

} else {
$i = FALSE;
}

if($i){
$query = "INSERT INTO `gallery` (`photo`, `title`, `cat_id`, `gal_id`) VALUE ('$i', '$title', '$cat', '$gal')";
$result = @mysql_query ($query);
if (mysql_affected_rows() == 1) {
$message = 'The photo has been added.';
}else{
$message = 'The photo could not be added due to a system error.';
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Company - Add Photo</title>
<link rel="icon" href="../favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="../style.css" type="text/css">
<script src="../scripts/png.js"></script>
</head>
<body>
<div id="bgWrapper">
<div id="siteWrapper">
<div id="header"><a href="../index.php"><img src="../images/header_logo.gif" border="0px" alt="Connico - A Rider Levett Bucknall Company" /></a></div>
<div id="nav">
<?php include('nav.html'); ?>
</div>
<div style="clear:both;"></div>
<div id="secWrapper">
<div id="secBg">
<div id="copy">
<h1>View Photos in <?php echo $gallery; ?></h1>
<div id="galleryTopNav">
<a href="view_gallery.php?id=<?php echo $id; ?>&&gal=<?php echo $gal; ?>">Back To Gallery</a>
</div>
<?php
if($message){
echo'<p><font color="RED">' . $message . '</font></p>';
}else{
echo'<p> </p>';
}
?>

<form enctype="multipart/form-data" action="add_photo.php?id=<?php echo $id; ?>&&gal=<?php echo $gal; ?>" method="post">
<?php
$sql = "SELECT * FROM `categories` WHERE `cat_id` = '$id'";
$res = @mysql_query($sql);
$row = mysql_fetch_array($res, MYSQL_ASSOC);

echo'<p><b>Gallery:</b> ' . $row['title'] . '</p>';
?>
<p><b>Photo:</b> <input type="file" name="photo1" /><br>Must be in .jpeg or .jpg format.</p>
<p><b>Caption:</b> <input type="text" name="title" size="45" /></p>
<p><input type="submit" name="submit" value="Submit" /><input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="cat" value="<?php echo $id; ?>" /><input type="hidden" name="gal" value="<?php echo $gal; ?>" /><input type="hidden" name="id" value="<?php echo $id; ?>" /></p>
</form>
</div>
</div>
</div>
<div id="footer">
<?php include('../includes/footer.html'); ?>
</div>
</div>
</div>
</body>
</html>
 


You know there's a space in the filename, right?

Or is that just the forum messing with stuff?
 
When your writing to your server you have to do either:

1) (the sloppy way) Set the directory your writing to CHMOD 777.

or

2) (the more secure way) Change the owner of the folder to that of whatever user apache is running under, it's usually 'apache'. The quickest way to figure it out is the CHMOD the folder 777 and write the image to it like you would have done in step one. The owner of the file should be the user that apache is running though.
 
Thanks rage9, but I don't quite understand what you mean. The odd thing is that I can't chmod it. This is not like it is in cpanel, it's plesk and it is different than I am used to. It seems like different users have different permissions. I didn't see one for the user, in this case the site admin I think?
 
If you have Plesk, you're likely on a more "robust" hosting platform that has SSH enabled. If this is the case, you should ssh in and chmod it from there
 
SSH, CHMOD, 777...

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'testimage.jpg' for writing: Permission denied in C:\Inetpub\vhosts\site.com\httpdocs\admyn\add_phot o.php on line 94

It's fucking windows!
 
Shit, I overlooked that!!! My friend had said that it worked on his test pc before he put it on the server! Good catch, and now I have somewhere to start for sure.

I guess you don't know why it would be going to c:\ do you?

Thank you for pointing this out!
 
eso: Permissions on a windows box are a lot different then unix. You can't just 775 something. To my knowledge you can't change permissions really via FTP, at least not on any of my dedicated servers with server 2003's FTP server.
 
Yeah the Windows servers are fucked for PHP apps because of this.

I was going to suggest you create a script with just an upload box and then var_dump the $_FILES['photo1']['tmp_name'] to see if it has the image.

If it was getting that far then then it was easy to determine that the problem was permissions on your upload folder which happened to be the case here. Glad you got it figured out.